9

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.

migrated from math.stackexchange.com May 29 '12 at 3:04

This question came from our site for people studying math at any level and professionals in related fields.

9

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

Here's a possible solution using the features provided by the exam document class and the answers package:

\documentclass{exam}
\usepackage{answers}

\Newassociation{sol}{Solution}{ans}

\newtheorem{ex}{}{}
\renewcommand\questionlabel{}
\renewcommand\questionshook{\leftmargin0pt}
\begin{document}

\Opensolutionfile{ans}[ans1]
\section{Problems}

\begin{ex}
\begin{questions}
\question
One of these things is not like the others; one of these things is not
the same. Which one is different?

\begin{oneparchoices}
\choice John
\choice Paul
\choice George
\choice Ringo
\choice Socrates
\end{oneparchoices}
\begin{sol}
Socrates
\end{sol}
\end{questions}
\end{ex}

\begin{ex}
\begin{questions}
\stepcounter{question}
\question
One of these numbers is perfect.

\begin{oneparchoices}
\choice $2$
\choice $28$
\choice $5$
\choice $3$
\choice $7$
\end{oneparchoices}
\end{questions}
\begin{sol}
$7$
\end{sol}
\end{ex}

\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}

\end{document}

enter image description here

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.