1

I want to type some multiple choice questions. I have the following code. The problem is that in some questions I need options in one line and evenly spaced, and in some questions I need two in one line and evenly spaced. How can I do that. I know that there are some answers related to this but I tried to figure out from that how to change this code, but every time I get an error and I don't want to use a different code all together. It will be nice if I am able to do this in this very code.

Thanks for the help in advance.

\documentclass{article}
%--------------------------------------------------------------------
%--------------------------------------------------------------------
\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 0 em}%
       \def\choice{%
         \item
       } % choice
       \labelwidth\leftmargin\advance\labelwidth-\labelsep
       \topsep=0pt
       \partopsep=0pt
     }%
  }%
  {\endlist}
%--------------------------------------------------------------------------------
\def\CChoice{%
      \choice
      \addanswer{\theenumi}{\thechoice}%
    }
    \let\CChoice\CChoice
%    \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{enumerate}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \CChoice Ringo
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \CChoice Yellow
    \choice White
    \choice Socrates    
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
    \choice Green
    \choice Yellow
    \CChoice White
    \choice Socrates
  \end{choices}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?

  \begin{choices}
    \choice George
    \choice Paul
    \choice John
    \choice Ringo
  \end{choices}
\item What was the color of George Washinton's white horse?

  \begin{choices}
    \choice Green
    \choice Yellow
    \choice White
    \choice Socrates    
  \end{choices}
\end{enumerate}

\showanswers
\end{document}

The out put is the following

1

From this question: we create a multicolumn environment:

\makeatletter
\newenvironment{multichoices}[1][2]{%
\begin{multicols}{#1}}{%
\end{multicols}}
\makeatother

and use it in the choices environment. MWE:

\documentclass{article}
\usepackage{multicol}
%--------------------------------------------------------------------
%--------------------------------------------------------------------
\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 0 em}%
       \def\choice{%
         \item
       } % choice
       \labelwidth\leftmargin\advance\labelwidth-\labelsep
       \topsep=0pt
       \partopsep=0pt
     }%
  }%
  {\endlist}
%--------------------------------------------------------------------------------
\def\CChoice{%
      \choice
      \addanswer{\theenumi}{\thechoice}%
    }
    \let\CChoice\CChoice
%    \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
}

\makeatletter
\newenvironment{multichoices}[1][2]{%
\begin{multicols}{#1}}{%
\end{multicols}}
\makeatother

%--------------------------------------------------------------------
\begin{document}

\begin{enumerate}
\item One of these things is not like the others; one of these things
  is not the same.  Which one doesn't belong?
  \begin{choices}
   \begin{multichoices}[4]
    \choice George
    \choice Paul
    \choice John
    \CChoice Ringo
   \end{multichoices}
  \end{choices}
\item What was the color of George Washinton's white horse?
  \begin{choices}
   \begin{multichoices}[2]
    \choice Green
    \CChoice Yellow
    \choice White
    \choice Socrates    
   \end{multichoices}
  \end{choices}
\end{enumerate}
\end{document}

output

BTW: why not use the exam class?

  • Thanks for the help. Its part of a bigger project and the document class is book so I can add another class. – Kumarm Oct 29 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.