将考试类中的计数器设置为基于第一组问题的计数

将考试类中的计数器设置为基于第一组问题的计数
\documentclass{exam}

\usepackage{totcount}

\newtotcounter{s}
\setcounter{s}{0}
\newtotcounter{e}
\setcounter{e}{0}
\newcommand{\squestion}[0]{\stepcounter{s}\question}
\newcommand{\equestion}[0]{\stepcounter{e}\question}


\begin{document}

\fullwidth{\Large \textbf{Short questions}}    
There are \total{s}\ short questions. %display the number of short questions.

\begin{questions}
\squestion
This is the first short question.
\squestion
This is the second short question.
\end{questions}


\fullwidth{\Large \textbf{Essay questions}}
There are \total{e}\ essay questions. %display the number of essay questions.

\begin{questions}
\setcounter{question}{\total{s}}  % Set the counter to start with 3
\equestion
This is the first essay question.
\equestion
This is the second essay question.
\equestion
This is the third essay question.
\equestion
This is the fourth essay question.
\end{questions}

For the record there is a total of \numquestions\ questions.

\end{document}

我想知道为什么这行代码\setcounter{question}{\total{s}}不起作用。为什么\total{s}不替换为2

答案1

由于\total{foo}将在第一次编译运行时打印??,因此输出\total{foo}不能输入到\setcounter语句中。

但是,可以使用\value{foo@totc}包装器命令中的或来提取真实计数器\extractcntrvalue值。编译中的第一次调用将提供错误的值,但这不是真正的问题,因为在大多数情况下,文档LaTeX必须至少运行两次,下一次调用将提供正确的计数器值并将其存储到question等。

\documentclass{exam}

\usepackage{totcount}

\makeatletter
\newcommand{\extractcntrvalue}[1]{%
  \value{#1@totc}%
}
\makeatother



\newtotcounter{s}
\setcounter{s}{0}
\newtotcounter{e}
\setcounter{e}{0}
\newcommand{\squestion}[0]{\stepcounter{s}\question}
\newcommand{\equestion}[0]{\stepcounter{e}\question}





\begin{document}
\fullwidth{\Large \textbf{Short questions}}    
There are \total{s}\ short questions. %display the number of short questions.

\begin{questions}
\squestion
This is the first short question.
\squestion
This is the second short question.
\end{questions}


\fullwidth{\Large \textbf{Essay questions}}
There are \total{e}\ essay questions. %display the number of essay questions.

\begin{questions}
  \setcounter{question}{\extractcntrvalue{s}}  % Set the counter to start with 3
  \equestion
This is the first essay question.
\equestion
This is the second essay question.
\equestion
This is the third essay question.
\equestion
This is the fourth essay question.
\end{questions}

For the record there is a total of \numquestions\ questions.

\end{document}

相关内容