25

This question led to a new package:
multiple-choice

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

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. long choice issue

\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}
4
  • What is a multiply choice? Oct 27, 2013 at 11:32
  • Have you seen the packages to write exams?
    – Fran
    Oct 27, 2013 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, 2013 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, 2013 at 13:25

5 Answers 5

14

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.

enter image description here

7
  • 1
    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, 2013 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, 2013 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} Oct 29, 2013 at 4:07
  • @minthao_2011 I'm not even considering multiline answers.
    – egreg
    Oct 29, 2013 at 7:24
  • 1
    It's three years ago about my original question, but today, I still have issues on how to Control the vertical space in multiple choice questions, can any guys have a look? Thanks.
    – ollydbg23
    Jan 2, 2017 at 6:56
6

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}

enter image description here

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 enter image description here and run the program. You can make many version.
6
  • 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, 2013 at 12:55
  • 1
    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, 2013 at 15:42
  • You can contact with the author. Oct 28, 2013 at 14:18
  • 1
    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, 2013 at 14:26
  • 1
    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, 2013 at 0:55
5

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}
5
  • Can I make a page which only contains the ansewrs of all questions? Oct 31, 2013 at 10:53
  • @minthao_2011: You will need to write some extra code for that.
    – Leo Liu
    Oct 31, 2013 at 14:39
  • Please help me. Oct 31, 2013 at 15:03
  • @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, 2013 at 15:05
  • @minthao_2011: answers package will help you for this purpose, I guess.
    – Leo Liu
    Oct 31, 2013 at 15:14
5

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}


\newlength\answerspace
\setlength\answerspace{0.5in}
\newcommand\dottedanswerline[1][{}]{%
  % One optional argument, the default value of which is empty.
  \unskip\linebreak[0]\enspace
  \hbox{}\nobreak\dotfill 
  \ifprintanswers
    \hbox to \answerspace{(\hfil#1\hfil)}%
  \else
    \hbox{(\hskip 0.5in)}%
  \fi
}% dottedanswerline


\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\ifprintanswers \xappto\theanswer{\thechoiceitem}\ignorespaces}%
  {\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\ifprintanswers\ignorespaces}%
  {\choice@blockitem\iffalse}}

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

\def\choiceparaitem{\@ifstar
  {\choice@paraitem\ifprintanswers\ignorespaces}%
  {\choice@paraitem\iffalse}}


\NewEnviron{items}{%
  \def\theanswer{}
  \begingroup
    \let\item\choicemesureitem
    \setcounter{choiceitem}{0}%
    \settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
  \endgroup \dottedanswerline[\theanswer]
  \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}

show answers

hide answers

0

@egreg @minthao_2011 I'm not even considering multiline answers.

I have a solution to this problem. Use minipage

    \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 
\begin{minipage}[t]{\textwidth}
    This is a very long question. This is a very long question. 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{minipage}
\choice0 
\begin{minipage}[t]{\textwidth}
    This is a very long question. This is a very long question. 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{minipage}
\choice0 
\begin{minipage}[t]{\textwidth}
    This is a very long question. This is a very long question. 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{minipage} 
\choice0 
\begin{minipage}[t]{\textwidth}
    This is a very long question. This is a very long question. 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{minipage}
\end{choices}
\end{document}

enter image description here

1
  • Writing such code especially using minipage for long choice makes inconsistent format of writing \choice0. My original idea was that the length should automatically determined by the typeset system, so one of the three kinds of typeset will be automatically chosen.
    – ollydbg23
    Jul 19, 2017 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .