我想保存一个项目列表及其计数器。这是我在 latex 中写的内容
\documentclass{article}
\RequirePackage{etoolbox} % defines lists and their operations
\RequirePackage{tabulary} % defines content-based sizable tables
\newcounter{vpt}
\setcounter{vpt}{0}
\newcommand{\facility}[1]{\addtocounter{vpt}{1} \listcsgadd{FacilityList}{#1}
\arabic{vpt}. $-->$ #1 }
\newcommand{\facilityno}[1]{\listcsgadd{FacilityNo}{#1} \arabic{vpt}. $-->$ #1 }
\begin{document}
I want to save the text and the counter\\
\facility{this is facility one} \facilityno{\the\value{vpt}} \\
\facility{this is facility two} \facilityno{\the\value{vpt}}\\
\facility{this is facility three}\facilityno{\the\value{vpt}}
the texts are saved
\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\dolistcsloop{FacilityList}
\end{itemize}
% But the counters are all 3??!!!
\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\dolistcsloop{FacilityNo}
\end{itemize}
% any comments?
\end{document}
答案1
快速回答:您必须扩展计数器的值\facilityno
。我在输入过程中插入了一些\expandafter
s,但我想这不是理想的语法。
如果不扩展参数,则\the\value{vpt}
使用列表元素时会扩展列表元素,此时计数器状态为 3。
\documentclass{article}
\RequirePackage{etoolbox} % defines lists and their operations
\RequirePackage{tabulary} % defines content-based sizable tables
\newcounter{vpt}
\setcounter{vpt}{0}
\newcommand{\facility}[1]{\addtocounter{vpt}{1} \listcsgadd{FacilityList}{#1}
\arabic{vpt}. $-->$ #1 }
\newcommand{\facilityno}[1]{\listcsgadd{FacilityNo}{#1} \arabic{vpt}. $-->$ #1}
\begin{document}
I want to save the text and the counter\\
\facility{this is facility one} \expandafter\facilityno\expandafter{\the\value{vpt}}\\
\facility{this is facility two} \expandafter\facilityno\expandafter{\the\value{vpt}}\\
\facility{this is facility three}\expandafter\facilityno\expandafter{\the\value{vpt}}
the texts are saved
\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\dolistcsloop{FacilityList}
\end{itemize}
% But the counters are all 3??!!!
\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\dolistcsloop{FacilityNo}
\end{itemize}
% any comments?
\end{document}