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}
\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.