14

I'm using the exam class, and I want to add answers. Right now the solutions are printet after the question, but before the next question. Ideally they would be presented at the end of a serie of questions. I'd like either

  • the numeric or very simple one-line answers to questions (something like a new \begin{numans} ... \end{numans} environment), or
  • the entire solution as it is now with the solution environment,

at the end of the document. Any ideas?

An MWE:

\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers

\begin{document}

\begin{questions}
\addpoints \question This is the first question
\begin{solution}
    This is the solution to question one. 
\end{solution}

\addpoints \question This is the second question
\begin{solution}
    This is the solution to questino two. 
\end{solution}
\end{questions}

\end{document}

Edit: the solution in Erik's suggestion is possible, but I would like to be able to stick to my already exam classed documents.

  • There's an implementation here, but it doesn't use the exam class. – erik Apr 7 '15 at 22:24
  • @erik This is absolutely the concept I'm looking for. Thanks! I could use this, although, I would also prefer a solution for the exam class. – Holene Apr 8 '15 at 5:21
  • Perhaps you could still use the exam class but incorporate a custom command similar to those in the link. – erik Apr 8 '15 at 6:17
  • Related, but unanswered: tex.stackexchange.com/q/176194/9668 :-( – alexwlchan Apr 9 '15 at 21:40
  • One idea: have an “array” (how, I don’t know) of solution texts. Whenever you have a solution environment, the text gets added to this array. At the end of the document, have a section that pops items off this array and makes them look like actual solutions. – alexwlchan Apr 9 '15 at 21:59
14
+100

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.

  • Nice! This was really helpful! How can I avoid the massive amount of space between the endnote entries (solutions)? – Holene Apr 10 '15 at 11:51
  • 1
    I took a look, and it seems that all the vertical space is coming from the exam class. I added some negative spacing (hack!) to get it to look better, but it is not the prettiest solution... Try it again, though. – alexis Apr 10 '15 at 14:40
  • @Holene, do you want to retain the boxes around each solution? If you don't want them, it should be easier to format the solutions in an appealing way. – alexis Apr 12 '15 at 18:41
  • Boxes aren't important. Just now I declare \unframedsolutions, but the spacing is still way too large! – Holene Apr 12 '15 at 18:52
8

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}
3

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}
  • 2
    Good thought. This only works if you write the solutions at the end of the document, not after the questions (which I find more natural). – Holene Apr 12 '15 at 6:49
1

You can combine two of the above styles to make a simple and separate set of solutions that avoids the [Solutions:] presentation for each question and makes an answer bank at the end of the exam. This is great if you want to presumably make a list separate from the exam.

\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers

\usepackage{url}
\usepackage{endnotes}

\newcounter{solcounter}
\newcommand{\solcount}{\stepcounter{solcounter}\arabic{solcounter}. }
\renewenvironment{TheSolution}{
    \solcount 
}

\def\enotesize{\normalsize} 
\def\makeenmark{\relax}
\def\notesname{Answers}
\def\answer#1{\endnotetext{\vspace*{1ex}\begin{solution}#1\end{solution}\unskip}}
\def\theanswers{\theendnotes \medskip}


\begin{document}

\begin{questions}
\question Is this the first question?
    \begin{choices}
        \choice{yes}
        \choice{no}
        \choice{maybe}
        \choice{doesn't matter}
    \end{choices}
\answer{ \textbf{A)} because it is the first question.}

\question This the second question
\answer{This is the solution to question two.}

\end{questions}


\theanswers

\end{document}
0

Similar to alexis' answer but instead of storing solutions with endnotes package, expl3 is used, storing each solution in a seq variable

\documentclass[a4paper]{exam}
\usepackage{xparse}
\printanswers

\ExplSyntaxOn
\seq_new:N \l_exam_endprint_seq
\NewDocumentCommand \WriteAnswer { +m } { \seq_gput_right:Nn \l_exam_endprint_seq { #1 } }
\NewDocumentCommand \EndPrintAnswers { } {
    \seq_map_inline:Nn \l_exam_endprint_seq { \begin{solution} ##1 \end{solution} }
}
\ExplSyntaxOff

\begin{document}
\begin{questions}
\addpoints \question This is question one.
\WriteAnswer{This is the solution to question one.}
\addpoints \question This is question two.
\WriteAnswer{This is the solution to question two.}
\addpoints \question This is the third question
\WriteAnswer{This is the solution to question three.}
\end{questions}
\EndPrintAnswers
\end{document}

If desired, numbering can be introduced by adding a counter (this will have issues if the questions have parts). The following should be added to the preamble

\newcounter{solution}
\stepcounter{solution}

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \arabic{solution}:}\enspace\stepcounter{solution}}

This combined produces enter image description here

As noted elsewhere in this question, adjusting the spacing between successive solution environments may be desirable.

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.