I've seen some questions asking for solutions to this problem in this site; I've had it myself. My code only works for printing the answer as a list with the same numbering as the questions; it doesn't put them on a any particular format nor it makes the grading table. I modified the code from this answer.
1 Answer
The gist of the mentioned answer is to collect all the answers in a vertical box to dump them at a latter stage, while using an answer
environment to do it (quite an elegant solution, in my opinion). My additions are:
- Printing the points obtained for the correct answer.
- Automatically printing the question number on each answer.
- Printing the correct answer for a multiple-choice question.
- Dumping the answers on a titled new page automatically.
Here's the code with comments of my understanding of how each thing there works (please correct it if I got anything wrong):
\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}
As commented in the code, I would like to add the following features:
- Let the answer environment decide whether to print points; not print them if the question has no points. I suppose this would require a
\ifpoints
command or something similar; I tried reading the source for theexam
class, but I was quite lost. - 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 theexam
class. I tried it, but got too many errors and, again, got quite lost at reading the code for the class.