再次编译可消除编译错误

再次编译可消除编译错误

后续行动这个答案,第一次运行编译会出现以下错误:

未定义控制序列。此考试包含 \textbf{\NAMEquestions}

必须通过第二次运行才能消除此错误。那么,如何才能消除第一次运行的错误,以便能够使用arara

% arara: lualatex: { options: [ '-synctex=1', '-shell-escape','-interaction=nonstopmode' ]}
% arara: lualatex: { options: [ '-synctex=1', '-shell-escape','-interaction=nonstopmode' ]}
\documentclass[addpoints]{exam}

\usepackage{fmtcount,etoolbox}

\makeatletter
\let\NAMEquestions\numquestions
\patchcmd{\NAMEquestions}{\exam@numquestions}{{\NUMBERstringnum\exam@numquestions} question\ifnum\exam@numquestions>1 s\fi}{}{}
\let\NAMEpages\numpages
\patchcmd{\NAMEpages}{\exam@lastpage}{{\NUMBERstringnum\exam@lastpage} page\ifnum\exam@lastpage>1 s\fi}{}{}
\let\NAMEpoints\numpoints
\patchcmd{\NAMEpoints}{\exam@numpoints}{{\NUMBERstringnum\exam@numpoints} mark\ifnum\exam@numpoints>1 s\fi}{}{}
\makeatother

\begin{document}

This exam contains \textbf{\NAMEquestions} in \textbf{\NAMEpages} (including the cover) for a total of \textbf{\NAMEpoints}.

\begin{questions}
  \question[10] single question
\end{questions}

\end{document}

答案1

该类exam会采取预防措施来检查命令是否已定义,但您的代码却没有,因为括号的位置不正确。

\documentclass[addpoints]{exam}

\usepackage{fmtcount,etoolbox}

\makeatletter
\newcommand{\diaa@plural}[1]{\ifnum#1>1 s\fi}
\let\NAMEquestions\numquestions
\patchcmd{\NAMEquestions}
  {\exam@numquestions}
  {{\NUMBERstringnum\exam@numquestions\ question\diaa@plural\exam@numquestions}}
  {}{}
\let\NAMEpages\numpages
\patchcmd{\NAMEpages}
  {\exam@lastpage}
  {{\NUMBERstringnum\exam@lastpage\ page\diaa@plural\exam@lastpage}}
  {}{}
\let\NAMEpoints\numpoints
\patchcmd{\NAMEpoints}
  {\exam@numpoints}
  {{\NUMBERstringnum\exam@numpoints\ mark\diaa@plural\exam@numpoints}}
  {}{}
\makeatother

\begin{document}

This exam contains \textbf{\NAMEquestions} in \textbf{\NAMEpages} (including the cover) for a total of \textbf{\NAMEpoints}.

\begin{questions}
  \question[10] single question
\end{questions}

\end{document}

的定义\numquestions

% exam.cls, line 1604:
\def\numquestions{\@ifundefined{exam@numquestions}%
  {\mbox{\normalfont\bfseries ??}}%
  \exam@numquestions
}% numquestions

并且 token\exam@newquestions是 的第三个参数\@ifundefined。使用你的补丁,只有\NUMBERstringnum\exam@numquestions被当作第三个参数,并且question无论如何都会执行代码。

答案2

我对 arara 一无所知(因为我不是一只鸭子)但这可以避免错误。

\documentclass[addpoints]{exam}

\usepackage{fmtcount,etoolbox}

\makeatletter
\ifcsname exam@numquestions\endcsname%
\let\NAMEquestions\numquestions
\patchcmd{\NAMEquestions}{\exam@numquestions}{{\NUMBERstringnum\exam@numquestions} question\ifnum\exam@numquestions>1 s\fi}{}{}
\fi
\ifcsname exam@lastpage\endcsname%
\let\NAMEpages\numpages
\patchcmd{\NAMEpages}{\exam@lastpage}{{\NUMBERstringnum\exam@lastpage} page\ifnum\exam@lastpage>1 s\fi}{}{}
\let\NAMEpoints\numpoints
\fi
\ifcsname exam@numpoints\endcsname%
\patchcmd{\NAMEpoints}{\exam@numpoints}{{\NUMBERstringnum\exam@numpoints} mark\ifnum\exam@numpoints>1 s\fi}{}{}
\fi%
\def\safetouse#1{%
\ifcsname#1\endcsname%
\csname#1\endcsname%
\fi}
\makeatother

\begin{document}

This exam contains \textbf{\safetouse{NAMEquestions}} in
\textbf{\safetouse{NAMEpages}} (including the cover) for a total of
\textbf{\safetouse{NAMEpoints}}.

\begin{questions}
  \question[10] single question
\end{questions}

\end{document}

相关内容