这是一个最小的例子。
\documentclass[a4paper, addpoints]{exam}
\usepackage{etoolbox}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the accumulated answers text
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\answertext}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% hook to fillin command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\renewcommand\fillin[1][{}]{%
% copy the contents of fillin definition from the exam class
% we try to append the content to the \answertext string
\xappto\answertext{\thequestion,#1\space}%
\def\fillin@ans{#1}%
\fillin@relay
}
\makeatother
\begin{document}
\section{Questions}
\begin{questions}
\question First Quesiton \fillin[Answers1] is the color of my true love’s hair.
\question Second Question \fillin[Answers2] is the \fillin[Answers3].
\question Third Question \fillin[Answers4] is OK.
\end{questions}
\section{Answers}
\ifprintanswers
% do nothing
\else
% this print the answertext
\answertext
\fi
\end{document}
我必须挂接到\fillin
命令来累积答案,并将它们放入全局变量中\answertext
。
但结果如下所示:
我希望看到如下内容:
1,Answers1 2,Answers2;Answers3 3,Answers4
我的意思是它应该只显示一次问题号“2”。因为一个问题可能有很多填空答案。另外,我需要一种方法来计算总的填空数。(也就是说,一个填空位置有 2 分,例如)
答案1
我添加了新的计数器fillno
和lastquesno
,用于计算填空的数量,并测试当前填空是否属于包含前一个填空的同一问题。我还更改了您对 的新\fillin
定义
\makeatletter
\renewcommand\fillin[1][{}]{%
% copy the contents of fillin definition from the exam class
% we try to append the content to the \answertext string
%\xappto\answertext{\thequestion,#1\space}%
\appendanswertext{\thequestion}{#1}%
\setcounter{lastquesno}{\value{question}}%
\addtocounter{fillno}{1}%
\def\fillin@ans{#1}%
\fillin@relay
}
\makeatother
\newcommand{\appendanswertext}[2]{%
% #1 is \thequestion, #2 is the answer
\ifnum \value{lastquesno} = #1\relax
% It's another fillin for the same question:
\xappto\answertext{;#2}%
\else
% First fillin for this question:
\xappto\answertext{\space #1,#2}
\fi
}
There were \thefillno\ fillins.
我还在您调用 后立即添加了该行
\answertext
。现在它似乎可以执行您的请求。