# How to write a multiple choice test book with LaTeX?

I want to write a book which will consist of many multiple choice exam sheets. Putting the codes below into my preamble, I wrote the question like as

\begin{enumerate}
\item $2+2=?$
\choice $1$   \choice $2$  \choice $3$   \choice $4$   \choice $5$
\end{enumerate}


For each exam sheet, in the footer I want to create a table of correct answers. Is there any simple way to do this, or a LaTeX macro script to do this automatically by mentioning correct answer in its LaTeX codes? For example;

\choice 1 \choice 2 \choice 3 \correctchoice 4 \choice 5


In addition, since the choices are not side by side, I have lost many spaces. Is there any LaTeX codes to write choices more symmetrically and automatically in one or two row?

%%% Codes in preamble %%%

\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice)}
\newenvironment{choices}%
{\list{\choicelabel}% {\usecounter{choice}\def\makelabel##1{\hss\llap{##1}}% \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 1.5em}% \def\choice{% \item } % choice \labelwidth\leftmargin\advance\labelwidth-\labelsep \topsep=0pt \partopsep=0pt }% }%
{\endlist}

\newenvironment{oneparchoices}%
{% \setcounter{choice}{0}% \def\choice{% \refstepcounter{choice}% \ifnum\value{choice}>1\relax \penalty -50\hskip 1em plus 1em\relax \fi \choicelabel \nobreak\enskip }% choice % If we're continuing the paragraph containing the question, %then leave a bit of space before the first choice:

\ifvmode\else\enskip\fi
\ignorespaces
}%

-
Welcome to tex.sx! If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it). – Caramdir Apr 10 '11 at 17:40
Two pointers: Questionnaire Template and Package to create a printable form. These do not provide an exact answer but provide some relevant context. – Christian Lindig Apr 10 '11 at 17:45

Here's a variation on my earlier answer. In this version, the choices aren't part of any environment; you just start listing \choices and \correctchoices after the question.

Here's a complete LaTeX file that illustrates it.

\documentclass{article}

%--------------------------------------------------------------------
\newcounter{question}
\newif\ifinchoices
\inchoicesfalse
\newenvironment{questions}{%
\list{\thequestion.}%
{%
\usecounter{question}%
\def\question{\inchoicesfalse\item}%
\settowidth{\leftmargin}{10.\hskip\labelsep}%
}%
}
{%
\endlist
}%

\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}
\def\choice{%
\ifinchoices
% Do nothing
\else
\startchoices
\fi
\refstepcounter{choice}%
\ifnum\value{choice}>1\relax
\penalty -50\hskip 1em plus 1em\relax
\fi
\choicelabel
\nobreak\enskip
}% choice
\def\CorrectChoice{%
\choice
}
\let\correctchoice\CorrectChoice

\newcommand{\startchoices}{%
\inchoicestrue
\setcounter{choice}{0}%
% \par % Uncomment this to have choices always start a new line
% If we're continuing the paragraph containing the question,
% then leave a bit of space before the first choice:
\ifvmode\else\enskip\fi
}%

}
\vfill
\begin{center}
\end{center}
}

%--------------------------------------------------------------------
\begin{document}

\begin{questions}
\question What was the color of George Washington's white horse?
\choice Blue
\choice Yellow
\correctchoice White
\choice Black

\question Which of these things doesn't fit in?
\choice John
\choice Paul
\choice George
\choice Ringo
\correctchoice Socrates

\question Who's buried in Grant's tomb?
\choice Washington
\choice Mickey Mouse
\correctchoice Grant
\end{questions}

\newpage

\begin{questions}
\question $1+1= \mathord{?}$
\choice $0$ \choice $1$ \correctchoice $2$ \choice $3$ \choice $4$

\question $\sin \frac{\pi}{2} = \mathord{?}$
\choice $0$ \correctchoice $1$ \choice $2$ \choice $\frac{\pi}{2}$

