# How to automatically align the four choices of a multiple choice question in exam?

We can have three types of column typeset. See the image below:

The above image was created using the LaTeX code below:

\documentclass{article}
\usepackage{ifthen}
\usepackage{calc}
\setlength\parindent{0pt}

%usage \choice{ }{ }{ }{ }
%(A)(B)(C)(D)
\newcommand{\fourch}[4]{
\par
\begin{tabular}{*{4}{@{}p{0.23\textwidth}}}
(A)~#1 & (B)~#2 & (C)~#3 & (D)~#4
\end{tabular}
}

%(A)(B)
%(C)(D)
\newcommand{\twoch}[4]{
\par
\begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
(A)~#1 & (B)~#2
\end{tabular}
\par
\begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
(C)~#3 & (D)~#4
\end{tabular}
}

%(A)
%(B)
%(C)
%(D)
\newcommand{\onech}[4]{
\par
(A)~#1 \par (B)~#2 \par (C)~#3 \par (D)~#4
}

\newlength\widthcha
\newlength\widthchb
\newlength\widthchc
\newlength\widthchd
\newlength\widthch
\newlength\tabmaxwidth

\setlength\tabmaxwidth{0.96\textwidth}
\newlength\fourthtabwidth
\setlength\fourthtabwidth{0.25\textwidth}
\newlength\halftabwidth
\setlength\halftabwidth{0.5\textwidth}

