[Tex/LaTex] Setting up a Multiple Choice Answer Key

answers

I had a question about setting up a LaTeX document. I'm not sure if it is proper to post such a question here, but I figured that many people here have experience with the system and might be able to help.

I've been asked to write a list of questions with multiple choice answers. The people who will be using it don't know much about TeX, so I need to try to make the code as robust as possible. I've written a command to automatically produce the multiple choice options: the command

\mc{2}{3}{5}{7}{11}

roughly outputs

(A) 2  (B) 3  (C) 5  (D) 7  (E) 11

What I'd like to do is add in a sixth argument that will take the answer to the question and then automatically append it to a list at the end of the document, essentially creating an answer key. The problem is that it's not clear to me how to call the sixth argument of every \mc command into an \enumerate environment at the end of the document.

I would greatly appreciate any tips or suggestions anyone may have regarding this project.

Best Answer

You could adapt the solution from How keep a running list of strings and then process them one at a time for this purpose:

enter image description here

Code:

\documentclass{article}
\usepackage{pgffor}
\usepackage{enumitem}

\newcommand\AnswersList{}
\newcommand\AddAnswer[1]{\edef\AnswersList{\AnswersList#1\endgraf}}

\newcommand*{\mc}[6]{%
    % code to display the multiple choice for #1...#5
    \AddAnswer{#6,}%
}%

\begin{document}

\mc{2}{3}{5}{7}{11}{3}
\mc{2}{3}{5}{7}{11}{5}


The list of answers are:

\begin{enumerate}[label=(\arabic*)]
\foreach \answer in \AnswersList {%
    \item \answer
}%
\end{enumerate}
\end{document}
Related Question