How to automatically align the four choices of a multiple choice question in exam?
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.
In Vietnam, the author Nguyen Huu Dien has a package. You can download at here http://www.mediafire.com/download/5mxmnzty4jm/dethitracnghiem.zip. Site home is http://nhdien.wordpress.com/
\documentclass[11pt]{vieexamdesign}
\usepackage{amsmath,amsxtra,latexsym, amssymb, amscd}
\usepackage[utf8]{vietnam}
\usepackage{pstricks-add}
\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''}
\madethi{110}
\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
{\sai{Wrong answer.}}
{\dung{Right answer.}}
{\sai{Wrong answer.}}
{\sai{Wrong answer.}}
\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}
\fbox{\bf Code of Test \made}
}
\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.
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}