我正在尝试创建一个成绩表。这是创建变量数组。问题是评分表位于问题下方。我想要的方式是问题应该出现在评分表之后。但是,如果评分表位于顶部,我将不会调用任何csdef
命令,也不会有任何输出。(我试过了,当然这是真的。)那么,我怎样才能将评分表放在实际问题之前。
ps 我已经尝试过totcount
包并用csdef
s替换\newtotcounter
,但它会出现大量错误,而我对此一无所知。
我的 MWE 在这里:
\documentclass{article}
\usepackage{etoolbox}
\newcounter{cnt}
\newcommand\addtext[1]{%
\stepcounter{cnt}%
\csdef{text\thecnt}{#1}}
\newcommand\gettext[1]{%
\csuse{text#1}}
\begin{document}
Question 1: Blah.
\addtext{3}
Question 2: Blah.
\addtext{2}
Question 3: Blah.
\addtext{4}
\begin{tabular}{ |l|c@{} *{\thecnt}{c|} } % the first row is hidden
\hline
\newcounter{colnum}%
\setcounter{colnum}{0}%
Q. No.:\hfill &
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\thecolnum}\\
\hline
Score: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\hspace{1cm}}\\
\hline
Total: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\gettext{\thecolnum}}\\
\hline
\end{tabular}
\end{document}
答案1
您可以滥用\label
-\ref
机制:\addtext
宏定义\@currentlabel
为练习的等级并发出将在下次运行时检索的\label
命令\gettext
。在结束文档中,我们还存储了练习的最终数量。为了避免文件.aux
尚未包含信息时出现问题,我们将设置\finalnumber
为 2(如果\getrefnumber{lastcnt}
返回 0)。
\documentclass{article}
\usepackage{etoolbox,refcount}
\newcounter{cnt}
\AtEndDocument{\refstepcounter{cnt}\label{lastcnt}}
\makeatletter
\newcommand\addtext[1]{%
\stepcounter{cnt}%
\def\@currentlabel{#1}%
\label{text\thecnt}}
\makeatother
\newcommand\gettext[1]{\getrefnumber{text#1}}
\newcounter{colnum}
\begin{document}
\ifnum\getrefnumber{lastcnt}=0
\def\finalnumber{2}%
\else
\edef\finalnumber{\number\numexpr\getrefnumber{lastcnt}-1\relax}%
\fi
\begin{tabular}{ |l|c@{} *{\finalnumber}{c|} } % the first row is hidden
\hline
\setcounter{colnum}{0}%
Q. No.:\hfill &
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\thecolnum}\\
\hline
Score: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\hspace{1cm}}\\
\hline
Total: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\gettext{\thecolnum}}\\
\hline
\end{tabular}
Question 1: Blah.
\addtext{3}
Question 2: Blah.
\addtext{2}
Question 3: Blah.
\addtext{4}
\end{document}