# [OS X TeX] Conditional Typesetting Inside Lists

Matthew Leingang leingang at math.harvard.edu
Tue Feb 26 15:02:46 EST 2008

On Feb 26, 2008, at 10:41 AM, Maurino Bautista wrote:
> We have a multiple choice section in our Calculus final exam and the
> questions and choices are entered using the \begin{enumerate} --
> \end{enumerate} environment in LaTeX. I would like to use the same
> file to generate the exam as well as the answer key for the exam. I
> would like to define a variable whose values are either exam or key.
> In the beginning of the document, this variable will be declared and
> if its value is exam, the file would be typeset as usual with the
> questions numbered 1, 2, 3, etc and the choices within each question
> labeled A, B, C, etc. If the value of the variable is key, the
> letter-label of the correct answer will be enclosed in a box.
> Alternative possibilities (which may be easier or harder) are:
> letter-label in bold, the correct answer (not the letter-label) is
> boxed or in bold, text after the correct answer stating this is the
> correct answer or some other symbol like a star, a dingbat, etc. I
> suppose, if I have the code for one of these, I could modify it to
> obtain any of the others. I just don't have enough knowledge to start
> it. Rather, I don't have time to really think about it. So, if any of
> you have done it before, I would appreciate getting some help.

Dear Maurino,

The versions package can allow you to declare document "modes" and
evaluate things conditionally, so you can redefine commands based on
flags.

It's a little bit trickier to actually make the list item label
change (at least, to me it was), but here's a way:

--
\documentclass{article}
\usepackage{enumitem}
\usepackage{ifthen}

\newboolean{key}
\newboolean{correct}
\makeatletter
\newcounter{choice}
% this is how choices will be formatted in the exam
\newcommand{\@thechoice}{\Alph{choice}}
% this is how the correct choice will be formatted in the key
% \fbox could be \textbf or something else
\newcommand{\@thecorrectchoice}{\fbox{\Alph{choice}}}
\newenvironment{mcenum}{%
\begin{list}{% item command
\ifthenelse{\boolean{correct} \and \boolean{key}}
{\@thecorrectchoice}
{\@thechoice}
}{% list preamble
\usecounter{choice}
\setboolean{correct}{false}
% this extends the definition of \item so that \item*
\let\olditem=\item
\def\item{\@ifstar%
{\setboolean{correct}{true}\olditem\setboolean{correct}{false}}
{\olditem}}
}}{\end{list}}
\makeatother

% set to true to print the key, false otherwise
\setboolean{key}{true}

\begin{document}

\begin{enumerate}
\item
What is the capital of California?
\begin{mcenum}
\item San Francisco
\item Los Angeles
\item* Sacramento
\end{mcenum}

\item
Which of these Bushes have been President of the United States?

\begin{mcenum}
\item* George Walker Bush
\item  Neil Mallon Bush
\item  John Ellis Bush
\item*  George Herbert Walker Bush
\end{mcenum}

\end{enumerate}

\end{document}
--

I don't like to produce different documents with the same job name.
Here the contents of test.pdf are going to be either the test or the
key, depending on the value of the flag when you last typeset.  So
that means you have to open it to find out.

What I do is create separate files such as source.tex, test.tex, and
key.tex.  The first has all the main text in it.  The last two are
just shells that set the right flags and include the source.  To
avoid repeating yourself as much as possible, farm out preamble code
into a fourth file.  So:

preamble.tex: \newcommand's and stuff from above

source.tex: everything between \begin{document} and \end{document}

test.tex:
\documentclass{article}
\input preamble
\setboolean{key}{false}
\begin{document}
\input source
\end{document}

key.tex:
\documentclass{article}
\input preamble
\setboolean{key}{true}
\begin{document}
\input source
\end{document}

Actually, what I do is put it all in a single docstrip file with the
driver code at the top.  So one run on the docstrip file extracts the
source and all the shells.  Then you run on the shell file you want.
It makes the whole test portable.  But that's pretty hacky; I'm not
sure if I recommend it.

Good luck.

--Matthew Leingang

--
Matthew Leingang
Preceptor in Mathematics
Harvard University

http://www.math.harvard.edu/~leingang/vCard.vcf