Answers at the end of the exam document
Presumably you want to type in the answer after each question (as you have them now), but to collect them and periodically display them in a group. This is just how endnotes work, so I'd use one of the endnotes packages: They all seem to support multiple sets of endnotes without any fuss. Here is a proof of concept using good old endnotes.sty
. You can easily tweak it to get the look you want, number answers automatically, etc.
\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers
\usepackage{url}
\usepackage{endnotes}
\def\enotesize{\normalsize}
\def\makeenmark{\relax}
\def\notesname{Answers}
\def\answer#1{\endnotetext{\vspace*{-3.5ex}\begin{solution}#1\end{solution}\unskip}}
\def\theanswers{\theendnotes \medskip}
\begin{document}
\begin{questions}
\addpoints \question This is the first question
\answer{This is the solution to question one.}
\addpoints \question This the second question
\answer{This is the solution to question two.}
\end{questions}
\theanswers
\end{document}
Note that you can call \theanswers
(i.e., \theendnotes
) as often as you want, and it will output the accumulated answers and clear the endnote file.
Edit: Tweaked it with some hacks to reduce spacing between solutions. Really the space comes from the solution
environment so this should be fixed by modifying its behavior, but it's hard to track down just which macro is responsible for how much of the vertical space.
The endnote package enotez
is another option, but endnotes.sty
is simpler and hence easier to hack.
Here is a solution with answers package
\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\usepackage{answers}
\Newassociation{sol}{Sol}{mycor}
\renewcommand{\Sollabel}[1]{\textbf{Solution #1.}}
\printanswers
\begin{document}
\Opensolutionfile{mycor}
\ifprintanswers
\renewenvironment{Sol}[1]{%
\framed\noindent\Sollabel{#1}}{\endframed}%
\else
\answerfilestrue
\renewcommand{\Readsolutionfile}[1]{}\fi
\begin{questions}
\addpoints
\begin{question} This is the first question
\begin{sol}
This is the solution to question one.
\end{sol}
\end{question}
\addpoints
\begin{question} This is the first question
\end{question}
\addpoints
\begin{question} This is the first question
\begin{sol}
This is the solution to question one.
\end{sol}
\end{question}
\end{questions}
\Closesolutionfile{mycor}
\Readsolutionfile{mycor}
\end{document}
if you want solutions just after question
\usepackage[nosolutionfiles]{answers}
exam
supports redefining the solution environment. See page 66 of this document, section 8.3.3. Simply create an environment that has a counter:
\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers
\begin{document}
\begin{questions}
\addpoints \question This is the first question
\addpoints \question This is the second question
\newcounter{solcounter}
\newcommand{\solcount}{\stepcounter{solcounter}\arabic{solcounter}. }
\renewenvironment{TheSolution}{
\solcount
}
\begin{solution}
This is the solution to question one.
\end{solution}
\begin{solution}
This is the solution to question two.
\end{solution}
\end{questions}
\end{document}