在以下MWE中
\documentclass{article}
\usepackage{totcount}
\edef\name{}
\edef\create#1{\noexpand\newtotcounter{t#1}}
\def\setname#1{\edef\name{#1}}
\begin{document}
\setname{est} \create{\name}
\setname{estnew} \create{\name}
Total counts: \total{test}, \total{testnew}
\addtocounter{test}{10}
The test: \thetest
\addtocounter{testnew}{5}
The new test: \thetestnew
\end{document}
我想创建其名称部分由我的命令\name
确定的计数器。这很有效,因为可以正确创建计数器。但是,totcount
仅适用于testnew
,不适用于test
。换句话说,输出为:
Total counts: ??, 5
The test: 10
The new test: 5
检查aux文件,似乎只设置了testnew
。实际上,我有两行相同的
\expandafter\ifx\csname c@testnew@totc\endcsname\relax\newcounter{testnew@totc}\fi\setcounter{testnew@totc}{5}
很明显,我的扩展存在问题,但我不确定如何解决。
我根据答案here找到了一个解决方案。我的印象是edef
扩展了(现在显然是)错误的参数。因此,\create
命令必须使用edef
定义新命令,以便扩展新命令。
\edef\create#1{%
\begingroup\edef\tmp{\endgroup\noexpand\newtotcounter{t#1}}
\tmp}