Olá a todos mais uma vez, após ter sanado minha dúvida quanto ao uso do \label
e \ref
. Lendo os comentários, percebi que em muitas ocasiões não é necessário utilizar o sistema de referência, apenas os valores associados a estes.
Minha idéia é não usar arquivos auxiliares (precisa de muitos arquivos em alguns casos, anexar modo em .aux
arquivo que não existe no TeX), salvar na memória listar respostas curtas para exercícios, assim ao copiar e colar de um arquivo para outro não precisa fazer grandes modificações (mudar apenas uma referência à lista).
Este é o arquivo (que funciona)
% !file: test.tex
% arara: pdflatex
\documentclass{article}%
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\usepackage{pgffor}
% Set key for multicols in enumitem
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
% Some definition
\newtheorem{theorem}{Theorem}
\newtheorem{exercise}[theorem]{Exercise}
% from https://tex.stackexchange.com/a/215571/7832
\ExplSyntaxOn
\NewDocumentCommand{\storedata}{mm}
{
\bcp_store_data:nn { #1 } { #2 }
}
\NewDocumentCommand{\appenddata}{mm}
{
\bcp_append_data:nn { #1 } { #2 }
}
\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
{
\bcp_get_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_store_data:nn #1 #2
{
% create the sequence if it doesn't exist or clear it if it exists
\seq_if_exist:cTF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
{ \seq_clear:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_append_data:nn #1 #2
{
% create the sequence if it doesn't exist, do nothing if it exists
\seq_if_exist:cF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
{
% append items one at a time
\tl_map_inline:nn { #2 }
{
\seq_put_right:cn { l_bcp_data_#1_seq } { ##1 }
}
}
\cs_new:Npn \bcp_get_data:nn #1 #2
{
% retrieve the requested item
\seq_item:cn { l_bcp_data_#2_seq } { #1 }
}
\ExplSyntaxOff
\begin{document}
\section{Exercices}
\begin{exercise}
Factoring next expression:
\end{exercise}
\begin{enumerate}
\item $a^{2}-b^{2}$
\item $x^{2}-2x+1$
\item $3x+3y+3z$
\item $3x+3y-6z$
\end{enumerate}
% save in list exe:1
\appenddata{exe:1}{{$\left(a-b\right)\left(a+b\right)$}}
\appenddata{exe:1}{{$\left(x-1\right)^{2}$}}
\appenddata{exe:1}{{$3x+3y+3z$}}
\appenddata{exe:1}{{$3\left(x+y-2z\right)$}}
\begin{exercise}
Solve:
\end{exercise}
\begin{enumerate}
\item $2x+3=5$
\item $5x-2=7$
\item $4-3x=5$
\end{enumerate}
% save in list exe:2
\appenddata{exe:2}{{$1$}}
\appenddata{exe:2}{{$\dfrac{9}{5}$}}
\appenddata{exe:2}{{$-\dfrac{1}{3}$}}
\section{Answer}
% #1 : pass to tcolorbox
% #2 : name
% #3 : list
% #4 : maxvalue
% #5 : columns
\DeclareTotalTColorBox{\answer}{O{} m m m O{4}}
{ colback=white,size=small,top=0mm,bottom=1.5mm, left=0mm,width=\columnwidth,title filled,%
fontupper=\small,fonttitle=\small\sffamily,%
adjusted title={#2},center title,#1}
{%
\begin{enumerate}[columns=#5,leftmargin=15pt,labelsep=3pt,font=\footnotesize,nosep,widest=25,]%
\small
\setlength{\columnsep}{0pt}
\foreach \x in {1,...,#4} {
\item \getdata[\x]{#3}%
}
\end{enumerate}
}%
\answer{Exercise 1}{exe:1}{4}
\answer{Exercise 2}{exe:2}{3}[3]
\end{document}
Agora vem o meu problema, a macro que tireiArmazenando uma matriz de strings em um comandosó funciona em modo local, se escrever dentro de um ambiente ele só existe neste e não fora dele, ou seja, não conseguirei acessá-lo fora do ambiente. Por exemplo:
% save list in enumerate
\begin{enumerate}
\item $a^{2}-b^{2}$ \appenddata{exe:1}{{$\left(a-b\right)\left(a+b\right)$}}
\item $x^{2}-2x+1$ \appenddata{exe:1}{{$\left(x-1\right)^{2}$}}
\item $3x+3y+3z$ \appenddata{exe:1}{{$3x+3y+3z$}}
\item $3x+3y-6z$ \appenddata{exe:1}{{$3\left(x+y-2z\right)$}}
\end{enumerate}
E minha ideia é que funcione dentro de ambientes enumerados, e depois acesse valores fora deles. Eu tentei com isso:
% !file: test.tex
% arara: pdflatex
\documentclass{article}%
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\usepackage{pgffor}
% Set key for multicols in enumitem
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
% Some definition
\newtheorem{theorem}{Theorem}
\newtheorem{exercise}[theorem]{Exercise}
% from https://tex.stackexchange.com/a/215571/7832
\ExplSyntaxOn
\NewDocumentCommand{\storedata}{mm}
{
\bcp_store_data:nn { #1 } { #2 }
}
\NewDocumentCommand{\appenddata}{mm}
{
\bcp_append_data:nn { #1 } { #2 }
}
\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
{
\bcp_get_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_store_data:nn #1 #2
{
% create the sequence if it doesn't exist or clear it if it exists
\seq_if_exist:cTF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
{ \seq_clear:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_append_data:nn #1 #2
{
% create the sequence if it doesn't exist, do nothing if it exists
\seq_if_exist:cF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
{
% append items one at a time
\tl_map_inline:nn { #2 }
{
\seq_put_right:cn { l_bcp_data_#1_seq } { ##1 }
}
}
\cs_new:Npn \bcp_get_data:nn #1 #2
{
% retrieve the requested item
\seq_item:cn { l_bcp_data_#2_seq } { #1 }
}
\ExplSyntaxOff
% Create a environment to encapsulate answer
\NewDocumentEnvironment{myenumerate}{m}{%
% Command to save answer
\NewDocumentCommand\sol{m}{%
\appenddata{#1}{{##1}}%
}% close \sol
}%
{}% close myenumerate
\begin{document}
\section{Exercices}
\begin{exercise}
Factoring next expression:
\end{exercise}
\begin{myenumerate}{exe:1}
\begin{enumerate}
\item $a^{2}-b^{2}$ \sol{$\left(a-b\right)\left(a+b\right)$}
\item $x^{2}-2x+1$ \sol{$\left(x-1\right)^{2}$}
\item $3x+3y+3z$ \sol{$3x+3y+3z$}
\item $3x+3y-6z$ \sol{$3\left(x+y-2z\right)$}
\end{enumerate}
\end{myenumerate}
\begin{exercise}
Solve:
\end{exercise}
\begin{myenumerate}{exe:2}
\begin{enumerate}
\item $2x+3=5$ \sol{$1$}
\item $5x-2=7$ \sol{$\dfrac{9}{5}$}
\item $4-3x=5$ \sol{$-\dfrac{1}{3}$}
\end{enumerate}
\end{myenumerate}
\section{Answer}
% #1 : pass to tcolorbox
% #2 : name
% #3 : list
% #4 : maxvalue default: lenght of list
% #5 : columns
\DeclareTotalTColorBox{\answer}{O{} m m m O{4}}
{ colback=white,size=small,top=0mm,bottom=1.5mm, left=0mm,width=\columnwidth,title filled,%
fontupper=\small,fonttitle=\small\sffamily,%
adjusted title={#2},center title,#1}
{%
\begin{enumerate}[columns=#5,leftmargin=15pt,labelsep=3pt,font=\footnotesize,nosep,widest=25,]%
\small
\setlength{\columnsep}{0pt}
\foreach \x in {1,...,#4} {
\item \getdata[\x]{#3}%
}
\end{enumerate}
}%
\answer{Exercise 1}{exe:1}{4}
\answer{Exercise 2}{exe:2}{3}[3]
\end{document}
Tente também adicionar uma chave para enumitem
evitar escrever um novo ambiente
\SetEnumitemKey{reflist}{before=\storedata{#1}{}}%
Mas nenhuma das opções que tentei funcionou, então aí vem a grande questão: como faço para modificar \appendata|\storedata
para que funcionem globalmente?
\appendata
seria como \label
e \getdata
seria como \ref
, claro, apenas com valores e sem .aux
arquivo (ou duas execuções).
Responder1
Para armazenar os dados globalmente, \seq_gput_right:Nn
(e suas variantes) deve ser aplicado, caso contrário o salvamento local será perdido após o término do grupo formado pelo enumerate
ambiente.
\seq_put_right:Nn
no entanto, funciona apenas dentro de um grupo.
\documentclass{article}%
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\usepackage{pgffor}
% Set key for multicols in enumitem
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
% Some definition
\newtheorem{theorem}{Theorem}
\newtheorem{exercise}[theorem]{Exercise}
% from https://tex.stackexchange.com/a/215571/7832
\ExplSyntaxOn
% Reporter macro, is expandable
\cs_new:Npn \reportnumberofseqitems #1{%
\seq_count:c {l_bcp_data_#1_seq}%
}
\NewDocumentCommand{\storedata}{mm}
{
\bcp_store_data:nn { #1 } { #2 }
}
\NewDocumentCommand{\appenddata}{mm}
{
\bcp_append_data:nn { #1 } { #2 }
}
\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
{
\bcp_get_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_store_data:nn #1 #2
{
% create the sequence if it doesn't exist or clear it if it exists
\seq_if_exist:cTF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
{ \seq_gclear:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \bcp_append_data:nn #1 #2
{
% create the sequence if it doesn't exist, do nothing if it exists
\seq_if_exist:cF { l_bcp_data_#1_seq }
{ \seq_new:c { l_bcp_data_#1_seq } }
% append the items
\__bcp_append_data:nn { #1 } { #2 }
}
\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
{
% append items one at a time
\tl_map_inline:nn { #2 }
{
\seq_gput_right:cn { l_bcp_data_#1_seq } { ##1 }
}
}
\cs_new:Npn \bcp_get_data:nn #1 #2
{
% retrieve the requested item
\seq_item:cn { l_bcp_data_#2_seq } { #1 }
}
\ExplSyntaxOff
% Create a environment to encapsulate answer
\NewDocumentEnvironment{myenumerate}{m}{%
% Command to save answer
\NewDocumentCommand\sol{m}{%
\appenddata{#1}{{##1}}%
}% close \sol
}%
{}% close myenumerate
\begin{document}
\section{Exercices}
\begin{exercise}
Factoring next expression:
\end{exercise}
\begin{myenumerate}{exe:1}
\begin{enumerate}
\item $a^{2}-b^{2}$ \sol{$\left(a-b\right)\left(a+b\right)$}
\item $x^{2}-2x+1$ \sol{$\left(x-1\right)^{2}$}
\item $3x+3y+3z$ \sol{$3x+3y+3z$}
\item $3x+3y-6z$ \sol{$3\left(x+y-2z\right)$}
\end{enumerate}
\end{myenumerate}
\begin{exercise}
Solve:
\end{exercise}
\begin{myenumerate}{exe:2}
\begin{enumerate}
\item $2x+3=5$ \sol{$1$}
\item $5x-2=7$ \sol{$\dfrac{9}{5}$}
\item $4-3x=5$ \sol{$-\dfrac{1}{3}$}
\end{enumerate}
\end{myenumerate}
There are \reportnumberofseqitems{exe:1} items in exe:1 and \reportnumberofseqitems{exe:2} items in exe:2
\section{Answer}
% #1 : pass to tcolorbox
% #2 : name
% #3 : list
% #4 : maxvalue default: lenght of list
% #5 : columns
\DeclareTotalTColorBox{\answer}{O{} m m m O{4}}
{ colback=white,size=small,top=0mm,bottom=1.5mm, left=0mm,width=\columnwidth,title filled,%
fontupper=\small,fonttitle=\small\sffamily,%
adjusted title={#2},center title,#1}
{%
\begin{enumerate}[columns=#5,leftmargin=15pt,labelsep=3pt,font=\footnotesize,nosep,widest=25,]%
\small
\setlength{\columnsep}{0pt}
\foreach \x in {1,...,#4} {
\item \getdata[\x]{#3}%
}
\end{enumerate}
}%
\answer{Exercise 1}{exe:1}{4}
\answer{Exercise 2}{exe:2}{3}[3]
\end{document}