[Tex/LaTex] How to write a multiple choice test book with LaTeX

book-designexamexercisesmacrosquestionnaire

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

Best Answer

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}%
    \labelwidth\leftmargin\advance\labelwidth-\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
  \addanswer{\thequestion}{\thechoice}%
}
\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
}%

\newbox\allanswers
\setbox\allanswers=\hbox{}
\newcommand{\addanswer}[2]{%
  \global\setbox\allanswers=\hbox{\unhbox\allanswers #1.~#2\quad}%
}
\newcommand{\showanswers}{%
  \vfill
  \begin{center}
    Answers
  \end{center}
  \noindent\unhbox\allanswers
}



%--------------------------------------------------------------------
\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}

\showanswers
\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}

\showanswers

\end{document}