3

Can somebody please change these codes for choices so that the choices show vertically in line and not in one paragraph?

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

You can achieve what you want adding \par before \choicelabel in the definition of the choices environment (I signaled the change with a %NEW mark in the code):

\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
      \par\choicelabel% NEW: \par added
      \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}

enter image description here

Perhaps you could be interested in the exam document class or in the exsheets package which offer ready to use features to build exams.

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.