exam – How to Append Answer Sheet to Exam Document Class in LaTeX

exam

I want to append an answer sheet to an exam. However, I only managed to do so manually, which causes conflicts if I reference things (with \cref for instance). Also, if have to re-adjust the points in the answer sheet every time I change them in the questions. Is there a proper way of doing this?

\documentclass[addpoints, 12pt]{exam}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[nameinlink]{cleveref}

\pagestyle{headandfoot}
\firstpageheader{\large\bfseries Astronomy Exam\\ \bigskip
\large\bfseries Last Name:\enspace\makebox[1.9in]{\hrulefill}}
{}
{\large\bfseries Univ ID: \makebox[2in]{\hrulefill} \\  \bigskip
 \large\bfseries First Name:\enspace\makebox[2in]{\hrulefill}}
\runningheader{\large\bfseries Last Name:\enspace\makebox[1.9in]{\hrulefill}}
{}
{\large\bfseries First Name:\enspace\makebox[2in]{\hrulefill}}
\firstpagefooter{}{}{}
\runningfooter{}{\thepage}{}

\begin{document}

\begin{center}
\fbox{\fbox{\parbox{5.5in}{\centering
Answer the questions in the spaces provided on the
answer sheets.}}}
\end{center}

\begin{questions}

\question[60] Explain Principle A

\question[40] Explain Principle B

\end{questions}

\newpage

\begin{center}
\fbox{\fbox{\parbox{5.5in}{\centering
Answer sheet}}}
\end{center}
\bigskip

\begin{questions}

\question[60] Answer:

\vspace{\stretch{1}}

\question[40] Answer:

\vspace{\stretch{1}}


\end{questions}

\end{document}

Best Answer

Hi I modified the answer from How to get LaTeX to expand at the correct position? to create a new environment for my answer booklet.

Here's the solution using your MWE

\documentclass[addpoints, 12pt]{exam}

\pagestyle{headandfoot}
\firstpageheader{\large\bfseries Astronomy Exam\\ \bigskip
\large\bfseries Last Name:\enspace\makebox[1.9in]{\hrulefill}}
{}
{\large\bfseries Univ ID: \makebox[2in]{\hrulefill} \\  \bigskip
 \large\bfseries First Name:\enspace\makebox[2in]{\hrulefill}}
\runningheader{\large\bfseries Last Name:\enspace\makebox[1.9in]{\hrulefill}}
{}
{\large\bfseries First Name:\enspace\makebox[2in]{\hrulefill}}
\firstpagefooter{}{}{}
\runningfooter{}{\thepage}{}

\usepackage{verbatim}

\newenvironment{ex@skripts}[1]{#1}{}

\makeatletter
\newwrite\verbatim@outSkr % Define file. 
\immediate\openout\verbatim@outSkr=\jobname.skr  % Open file for writing. 

\def\skript{
\@bsphack
\let\do\@makeother\dospecials
\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\verbatim@outSkr{\the\verbatim@line}}%
\immediate\write\verbatim@outSkr{\string\begin{ex@skripts}{\expandafter\thequestion. (\expandafter\totalpoints \ points) Answer:\string\par}}
\verbatim@start}

\def\endskript{%
\immediate\write\verbatim@outSkr{\string\end{ex@skripts}}
\@esphack}

\newcommand*{\includeSkripts}{%
\immediate\closeout\verbatim@outSkr    % Close file. 
\InputIfFileExists{\jobname.skr}{}{}
\newwrite\verbatim@outSkr % Datei wird definiert 
\immediate\openout\verbatim@outSkr=\jobname.skr  % Open file for writing. 
}
\makeatother

\begin{document}

\begin{center}
\fbox{\fbox{\parbox{5.5in}{\centering
Answer the questions in the spaces provided on the
answer sheets.}}}
\end{center}

\begin{questions}

\question[60] Explain Principle A

\begin{skript}
\vspace{\stretch{1}}
\end{skript}

\question[40] Explain Principle B

\begin{skript}
\vspace{\stretch{1}}
\end{skript}

\end{questions}

\newpage

\begin{center}
\fbox{\fbox{\parbox{5.5in}{\centering
Answer sheet}}}
\end{center}
\bigskip

\includeSkripts

\end{document}

You can rename skript to something more obvious like answerbox. Basically it just writes the contents of the environment to a file and outputs it when \includeSkripts is called.