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 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
}%
share|improve this question
    
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
1  
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

5 Answers 5

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

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}%
    \labelwidth\leftmargin\advance\labelwidth-\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
      \addanswer{\thequestion}{\thechoice}%
    }
    \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
\newbox\allanswers
\setbox\allanswers=\hbox{}
\newcommand{\addanswer}[2]{%
  \global\setbox\allanswers=\hbox{\unhbox\allanswers \quad #1.~#2}%
}
\newcommand{\showanswers}{%
  \vfill
  \begin{center}
    Answers
  \end{center}
  \unhbox\allanswers
}



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

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

\showanswers

\end{document}
share|improve this answer

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

share|improve this answer

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

\documentclass[a4paper,12pt]{book}
\usepackage{answers}
\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}
your question here \ldots 
\begin{enumerate}
 \item first choice
 \item second choice
 \item the correct answer \label{label1}
 \item fourth choice
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{sol1kef1}
the correct answer is: (\textbf{\ref{label1}})
\end{sol1kef1} 
\end{exercise}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Closesolutionfile{ans11}

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

\end{document}
share|improve this answer

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

share|improve this answer

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.