我正在尝试收集令牌列表中的某些计数器值。这是我的天真尝试:
\documentclass{article}
\newtoks\list
\list={}
\def\addtolist#1{\global\list=\expandafter{\the\list #1}}
\newcounter{counter}
\def\countertolist{\expandafter\addtolist{\arabic{counter}}}
\begin{document}
\stepcounter{counter}
\stepcounter{counter}
\countertolist
\stepcounter{counter}
\stepcounter{counter}
\countertolist
\the\list
\end{document}
我希望结果是 24,但结果是 44。似乎 的定义\countertolist
不够\arabic{counter}
“扩展”(即\countertolist
使用 时没有扩展)。正确的定义是什么?
答案1
主要原因是\arabic{counter}
扩展不够,可以改为\expandafter\addtolist\expandafter{\the\c@counter}
强制扩展。
\documentclass{article}
\newtoks\list
\list={}
\def\addtolist#1{\global\list=\expandafter{\the\list #1}}
\newcounter{counter}
\makeatletter
\def\countertolist{\expandafter\addtolist\expandafter{\the\c@counter}}
\makeatother
\begin{document}
\stepcounter{counter}
\stepcounter{counter}
\countertolist
\stepcounter{counter}
\stepcounter{counter}
\countertolist
\the\list
\end{document}