\question What was the color of the bus driver's eyes?
\choice Green \choice Yellow \correctchoice Blue \choice Red
\end{questions}

\end{document}

-

The following extracts the basics of the questions environment and the oneparchoices environment from the exam documentclass, but I changed the name oneparchoices to plain old choices, since it's the only one you'll be using here. This leaves out all the extra baggage from the exam class that you don't need. You can use it with the regular article class (or any other class that you like).

To collect all the correct choices, we use an \hbox, but we put it onto the page using an \unhbox command, so that when it hits the page it's not in a box, and so there's no interference with linebreaks.

Here's a complete LaTeX file that illustrates (nearly) everything.

\documentclass{article}

%--------------------------------------------------------------------
\newcounter{question}
\newenvironment{questions}{%
\list{\thequestion.}%
{%
\usecounter{question}%
\def\question{\item}%
\settowidth{\leftmargin}{10.\hskip\labelsep}%
}%
}
{%
\endlist
}%

\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}
\makeatletter
\newenvironment{choices}%
{%
\setcounter{choice}{0}%
\def\choice{%
\refstepcounter{choice}%
\ifnum\value{choice}>1\relax
\penalty -50\hskip 1em plus 1em\relax
\fi
\choicelabel
\nobreak\enskip
}% choice
\def\CorrectChoice{%
\choice
}
\let\correctchoice\CorrectChoice
%    \par % Uncomment this to have choices always start a new line
\let\par\@empty
% If we're continuing the paragraph containing the question,
% then leave a bit of space before the first choice:
\ifvmode\else\enskip\fi
\ignorespaces
}%
{}
\makeatother
}
\vfill
\begin{center}
\end{center}
}

%--------------------------------------------------------------------
\begin{document}

\begin{questions}
\question What was the color of George Washington's white horse?
\begin{choices}
\choice Blue
\choice Yellow
\correctchoice White
\choice Black
\end{choices}
\question Which of these things doesn't fit in?
\begin{choices}
\choice John
\choice Paul
\choice George
\choice Ringo
\correctchoice Socrates
\end{choices}
\question Who's buried in Grant's tomb?
\begin{choices}
\choice Washington
\choice Mickey Mouse
\correctchoice Grant
\end{choices}
\end{questions}

\newpage

\begin{questions}
\question $1+1= \mathord{?}$
\begin{choices}
\choice $0$ \choice $1$ \correctchoice $2$ \choice $3$ \choice $4$
\end{choices}
\question $\sin \frac{\pi}{2} = \mathord{?}$
\begin{choices}
\choice $0$ \correctchoice $1$ \choice $2$ \choice $\frac{\pi}{2}$
\end{choices}
\question What was the color of the bus driver's eyes?
\begin{choices}
\choice Green \choice Yellow \correctchoice Blue \choice Red
\end{choices}
\end{questions}

\end{document}

-

I use the exam document class to make up multiple-choice tests. It may not do exactly what you describe, but it's pretty well thought out and reasonably customizable. The documentation on the package page includes many examples you can browse to see if it will suffice.

In particular, it has options for indicating the correct answers in your source code, and with a one-line command switching between the student version (without answers) and the instructor version (with correct answers indicated).

-

Take a look at this, I know is not the best way but this is also a way. . .

\documentclass[a4paper,12pt]{book}
\usepackage{pgf}

\newtheorem{exercise}{\color{magenta} \large \bfseries}[chapter]
\Newassociation{sol1kef1}{solutions1}{ans11}

\begin{document}
\Opensolutionfile{ans11}[sol1]

\chapter{Oscillations}
\section{Kinematics Approach}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{exercise}
\begin{enumerate}
\item first choice
\item second choice
\item fourth choice
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{sol1kef1}
\end{sol1kef1}
\end{exercise}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Closesolutionfile{ans11}

\chapter{Solutions}
\input{sol1.tex}

\end{document}

-

Also the LaTeX alternative Context has some features useful for test design. The creator of Context (Hans Hagen) designs lots of college texts.

-