
Hola a todos una vez más, después de haber resuelto mi duda sobre el uso de \label
y \ref
. Leyendo los comentarios, he notado que en muchas ocasiones no es necesario utilizar el sistema de referenciación, sólo los valores asociados a estos.
Mi idea es no utilizar archivos auxiliares (necesito muchos archivos en algunos casos, el modo de agregar en .aux
el archivo no existe en TeX), guardar en la memoria una lista de respuestas breves a los ejercicios, de modo que al copiar y pegar de un archivo a otro no sea necesario realizar grandes tareas. modificaciones (cambiar solo una referencia a la lista).
Este es el archivo (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}
Ahora viene mi problema, la macro que tomo deAlmacenar una serie de cadenas en un comandosolo funciona en modo local, si lo escribo dentro de un entorno solo existe en este y no fuera de él, es decir, no podré acceder fuera del entorno. Por ejemplo:
% 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}
Y mi idea es que funcione dentro de entornos de enumeración y luego acceda a valores fuera de ellos. Lo he probado con esto:
% !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}
También intente agregar una clave para enumitem
evitar escribir un nuevo entorno.
\SetEnumitemKey{reflist}{before=\storedata{#1}{}}%
Pero ninguna de las opciones que probé funcionó, así que aquí viene la gran pregunta: ¿Cómo las modifico \appendata|\storedata
para que funcionen globalmente?
\appendata
sería como \label
y \getdata
sería como \ref
, por supuesto solo con valores y sin .aux
archivo (o dos ejecuciones).
Respuesta1
Para almacenar los datos globalmente, \seq_gput_right:Nn
(y sus variantes) se debe aplicar, de lo contrario el ahorro local se pierde finalmente una vez que enumerate
finaliza el grupo formado por el entorno.
\seq_put_right:Nn
sin embargo, sólo funciona dentro de un 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}