\newcommand{\choice}[4]{
\settowidth\widthcha{AM.#1}\setlength{\widthch}{\widthcha}
\settowidth\widthchb{BM.#2}
\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
\settowidth\widthchb{CM.#3}
\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
\settowidth\widthchb{DM.#4}
\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
\ifthenelse{\widthch<\fourthtabwidth}{\fourch{#1}{#2}{#3}{#4}}
{\ifthenelse{\widthch<\halftabwidth\and\widthch>\fourthtabwidth}{\twoch{#1}{#2}{#3}{#4}}
{\onech{#1}{#2}{#3}{#4}}}
}

\begin{document}
Question:
\choice{apple}{banana}{orange}{pear}
\par
Another Question:
\choice{apple}{banana}{orang, eeeeeeeee}{pear}
\par
Third Question:
\choice{apple}{banana}{orange, eeeeeeeee, eeeeeeeee, eeeeeeeee}{pear}

\end{document}


Then main algorithm checks the longest width of the choices, if it is larger than 0.5\textwidth, then we should use the one column style. If the largest choice is larger than 0.25\textwidth, then we can put them in the two column style. Otherwise, all the choices can be put on one line.

But I'm not satisfied with the code above. I would like to use the code snippet like below:

\begin{question}
What do you like best?
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}
\end{question}
==>
What do you like best?..................................(B)
A. apple    B. banana    C. orange   D. pear

\begin{question}
What do you like best?
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orang, eeeeeeeee
\choice0 pear
\end{choices}
\end{question}
==>
What do you like best?..................................(B)
A. apple                B. banana
C. orange, eeeeeeeee    D. pear

\begin{question}
What do you like best?
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange, eeeeeeeee, eeeeeeeee, eeeeeeeee
\choice0 pear
\end{choices}
\end{question}
==>
What do you like best?..................................(B)
A. apple
B. banana
C. orange, orange, eeeeeeeee, eeeeeeeee, eeeeeeeee
D. pear


In this code, an environment is used instead of a new command. An issue is that the answer can be automatically put at the end of the question line. It is better to have the answers 'show/hide' option.

Addendum: I have a general approach in mind, which is: When the LaTeX parser see a \choice0 or \choice1, it can put the later text in a token list, then all the four token list will be typeset according to their lengths when leaving the question or choices environment, but sorry I'm not familiar with LaTeX programming. (The exam package does not have such feature I wanted, so that's the reason I asked here.)

Edit: since egreg give me the answer for align the four choices in a compact way. I sill see some improve in putting a dotted line after the question title, and optionally put the correct answer or a blank brace there. So, for egreg's source, if I wrote this:

\begin{choices}
This is the question.\par
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}

Or, it is better to put the question before the choices environment.
This is the question.
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}

I would suggest that the output should be:

This is the question...................................( )
A. apple                B. banana
C. orange, eeeeeeeee    D. pear

Or if need to show the correct answer
This is the question...................................(B)
A. apple                B. banana
C. orange, eeeeeeeee    D. pear


Thanks.

EDIT2: As minthao_2011 said in the comments, the code below has some issue, the long choice will not wrap the line, see the image shot below.

\documentclass{article}
\usepackage{environ}
\newlength{\choiceslen}

\newif\ifshowcorrect
\newcounter{choices}
\newcommand{\choicefinal}[1]{%
\ifnum\value{choices}>0 \hfill\fi\egroup
\hspace{0pt}%
\hbox to\choiceslen
\bgroup
\stepcounter{choices}%
\ifcase#1\relax
(\Alph{choices})%
\else
\ifshowcorrect
\expandafter\underline
\fi
{(\Alph{choices})}%
\fi\space}
\newcommand{\choicetemp}[1]{\stepcounter{choices}\space(\Alph{choices})\cr}

\NewEnviron{choices}
{\setcounter{choices}{0}%
\let\choice\choicetemp
\settowidth{\choiceslen}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}
\ifdim\choiceslen>.5\textwidth
\setlength{\choiceslen}{\textwidth}%
\else
\ifdim\choiceslen>.25\textwidth
\setlength{\choiceslen}{.5\textwidth}%
\else
\setlength{\choiceslen}{.25\textwidth}%
\fi
\fi
\let\choice\choicefinal
\setcounter{choices}{0}%
\begin{flushleft}
\bgroup\BODY\hfill\egroup
\end{flushleft}}

\begin{document}
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}

\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orang, eeeeeeeee
\choice0 pear
\end{choices}

\showcorrecttrue

\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange, eeeeeeeee, eeeeeeeee, eeeeeeeee
\choice0 pear
\end{choices}

\begin{choices}
\choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question.
\choice1 This is a very long question. This is a very long question. This is a very long question. This is a very long question.
\choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question.
\choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question.
\end{choices}

\end{document}

-
What is a multiply choice? –  Nicholas Hamilton Oct 27 '13 at 11:32
Have you seen the packages to write exams? –  Fran Oct 27 '13 at 11:57
Hi, Fran, I have read all the packages in the exam topic in the link you give, but non of them has the feature in my question, thanks. –  ollydbg23 Oct 27 '13 at 12:52
@ADP a multiply choice question is that the four candidate question answers are listed below the questions, and user need to select which one is the correct choice(answer). –  ollydbg23 Oct 28 '13 at 13:25

With environ and some low level TeX.

\documentclass{article}
\usepackage{environ}
\newlength{\choiceslen}

\newif\ifshowcorrect
\newcounter{choices}
\newcommand{\choicefinal}[1]{%
\ifnum\value{choices}>0 \hfill\fi\egroup
\hspace{0pt}%
\hbox to\choiceslen
\bgroup
\stepcounter{choices}%
\ifcase#1\relax
(\Alph{choices})%
\else
\ifshowcorrect
\expandafter\underline
\fi
{(\Alph{choices})}%
\fi\space}
\newcommand{\choicetemp}[1]{\stepcounter{choices}\space(\Alph{choices})\cr}

\NewEnviron{choices}
{\setcounter{choices}{0}%
\let\choice\choicetemp
\settowidth{\choiceslen}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}
\ifdim\choiceslen>.5\textwidth
\setlength{\choiceslen}{\textwidth}%
\else
\ifdim\choiceslen>.25\textwidth
\setlength{\choiceslen}{.5\textwidth}%
\else
\setlength{\choiceslen}{.25\textwidth}%
\fi
\fi
\let\choice\choicefinal
\setcounter{choices}{0}%
\begin{flushleft}
\bgroup\BODY\hfill\egroup
\end{flushleft}}

\begin{document}
\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange
\choice0 pear
\end{choices}

\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orang, eeeeeeeee
\choice0 pear
\end{choices}

\showcorrecttrue

\begin{choices}
\choice0 apple
\choice1 banana
\choice0 orange, eeeeeeeee, eeeeeeeee, eeeeeeeee
\choice0 pear
\end{choices}

\end{document}


I first box the items using an \halign so they can be measured. In this way I can set the length according to the specified cases and retypeset the items again. I added also a conditional for marking the correct answer.

-
many many thanks, and works perfectly. I never know the environ package, but it seem this package did a wonderful job. I will contact the author of exam class, and see whether he can put this in his package. –  ollydbg23 Oct 29 '13 at 1:06
I have modified my original question (add a section named Edit) with some details explained. Your answer is already good enough, I can use them, but if it can put the correct answer after the question title, it becomes better. Thanks. –  ollydbg23 Oct 29 '13 at 1:38
How to fix this case\begin{choices} \choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question. \choice1 This is a very long question. This is a very long question. This is a very long question. This is a very long question. \choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question. \choice0 This is a very long question. This is a very long question. This is a very long question. This is a very long question. \end{choices} –  minthao_2011 Oct 29 '13 at 4:07
@minthao_2011 I'm not even considering multiline answers. –  egreg Oct 29 '13 at 7:24

\documentclass[11pt]{vieexamdesign}
\usepackage{amsmath,amsxtra,latexsym, amssymb, amscd}
\usepackage[utf8]{vietnam}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{times}
\usepackage{ifthen}
\usepackage{dethi} %Gói lệnh cho đề thi Việt Nam
%\usepackage{enumitem}
\usepackage{enumerate}
\usepackage{verbatim}
%\usepackage{hyper}
\Fullpages %Định dạng trang đề thi
\ContinuousNumbering
\ShortKey

\NumberOfVersions{1}
\SectionPrefix{\relax }

\tentruong{University}
\tenkhoa{Mathematica}
\loaidethi{K51 (ĐỀ THI CHÍNH THỨC)}
\tenkythi{Test}
\tenmonhoc{Subject}
\thoigian{Time: 90''}
\tieudetracnghiem
%\tieudethiviet
\tieudedapan
%\tieudetren
\tieudeduoi
\chucauhoi{Problem}
\mauchu{black}
\socauhoi{60}
\sotrang{4}
\setlength{\baselineskip}{12truept}
\def\v#1{\overrightarrow{#1}}

\def\v#1{\overrightarrow{#1}}
\usepackage{listings}

\begin{document}

\setlength{\baselineskip}{10truept}
\begin{multiplechoice}[title={\it Choose the correct answer of each question} , rearrange=yes , keycolumns=6]%

\begin{question}%1
$2 + 2 =$
\datcot
\bonpa
{\sai{1}}
{\sai{3}}
{\dung{4}}
{\sai{2}}
\end{question}

\begin{question}%2
This is a question \datcot[2]
\bonpa
\end{question}

\begin{question}%2
This is a question \datcot
\bonpa
{\sai{Long question  Long question   Long question   Long question   answer.}}
{\dung{Long question  Long question   Long question   Long question   answer.}}
{\sai{Long question  Long question   Long question   Long question   answer. }}
{\sai{Long question  Long question   Long question   Long question   answer .}}
\end{question}
\end{multiplechoice}
\end{document}


Another package is also of a Vietnames. You can dowload at here http://mathblog.org/cai-dat-va-su-dung-phan-mem-lam-de-trac-nghiem/ EX_test.

\documentclass[12pt,a4paper]{article}
\usepackage{ex_test}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}\usepackage{enumerate}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm] {geometry}

\begin{document}
\noindent
\begin{minipage}[b]{6cm}
\centerline{\textbf{\fontsize{13}{0}\selectfont Manchester University}}
\centerline{\rule{1.7in}{1pt}}
\centerline{\fontsize{13}{0}\selectfont Let's try}
\centerline{(\textit{Test containts \sotrang\ page(s)})}
\end{minipage}\hspace{1.5cm}
\begin{minipage}[b]{11cm}
\centerline{\textbf{\fontsize{13}{0}\selectfont Year 2013 - 2014}}
\centerline{\textbf{\fontsize{12}{0}\selectfont Subject: Math}}
\centerline{\textit{\fontsize{12}{0}\selectfont Times for Test: 90 minustes}}
\centerline{}
\end{minipage}\\[0.1cm]
\rightline{
\setlength\fboxrule{2pt}
\setlength\fboxsep{5pt}
}
\noindent
\begin{minipage}[b]{10cm}
\textbf{Name of student:}\dotfill\\
\textbf{Number:}\dotfill
\end{minipage}\\
\Opensolutionfile{ans}[ans1]
\begin{ex}
The real  solutions of the equation $x^2 - 2x - 3 = 0$ are
\boncot{$-1$ and $3$}{$1$ and $3$}{$1$ and $-3$}{$1$ and $3$}
\begin{sol}
A
\end{sol}
\end{ex}

\begin{ex}
The real solutions of the equation $x^2 - 2x - 3 = 0$ are
\haicot{$-1$ and $3$}{$1$ and $3$}{$1$ and $-3$}{$1$ and $3$}
\begin{sol}
A
\end{sol}
\end{ex}

\begin{ex}
The real solutions of the equation $x^2 - 2x - 3 = 0$ are
\motcot{$-1$ and $3$}{$1$ and $3$}{$1$ and $-3$}{$1$ and $3$}
\begin{sol}
A
\end{sol}
\end{ex}

\begin{ex}
This is a long question  This is a long question This is a long question
\motcot{This is a long question This is a long question This is a long question This is a long question This is a long question This is a long question}{This is a long question This is a long question This is a long question This is a long question This is a long question This is a long question}{This is a long question This is a long question This is a long question This is a long question This is a long question This is a long question}{This is a long question This is a long question This is a long question This is a long question This is a long question This is a long question}
\begin{sol}
A
\end{sol}
\end{ex}

\Closesolutionfile{ans}
\indapan
\begin{multicols}{10}
\input{ans1}
\end{multicols}
\end{document}


Note that

• motcot: One column;
• haicot: two columns;
• boncot: four columns. After you repair on Tex, using Ex_test.batch and run the program. You can make many version.
-
Thanks, I just download the dethitracnghiem.zip, and I did see the choices get re-arranged in the pdf files, but as I don't know Vietnam language, it is a bit hard to read the Tex sources. –  ollydbg23 Oct 27 '13 at 12:55
Dear Minthao_2011, I see that the dethitracnghiem.zip did Not solve my problem. I use Google translate to read the Tex source files in this zip achieve, I tested it under Miktex right now, there is a command: \datcot which will put the four choices in one row, but \datcot[2] will put the four choices in two rows, so it is Not automatically typeset the choices, but the user need to specify the row numbers. –  ollydbg23 Oct 27 '13 at 15:42
You can contact with the author. –  minthao_2011 Oct 28 '13 at 14:18
Hi, minthao_2011, I have emailed the author, but currently no replies. I have further read the dethitracnghiem.zip, I don't see it has the automatically set the column style feature, so I'm not sure that whether Nguyen Huu Dien can give me further help. Thanks. –  ollydbg23 Oct 28 '13 at 14:26
Dear minthao_2011, thanks for your test code, but you code still not solve my issue. E.g. In your Tex code, if you change the line {\dung{4}} to a long question line like: {\dung{4 Long question Long question Long question Long question answer.}}, you will have a two column choices, first row containing A and B, the second row contains C and D, but note that the choices are not aligned. The expect result is one column (four rows). –  ollydbg23 Oct 29 '13 at 0:55

This solution is similar to egreg's, but with some minor improvements for long items and the syntax.

\documentclass{article}
\usepackage{environ}
\usepackage[normalem]{ulem}

\makeatletter
\newlength\choiceitemwidth
\newif\ifshowsolution \showsolutiontrue
\newcounter{choiceitem}
\def\thechoiceitem{\Alph{choiceitem}}
\def\makechoicelabel#1{#1\uline{\thechoiceitem.}\else\thechoiceitem.\fi\space}
\def\choice@mesureitem#1{\cr\stepcounter{choiceitem}\makechoicelabel#1}
\def\choicemesureitem{\@ifstar
{\choice@mesureitem\ifshowsolution}%
{\choice@mesureitem\iffalse}}%
\def\choice@blockitem#1{%
\ifnum\value{choiceitem}>0\hfil\fi
\egroup\hskip0pt
\hbox to \choiceitemwidth\bgroup\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceblockitem{\@ifstar
{\choice@blockitem\ifshowsolution}%
{\choice@blockitem\iffalse}}
\def\choice@paraitem#1{%
\par\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceparaitem{\@ifstar
{\choice@paraitem\ifshowsolution}%
{\choice@paraitem\iffalse}}
\NewEnviron{choices}{%
\begingroup
\let\item\choicemesureitem
\setcounter{choiceitem}{0}%
\settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
\endgroup
\trivlist\item\relax
\parindent0pt
\setcounter{choiceitem}{0}%
\ifdim\choiceitemwidth<0.25\columnwidth
\choiceitemwidth=0.25\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfil\egroup
\else\ifdim\choiceitemwidth<0.5\columnwidth
\choiceitemwidth=0.5\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfil\egroup
\else % \choiceitemwidth > 0.5\columnwidth
\let\item\choiceparaitem
\BODY
\fi\fi
\endtrivlist
}
\makeatother

\begin{document}

text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{choices}
\item* foo
\item bar
\item* baz
\end{choices}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{choices}
\item* foo
\item bar
\item* foo
\item bar
\end{choices}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{choices}
\item* foo
\item bar
\item* foo
\item bar with long long text
\end{choices}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{choices}
\item* foo
\item bar
\item* baz
\item text text text text text text text text text text
text text text text text text text text text text
\end{choices}

\showsolutionfalse

\begin{choices}
\item* foo
\item bar
\item* foo
\item bar
\end{choices}

\end{document}

-
Can I make a page which only contains the ansewrs of all questions? –  minthao_2011 Oct 31 '13 at 10:53
@minthao_2011: You will need to write some extra code for that. –  Leo Liu Oct 31 '13 at 14:39
@minthao_2011, I see Leo Liu have a solution to put all the answers in another page, see here: bbs.ctex.org/…, note this is in Chinese ctex forum, so only some Chinese words there in the Tex code, but does not affect the understanding. –  ollydbg23 Oct 31 '13 at 15:05
@minthao_2011: answers package will help you for this purpose, I guess. –  Leo Liu Oct 31 '13 at 15:14

Ok, with the help of Liu and other people in bbs.ctex.org, also some Tex code to draw dotted line from Philip Hirschhorn(exam package author). The code below can mainly solve my question, it can automatically align the choices in three style of format, and add a dotted line to automatically print the answer. I try to renew the environment of choices in exam class, but I get build failure, so I choose to define a new environment named "items". Here is the code and screen shots with answers printed or not.

\documentclass[answers,12pt]{exam}
%\documentclass[12pt]{exam}
\usepackage{environ}
\usepackage[normalem]{ulem}
\usepackage{etoolbox}

% One optional argument, the default value of which is empty.
\unskip\linebreak[0]\enspace
\hbox{}\nobreak\dotfill
\else
\hbox{(\hskip 0.5in)}%
\fi

\makeatletter
\newlength\choiceitemwidth
\newif\ifshowsolution \showsolutiontrue
\newcounter{choiceitem}%

\def\thechoiceitem{\Alph{choiceitem}}%

\def\makechoicelabel#1{#1\uline{\thechoiceitem.}\else\thechoiceitem.\fi\space} %underline the answer item label if we want to print the answer

\def\choice@mesureitem#1{\cr\stepcounter{choiceitem}\makechoicelabel#1}%

%measure the choices, this is the first time we need to parse the \BODY
\def\choicemesureitem{\@ifstar
{\choice@mesureitem\iffalse}}%

\def\choice@blockitem#1{%
\ifnum\value{choiceitem}>0\hfill\fi
\egroup\hskip0pt
\hbox to \choiceitemwidth\bgroup\hss\refstepcounter{choiceitem}\makechoicelabel#1}

\def\choiceblockitem{\@ifstar
{\choice@blockitem\iffalse}}

\def\choice@paraitem#1{%
\par\refstepcounter{choiceitem}\makechoicelabel#1}

\def\choiceparaitem{\@ifstar
{\choice@paraitem\iffalse}}

\NewEnviron{items}{%
\begingroup
\let\item\choicemesureitem
\setcounter{choiceitem}{0}%
\settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
\trivlist\item\relax
\parindent0pt
\setcounter{choiceitem}{0}%
\ifdim\choiceitemwidth<0.25\columnwidth
\choiceitemwidth=0.25\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else\ifdim\choiceitemwidth<0.5\columnwidth
\choiceitemwidth=0.5\columnwidth
\let\item\choiceblockitem
\bgroup\BODY\hfill\egroup
\else % \choiceitemwidth > 0.5\columnwidth
\let\item\choiceparaitem
\BODY
\fi\fi
\endtrivlist
}
\makeatother

\begin{document}

\begin{questions}
\question Why is there air?
\begin{items}
\item* To blow up volleyballs.
\item  To fill an unneeded void.
\end{items}

\question text text text text text text text text text text text text text text text text text text text text
\begin{items}
\item*   foo
\item   bar
\item   baz
\end{items}

\question text text text text text text text text text text text text text text text text text text text text
\begin{items}
\item*foo
\item bar
\item*baz
\item text text text text text text text
\end{items}

\question text text text text text text text text text text text text text text text text text text text text
\begin{items}
\item*foo
\item bar
\item*baz
\item text text text text text text text text text text text text text text text text text text text text text text text text text
\end{items}

\end{questions}
\end{document}


-