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 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.

share|improve this question

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.

    
Welcome to TeX.SE. –  Peter Grill May 29 '12 at 3:29
    
I'd recommend looking at the exam package. –  Caramdir May 29 '12 at 3:29
add comment

2 Answers

up vote 9 down vote accepted

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}
share|improve this answer
add comment

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

share|improve this answer
3  
I know the answer for 2 is not 7 (just in case). –  Gonzalo Medina May 29 '12 at 4:04
    
However, it's the successor of one :) –  Tom Bombadil May 29 '12 at 13:03
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.