如何引用新计数器中的最后一个数字

如何引用新计数器中的最后一个数字

下面我想引用最后一个数字。提前谢谢

 \documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \ans=\expandafter{\the\ans #2}%
      \advance\i by 1%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
   \newcount\i%
   \i=0%
   \newtoks\ans%
   \loop%
      \advance\i by 1%
      \ans=\expandafter{\the\ans #2}%
      \ifnum\i<#1\repeat%
   \the\ans%
}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[1em]{\textcolor{white}{#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1.2em]{#2}}}
\newcounter{problem}
\newcounter{choice}
\newcommand{\generateQs}[2][0]{%
  \countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
    \numberbox{\theproblem}}%
    \setcounter{choice}{0}%
    \repeatntimes{\numexpr\NumberOfChoices-1}{\stepcounter{choice}%
      \if\Alph{choice}#1\relax%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
      \else%
        \savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
      \fi%
    }\Prob\kern7pt%
  }%
}
\def\NumberOfChoices{4}
\newcommand\answergrid[2]{\renewcommand\NumberOfChoices{#1}\makegrid #2;\relax\relax}
\def\makegrid#1#2#3\relax{%
  \generateQs[#1]{1}\ifx\relax#3\else\makegrid#3\relax\fi%
}
\begin{document}

The exam contains (ref the last number) MCQs 

\answergrid{5}{B;0;0;A;C;0;0;0;0;0;D;0;B;E}

\end{document}

答案1

类似地,LastPage你可以在文档的序言中添加

\makeatletter
\def\lastproblem@putlabel{%
   \immediate\write\@auxout{%
     \string\newlabel{LastProblem}{{\@arabic\c@problem}{}{}{}{}}}}
\AtEndDocument{\lastproblem@putlabel}
\makeatother

进而

...
The exam contains \ref{LastProblem} problems.
...

在文档正文中。

相关内容