![使用列表创建假标签引用](https://linux22.com/image/353072/%E4%BD%BF%E7%94%A8%E5%88%97%E8%A1%A8%E5%88%9B%E5%BB%BA%E5%81%87%E6%A0%87%E7%AD%BE%E5%BC%95%E7%94%A8.png)
大家好,我再次解决了关于\label
和使用的疑问\ref
。阅读评论后,我注意到在很多情况下不需要使用引用系统,只需要使用与这些相关的值。
我的想法是不使用辅助文件(在某些情况下需要许多文件,.aux
TeX 中不存在文件中的附加模式),在内存中保存练习的简短答案列表,因此从一个文件复制并粘贴到另一个文件时不需要进行重大修改(仅更改对列表的引用)。
这是文件(有效)
% !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}
现在我的问题来了,我从在命令中存储字符串数组仅在本地模式下有效,如果在环境中写入它,它仅存在于此环境中,而不存在于环境之外,也就是说,我将无法在环境之外访问它。例如:
% 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}
我的想法是,它在枚举环境中工作,然后访问其外部的值。我曾尝试过:
% !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}
还可以尝试添加一个键以enumitem
避免写入新的环境
\SetEnumitemKey{reflist}{before=\storedata{#1}{}}%
但是我尝试的所有选项都没有奏效,所以,这里出现了一个大问题:我该如何修改\appendata|\storedata
才能使它们在全球范围内发挥作用?
\appendata
将会像\label
并且\getdata
将会像一样\ref
,当然只有值和没有.aux
文件(或两次运行)。
答案1
为了全局存储数据,\seq_gput_right:Nn
必须应用(及其变体),否则在enumerate
环境形成的组结束后,本地保存最终会丢失。
\seq_put_right:Nn
但仅在群组内部有效。
\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}