[Tex/LaTex] Print answer key to multiple choice questions

cross-referencingexam

I'm using the exam document class, and I'd like to print the letter of the correct answer. Something like:

\documentclass{exam}
\begin{document}

\begin{questions}

    \question This is a multiple choice question
    \begin{choices}
       \choice this is a choice
       \choice this is another choice
       \CorrectChoice this is the correct choice
       \choice this is another choice
    \end{choices}

    The answer is /theCorrectChoice

\end{questions}

\end{document}

I've seen similar issues online but I haven't been able to find a good way to directly reference "the correct choice" the way you can reference /thequestion, for example. Does anyone know how to solve this?

Best Answer

You could update \CorrectChoice to insert an incremental \label. However, this would mean that only the most recent \CorrectChoice would be returned through a \theCorrectChoice call:

enter image description here

\documentclass{exam}% http://ctan.org/pkg/exam

\newcounter{correctchoicemark}% Counter to keep track of correct choice mark
\newcommand{\theCorrectChoice}{\ref{correctchoice-\thecorrectchoicemark}}% Print correct choice
\renewcommand{\choiceshook}{% Adapt \CorrectChoice to insert \label
  \let\oldCorrectChoice\CorrectChoice% Store \CorrectChoice in \oldCorrectChoice
  \renewcommand{\CorrectChoice}{%
    \oldCorrectChoice% Original \CorrectChoice
    \stepcounter{correctchoicemark}% Increment correct choice mark
    \label{correctchoice-\thecorrectchoicemark}% \label original \CorrectChoice
  }%
}
\let\oldoneparchoices\oneparchoices
\renewcommand{\oneparchoices}{%
  \oldoneparchoices% Original oneparchoices environment start
  \let\oldCorrectChoice\CorrectChoice% Store \CorrectChoice in \oldCorrectChoice
  \renewcommand{\CorrectChoice}{%
    \oldCorrectChoice% Original \CorrectChoice
    \stepcounter{correctchoicemark}% Increment correct choice mark
    \label{correctchoice-\thecorrectchoicemark}% \label original \CorrectChoice
  }%
}
\begin{document}

\begin{questions}

  \question This is a multiple choice question
  \begin{choices}
    \choice this is a choice
    \choice this is another choice
    \CorrectChoice this is the correct choice
    \choice this is another choice
  \end{choices}

  The answer is \theCorrectChoice.

  \question This is a multiple choice question
  \begin{oneparchoices}
    \choice this is a choice
    \CorrectChoice this is another choice
    \choice this is the correct choice
    \choice this is another choice
  \end{oneparchoices}

  The answer is \theCorrectChoice.

\end{questions}

\end{document}

exam provides a hook into the choices environment via \choiceshook. Updating this to redefine \CorrectChoice is one (clean) way of inserting the appropriate code to obtain a \label you can \reference. oneparchoices does not provide this hook, so a different method altogether is employed in order to redefine \CorrectChoice.

More work is required for this solution to work correctly with hyperref.