Hallo zusammen, nachdem meine Zweifel bezüglich der Verwendung von \label
und ausgeräumt wurden \ref
. Beim Lesen der Kommentare ist mir aufgefallen, dass in vielen Fällen nicht das Referenzsystem verwendet werden muss, sondern nur die damit verbundenen Werte.
Meine Idee ist, keine Zusatzdateien zu verwenden (ich brauche viele Dateien und in manchen Fällen .aux
gibt es in TeX keinen Anfügemodus in der Datei), sondern eine Liste mit Kurzantworten auf die Übungen im Speicher zu speichern, damit beim Kopieren und Einfügen von einer Datei in eine andere keine größeren Änderungen vorgenommen werden müssen (nur einen Verweis auf die Liste ändern).
Dies ist die Datei (die funktioniert)
% !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}
Jetzt kommt mein Problem, das Makro, das ich nehme,Speichern eines Arrays von Zeichenfolgen in einem Befehlfunktioniert nur im lokalen Modus. Wenn Sie es in einer Umgebung schreiben, existiert es nur in dieser und nicht außerhalb davon. Das heißt, ich kann außerhalb der Umgebung nicht darauf zugreifen. Beispiel:
% 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}
Und meine Idee ist, dass es in Enumerate-Umgebungen funktioniert und dann auf Werte außerhalb dieser Umgebungen zugreift. Ich habe es hiermit versucht:
% !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}
Versuchen Sie auch, einen Schlüssel hinzuzufügen, um enumitem
das Schreiben einer neuen Umgebung zu vermeiden
\SetEnumitemKey{reflist}{before=\storedata{#1}{}}%
Aber keine der Optionen, die ich ausprobiert habe, hat funktioniert, daher stellt sich hier die große Frage: Wie kann ich sie ändern, \appendata|\storedata
damit sie global funktionieren?
\appendata
wäre wie \label
und \getdata
wäre wie \ref
, natürlich nur mit Werten und ohne .aux
Datei (oder zwei Ausführungen).
Antwort1
Um die Daten global zu speichern, \seq_gput_right:Nn
muss (und deren Varianten) angewendet werden, da sonst die lokale Speicherung nach dem enumerate
Ende der durch die Umgebung gebildeten Gruppe endgültig verloren geht.
\seq_put_right:Nn
funktioniert jedoch nur innerhalb einer Gruppe.
\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}