[OS X TeX] Conditional Typesetting Inside Lists

Matthew Leingang leingang at math.harvard.edu
Tue Feb 26 21:02:46 CET 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  

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:


% this is how choices will be formatted in the exam
% this is how the correct choice will be formatted in the key
% \fbox could be \textbf or something else
   \begin{list}{% item command
	 \ifthenelse{\boolean{correct} \and \boolean{key}}
   }{% list preamble
    % this extends the definition of \item so that \item*
    % indicates a correct answer

% set to true to print the key, false otherwise


What is the capital of California?
\item San Francisco
\item Los Angeles
\item* Sacramento

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

\item* George Walker Bush
\item  Neil Mallon Bush
\item  John Ellis Bush
\item*  George Herbert Walker Bush



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}

\input preamble
\input source

\input preamble
\input source

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


More information about the macostex-archives mailing list