2

I am trying to use the exam class to create a study guide for my students that has all of the answers at the end of the document.

I would like to have the answers automatically labeled by question number and part, but I'm hung up on how to pass the question and part counters to the command I'm using to construct the end notes.

All of my attempts to do this end up labeling all answers with the most recent question and part counter values. My problem seems very related to this question, but I'm unable to bridge the gap between them. I've tried reading through a few posts on expanding arguments, but I don't know enough of the deeper aspects of TeX to understand what's going on.

Here's a (relatively minimal) working example. Again, I would like to have output that numbers answers like "1: 5, 2(a): 9, 2(b): 30, 3: 2 apples".

\documentclass{exam}

\usepackage[utf8]{inputenc}
\usepackage{endnotes}

\def\enotesize{\normalsize}
\def\makeenmark{\relax}
\def\notesname{\normalsize Answers}
\def\answer#1{\endnotetext{#1\\}}
\def\theanswers{\theendnotes}

\begin{document}
\noindent\textbf{Practice Problems}
\begin{questions}
\question How many sides does a pentagon have?
\answer{\thequestion: $5$}

\question Calculate the following:
\begin{parts}
\part $2+7$
\answer{\thequestion(\thepartno): $9$}

\part $5\cdot 6$
\answer{\thequestion(\thepartno): $30$}
\end{parts}

\question If I have four apples and I eat two, how many are left?
\answer{\thequestion: $2$ apples}
\end{questions}

\theanswers
\end{document}
2

You're seeing this problem, as you may already understand, because you need to expand the values of \thequestion and thepartno at the point of use. As you've written it, only the final value is used.

As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3 gives the most intuitive solution. In the following, I've rewritten your \answer macro to fully expand anything you pass it using the expl3 approach.

\documentclass{exam}

\usepackage[utf8]{inputenc}
\usepackage{endnotes}
\usepackage{xparse}

\renewcommand{\enotesize}{\normalsize}
\renewcommand{\makeenmark}{\relax}
\def\notesname{\normalsize Answers}
\def\theanswers{\theendnotes}

\ExplSyntaxOn

\cs_new:Nn \aegis_answer:n
{
  \endnotetext{#1\par}
}

\cs_generate_variant:Nn \aegis_answer:n {x}

\NewDocumentCommand{\answer}{m}
{
  \aegis_answer:x {#1}
}

\ExplSyntaxOff

\begin{document}
\noindent\textbf{Practice Problems}
\begin{questions}
\question How many sides does a pentagon have?
\answer{\thequestion: $5$}

\question Calculate the following:
\begin{parts}
\part $2+7$
\answer{\thequestion(\thepartno): $9$}

\part $5\cdot 6$
\answer{\thequestion(\thepartno): $30$}
\end{parts}

\question If I have four apples and I eat two, how many are left?
\answer{\thequestion: $2$ apples}
\end{questions}

\theanswers
\end{document}

The result:

enter image description here

Update: If you find expl3 off-putting, here's a method with \edef. Frankly, I find this much less transparent, but your mileage may vary:

\newcommand{\answer}[1]{%
  \begingroup
  \edef\x{%
    \endgroup
    \noexpand\endnotetext{#1\par}}%
   \x%
}

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.