在環境的頂層document
,定義的巨集(使用\def
)可以擴展為計數器的名稱totcount
。由於某種原因,這在環境定義中不起作用。我嘗試了各種組合\newcommand
,\expandafter
等等\edef
,但沒有一個起作用。
\documentclass{memoir}
\usepackage{totcount}
\newenvironment{passagequestions}{% This does not work
\def\counterIDX{passagecounterX}%
\newtotcounter{c\counterIDX}%
\total{c\counterIDX}%
}{%
}
\begin{document}
\def\counterID{passagecounter}
\newtotcounter{c\counterID}% This works
\total{c\counterID}
\begin{passagequestions}
this doesn't work
\end{passagequestions}
\end{document}
答案1
當您在環境中執行操作時,您需要\counterIDX
使用以下命令來「全域」定義\gdef
:
\documentclass{memoir}
\usepackage{totcount}
\newenvironment{passagequestions}{% This does not work
\gdef\counterIDX{passagecounterX}%
\newtotcounter{c\counterIDX}%
\total{c\counterIDX}%
}{%
}
\begin{document}
\def\counterID{passagecounter}
\newtotcounter{c\counterID}% This works
\total{c\counterID}
\begin{passagequestions}
this doesn't work
\end{passagequestions}
\end{document}