0

I use the exam class to write multiple choice tests in a couple of my classes. I use the following code to collect and then display the correct answers at the end of the test using the command \showallanswers.

%%%%%start code for printing answers
\CorrectChoiceEmphasis{} %% removes question answers being boldface on test 
\newbox\allanswers
\setbox\allanswers=\vbox{}
\newenvironment{answer}
{%
 \global\setbox\allanswers=\vbox\bgroup %
  \unvbox\allanswers%
    \thequestion. \thechoice\\ 
}%
{%
 \egroup%
}
\newcommand{\CC}{\CorrectChoice  \leavevmode\begin{answer}\end{answer}}% New command \CC replaces \correctchoice from exam.sty and saves answers in the box \allanswers .
\newcommand{\showallanswers}{%
\ifprintanswers \centering Here are the answers: \par \usebox\allanswers \fi}
%%%%%end code for printing answers

This works perfectly as long as the test contains about 35 questions or less. If the test contains more questions, then the answer key gets cut off and all answers above 35 are not displayed. I suspect this is because a vbox is used to display the answers and a single page does not have enough lines. I'd greatly appreciate a little help with amending the above code to somehow accommodate a longer test. Is there way to have the answer key use two columns, if necessary? I tried to edit the code and apply the multicols package, but my macro making skills are not great.

Thank you very much in advance.

Here's a more complete MWE that will allow the entire document to compile:

\documentclass[addpoints,answers]{exam}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage{multicol}
\footer{}{Page \thepage}{}
\pagestyle{headandfoot}
\title{Exam Title}
\header{Course}{Test 1 --- Form A}{Spring 2023}
%\footer{Page \thepage\ of \numpages}
%{}
%{Points earned: \makebox[.5in]{\hrulefill}\\ 
%out of a possible \pointsonpage{\thepage} points}

%%%%%start code for printing answers
\CorrectChoiceEmphasis{} %% removes question answers being boldface on test 
\newbox\allanswers
\setbox\allanswers=\vbox{}
\newenvironment{answer}
{%
 \global\setbox\allanswers=\vbox\bgroup %
  \unvbox\allanswers%
    \thequestion. \thechoice\\ 
}%
{%
 \egroup%
}
\newcommand{\CC}{\CorrectChoice  \leavevmode\begin{answer}\end{answer}}% New command \CC replaces \correctchoice from exam.sty and saves answers in the box \allanswers .
\newcommand{\showallanswers}{%
\ifprintanswers \centering Here are the answers: \par \usebox\allanswers \fi}
%%%%%end code for printing answers

\begin{document}
\begin{center}

\fbox{\fbox{\parbox{5.5in}{\centering
 INSERT EXAM INSTRUCTIONS HERE.}}}
\end{center}
\vspace{0.1in}
\makebox[\textwidth]{Name:\enspace\hrulefill} \\ 
 
\begin{questions}
     \question[2] What is the answer?  
     \begin{choices}
     \choice Option 1
     \choice Option 2
     \CC Option 3
     \choice Option 4
     \end{choices}


\end{questions}
\newpage
\showallanswers
\end{document}

5
  • Please what is your minimal complete code that we can compile on our pc?
    – Sebastiano
    Jan 25 at 19:32
  • @twothirtyeight I see you have a counter for the questions \thequestion. So the obvious way to proceed would be to use that counter in a condition macro inside your \ifprintanswers. When \thequestion exceeds a certain value, a command like \clearpage or \switchcolumn should be issued after which the printing of the answers can continue. Unfortunately, that is as far as I can suggest. Writing macro's isn't my forte either, but maybe someone can put this idea into code.
    – alchemist
    Jan 25 at 19:40
  • 1
    @Sebastiano I edited the original post to include a complete MWE to allow folks to compile a demo document. Thank you for your help. Jan 26 at 0:35
  • @twothirtyeight You are welcome. :-)
    – Sebastiano
    Jan 26 at 9:09
  • The code to gather and display answers was found on this post: tex.stackexchange.com/questions/238153/… Jan 26 at 17:56

1 Answer 1

0

I was able to to find a solution by viewing the post

Multiple Choice Answer Key in exam package at the end of document

The line

\ifprintanswers \centering Here are the answers: \par \usebox\allanswers \fi}

was tweaked to read

\ifprintanswers \centering Here are the answers: \par \unvbox\allanswers \fi}

and then the list of answers was allowed to break over multiple pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.