使用宏作为totcount计数器名称

使用宏作为totcount计数器名称

在环境的顶层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}

相关内容