我在这个网站上看到过一些问题,要求解决此问题;我自己也遇到过这种情况。我的代码只能将答案打印为与问题编号相同的列表;它不会将它们放在任何特定格式上,也不会制作评分表。我修改了来自这个答案。
答案1
要点提到的答案是将所有答案收集到一个垂直框中,然后在后期转储它们,同时使用环境answer
来执行此操作(在我看来,这是一个相当优雅的解决方案)。我的补充是:
- 打印正确答案所获得的分数。
- 自动在每个答案上打印问题编号。
- 打印多项选择题的正确答案。
- 自动将答案转储到有标题的新页面上。
下面是我对其中每个部分工作原理的理解以及注释的代码(如果有任何错误,请纠正):
\documentclass[addpoints]{exam}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%CODE TO PRINT ANSWERS ON A SEPARATE PAGE %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\answers{Answer key}%DEFINE THE NAME OF THE ANSWER PAGE.
%%%%% SET THE BOX WHERE THE ANSWERS WILL BE STORED
\newbox\allanswers
\setbox\allanswers=\vbox{}
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%ENVIRONMENT FOR ANSWERS
\newenvironment{answer}
{%
\global\setbox\allanswers=\vbox\bgroup%BEGINS A VERTICAL BOX (1)
\unvbox \allanswers\par%%%RECURSIVELY ADD NEW ANSWERS TO THE \vbox.
\hspace{2em}\makebox[1pt][r]{\thequestion.\ }%%%%%%THIS PRINTS THE CURRENT QUESTION NUMBER AFTER ALL THE PREVIOUS QUESTOINS HAVE BEEN ADDED TO THE BOX; THE FIRST \mbox IS RIGHT-ALIGNED, SO IT PRINTS THE NUMBER ON THE CORRECT COLUMN. IF ABSENT, THE 1 OF A 13 WILL BE ON THE SAME COLUMN AS A 9.
\mbox\bgroup%PUTS EVERYTHING INSIDE AN UNBREAKABLE BOX, FOR QUESTIONS ARRANGED ON COLUMNS, BECAUSE THE ANSWERS WILL BE THEN PRINTED THE SAME WAY; \bgroup BEGINS SUCH A BOX...(*)
}%
{%
(\pointsofquestion{\value{question}} \points)%PRINTS THE POINTS OBTAINED, IN PARENTHESES; USEFUL WHEN GRADING WITH ONLY THE ANSWER KEY AT HAND.
\egroup%(*)...\egroup ENDS IT.
\medbreak%A BIT MORE OF SPACE BETWEEN ANSWER, SO YOU CAN USE \displaystyle IF NEEDED.
\egroup%ENDS THE VERTICAL BOX (1)
}
%%%%%%%%% /ANSWERS
%%%%%%%%%%%%PRINT ALL ANSWERS ON A SEPARATE PAGE
\newcommand{\showallanswers}{%
\par\pagebreak%NEW PAGE FOR OUR ANSWERS
\pagestyle{empty}%
\makebox[\linewidth][c]{\hfill{\Large\bfseries\answers}\hfill}\par%CENTRED BOLDFACE TITLE FOR THE ANSWER KEY.
\null\par%EXTRA SPACE FOR THE TITLE, OTHERWISE IT LOOKS VERY CRAMMED.
\unvbox\allanswers}
%%%%%%%%%%%%%%%%%%%%%%/PRINTANSWERS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% TO-DO:
%1. Let the answer environment decide whether to print points; not print them if the question has no points.
%2. Use only the command \CorrectChoice to print the correct answer in a multiple-choice question, without the '\setcounter{correct}{\value{choice}}' line.
%DO ALL OF THE ABOVE REDEFINING THE solution ENVIRONMENT ALREADY DEFINED BY THE exam CLASS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{questions}
\pagestyle{empty}
\newcounter{correct} %COUNTER TO STORE THE VALUE OF A CORRECT CHOICE.
\question[1] Did John Doe sign the Magna Carta?
\begin{answer}
He was busy baking bread, so he couldn't make it.
\end{answer}
\question[3] Who's Hamlet's true friend?
\begin{answer}
None, according to himself.
\end{answer}
\question[6] When was the Necronomicon written?
\begin{answer}
In the Age of Madness.
\end{answer}
\question[15] How many modes are there to skin a cat?
\begin{choices}
\choice 1
\CorrectChoice\setcounter{correct}{\value{choice}} 20
\choice 4
\end{choices}
\begin{answer}
\Alph{correct}
\end{answer}
\question[4] What is \textbf{the} answer?
\begin{choices}
\CorrectChoice\setcounter{correct}{\value{choice}} 42.
\choice 62.
\choice 12.
\choice 2.
\end{choices}
\begin{answer}
\Alph{correct}
\end{answer}
\end{questions}
\showallanswers
\end{document}
正如代码中所注释的,我想添加以下功能:
- 让答案环境决定是否打印分数;如果问题没有分数,则不打印。我想这需要一个
\ifpoints
命令或类似的东西;我尝试阅读该课程的源代码exam
,但我完全迷失了。 - 仅使用命令 \CorrectChoice 即可打印多项选择题中的正确答案,不带线条
\setcounter{correct}{\value{choice}}
。 - 执行上述所有操作,重新定义类
solution
中已经定义的环境exam
。我试过了,但错误太多,而且在阅读类的代码时,我又一次迷失了方向。