Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
how to list the answers in a separate sheet...???@Werner –  David Jul 6 at 6:33
    
@David: In what way? I think it's best if you ask a follow-up question. –  Werner Jul 7 at 15:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.