Document ClassesHow do I keep exam choice options together on the same page?

Information and discussion about specific document classes and how to create your own document classes.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

How do I keep exam choice options together on the same page?

Post by LavaTyper »

1. How do I keep exam choice options together on the same page?

2. And how do I change the spacing between adjacent choices?

I already went through the exam document class documentation and it did not answer my questions.

Code: Select all

\documentclass[answers, 10pt]{exam}
\usepackage[margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{indentfirst}
\usepackage{enumerate}

\linespread{1}

\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}


\begin{document}\pagestyle{empty}\raggedright

\lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[2] 



\begin{questions}

\question Which of the following is an example of the correct answer?

\begin{choices}
\CorrectChoice first answer
\choice second answer
\choice third answer
\choice fourth answer
\choice fifth answer
\end{choices}


\question Which of the following is an example of the correct answer?

\begin{choices}
\CorrectChoice first answer
\choice second answer
\choice third answer % This should be kept together with the first two choices
\choice fourth answer
\choice fifth answer
\end{choices}


\end{questions}






\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I keep exam choice options together on the same page?

Post by Johannes_B »

It is right there behind the choices environment including an example.

Code: Select all

\documentclass[answers, 10pt]{exam}
%\usepackage[margin=0.75in]{geometry}
\usepackage{lipsum}
%\usepackage{indentfirst}
%\usepackage{enumerate}

%\linespread{1}
%
%\renewcommand{\rmdefault}{phv}
%\renewcommand{\sfdefault}{phv}


\begin{document}
\pagestyle{empty}
%\raggedright<-Gets you into trouble

\lipsum[1-5]

\begin{questions}

\question Which of the following is an example of the correct answer?

\begin{oneparchoices}
\CorrectChoice first answer
\choice second answer
\choice third answer
\choice fourth answer
\end{oneparchoices}

\question Which of the following is an example of the correct answer?

\begin{choices}
\CorrectChoice first answer
\choice second answer
\choice third answer % This should be kept together with the first two choices
\choice fourth answer
\choice fifth answer
\end{choices}

\end{questions}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Re: How do I keep exam choice options together on the same p

Post by LavaTyper »

Johannes_B, see my comments below:

1. How do I keep exam choice options together on the same page? (I did not ask how to condense all the options into one line; the actual exam will have short statements for response options and so I will need one choice per line.)

2. And how do I change the spacing between adjacent choices? (The space between A and B should be single spaced apart, and instead it is more than that, like, 1.5x the line spread.)

(Also ragged2e doesn't work a lot of the time for me, so that is why I use ragged right.)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

How do I keep exam choice options together on the same page?

Post by cgnieder »

LavaTyper wrote:1. How do I keep exam choice options together on the same page? (I did not ask how to condense all the options into one line; the actual exam will have short statements for response options and so I will need one choice per line.)
Interesting question. The usual idea would be to put it in a box since boxes never are broken at page boundaries. In LaTeX terms: put it in a {minipage}. This works but will separate the question from the choices:

Code: Select all

\question Which of the following is an example of the correct answer?

\noindent\begin{minipage}{\linewidth}
\begin{choices}
  \CorrectChoice first answer
  \choice second answer
  \choice third answer
  \choice fourth answer
  \choice fifth answer
\end{choices}
\end{minipage}
I can't say if that suffices for you.

For keeping the question and the possible answers together the logical idea would be to wrap the {minipage} around both

Code: Select all

\noindent\begin{minipage}{\linewidth}
\question Which of the following is an example of the correct answer?

\begin{choices}
  \CorrectChoice first answer
  \choice second answer
  \choice third answer
  \choice fourth answer
  \choice fifth answer
\end{choices}
\end{minipage}
but for reasons I don't yet understand this destroys the formatting (the list is then too close to the question).
LavaTyper wrote:(Also ragged2e doesn't work a lot of the time for me, so that is why I use ragged right.)
That's strange... loading ragged2e shouldn't change anything unless you use an option like \usepackage[document]{ragged2e}. However, whatever your problems with the package are IMHO it might be worth trying to solve them and use the package, anyway...

Regards
site moderator & package author
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: How do I keep exam choice options together on the same p

Post by Johannes_B »

Sorry, i missread the question. That happens.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

How do I keep exam choice options together on the same page?

Post by LavaTyper »

So, the exam class has no built-in option to keep choices together and I have to use a minipage for each question? That seems rather disappointing.

Well, what about this?
LavaTyper wrote:2. And how do I change the spacing between adjacent choices? (The space between A and B should be single spaced apart, and instead it is more than that, like, 1.5x the line spread.)
Any idea?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I keep exam choice options together on the same page?

Post by Johannes_B »

You can define your own environment and use this instead. It will safe you some work.

To the second thing a little qick and dirty hack:

Code: Select all

\documentclass[answers, 10pt]{exam}
\usepackage[margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{indentfirst}
\usepackage{enumerate}
\usepackage{etoolbox}
\patchcmd{\choices}{\topsep=0pt}{%
\topsep=0pt
\itemsep=42pt}{}{}


%\linespread{1}

\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\begin{document}\pagestyle{empty}\raggedright

\lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[2]


\begin{questions}

\begin{minipage}{\linewidth}
	\question Which of the following is an example of the correct answer?

	\begin{choices}
		\CorrectChoice first answer
		\choice second answer
		\choice third answer
		\choice fourth answer
		\choice fifth answer
	\end{choices}

\end{minipage}

\begin{minipage}{\linewidth}
	\question Which of the following is an example of the correct answer?

	\begin{choices}
		\CorrectChoice first answer
		\choice second answer
		\choice third answer % This should be kept together with the first two choices
		\choice fourth answer
		\choice fifth answer
	\end{choices}

\end{minipage}

\end{questions}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

How do I keep exam choice options together on the same page?

Post by LavaTyper »

That is not satisfying.

I have given up on the exam class and have gone back to using basic enumerate.

Johannes, you mentioned that using \raggedright "gets you into trouble" - well then how do I take care of the options in {enumerate}?

Also, how do I get the list items (choices) to have the same alignment even if one option continues onto a second line?

Code: Select all

\documentclass[10pt]{article}
\usepackage[showframe, margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{indentfirst}
\usepackage{enumerate}
\usepackage{ragged2e}

\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}



% LINE SPACING DEFINITIONS

\linespread{1}
\def\titlelinespace{6pt}
\def\itemindentadjustment{2em}
\def\itemspaceadjustment{-0.25em}


% QUESTION ENVIRONMENT

\newcounter{questionNumber}
\setcounter{questionNumber}{0}

\newenvironment{question}{\noindent
\begin{minipage}[t]{\textwidth}\vspace{0pt}
\addtocounter{questionNumber}{1}\arabic{questionNumber}.}
%
{\end{minipage}}

\newenvironment{questionoptions}{
\vspace{-1em}
\begin{enumerate}[(a)]
\setlength{\itemsep}{\itemspaceadjustment}
\setlength{\itemindent}{\itemindentadjustment}
}
%
{\vspace{1.3\baselineskip}\end{enumerate}}



% THE DOCUMENT
\begin{document}\pagestyle{empty}

\lipsum[1]



\begin{question}
I am testing a long question. Suppose that my students will be able to respond to it. Which of these will be the answer?

\begin{questionoptions}
\item not this one
\item definitely not this one
\item don't even think about selecting this one
\item well students will tend to select the long answer because it has all the details and looks undoubtedly sophisticated
\item this is correct
\end{questionoptions}
\end{question}


\begin{question}
What goes here?

\begin{questionoptions}
\item something
\item something
\item something
\item something
\item something
\end{questionoptions}
\end{question}


\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I keep exam choice options together on the same page?

Post by Johannes_B »

Package enumerate has its limitations. In the following code i used the more modern enumitem. The troubling raggedright was referring to the oneparchoices-environment. Both are incompatible with one another. There is no problem in the current example (at least i see none).

Code: Select all

\documentclass[10pt]{article}
\usepackage[showframe, margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{indentfirst}
%\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{ragged2e}

\renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\newlength{\titlelinespace}
\setlength{\titlelinespace}{6pt}
\newlength{\itemspaceadjustment}
\setlength{\itemspaceadjustment}{-.25em}
\newlength{\itemindentadjustment}
\setlength{\itemindentadjustment}{2em}

	\raggedright %<-

\newcounter{questionNumber}

\newenvironment{question}{\noindent
\begin{minipage}[t]{\textwidth}\vspace{0pt}
\refstepcounter{questionNumber}\arabic{questionNumber}.}
%
{\end{minipage}}

\newenvironment{questionoptions}[1][]{
\vspace{-1em}
\begin{enumerate}[itemsep=\itemspaceadjustment,leftmargin=*,labelindent=1em,label={(\alph*)},#1]
%\setlength{\itemsep}{\itemspaceadjustment}
%\setlength{\itemindent}{\itemindentadjustment}
}
%
{\vspace{1.3\baselineskip}\end{enumerate}}

\begin{document}\pagestyle{empty}
\lipsum[1]

\begin{question}
I am testing a long question. Suppose that my students will be able to respond to it. Which of these will be the answer?

\begin{questionoptions}
\item not this one
\item definitely not this one
\item don't even think about selecting this one
\item well students will tend to select the long answer because it has all the details and looks undoubtedly sophisticated
\item this is correct
\end{questionoptions}
\end{question}


\begin{question}
What goes here?

\begin{questionoptions}[noitemsep]
\item something
\item something
\item something
\item something
\item something
\end{questionoptions}
\end{question}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
StarValkyrie
Posts: 89
Joined: Fri Jan 24, 2014 12:42 am

How do I keep exam choice options together on the same page?

Post by StarValkyrie »

I have my exam.cls-based exam template in LyX set up to prevent page breaks between choices. My solution only works in LyX though because LyX is set up already so that you can automatically append a custom command (which I can't take credit for - I googled for it) to all instances of a particular environment - in this case, both "choice" and "correctchoice".

In Preamble, I put:

Code: Select all

\makeatletter
 \newcommand\mynobreakpar{\par\nobreak\@afterheading}
\makeatother
Then using the LyX Layout's RightDelim option, I have it add ~\mynobreakpar at the end of each choice line. That keeps the choices together. To prevent a page break between those answers and the question they belong to, I then have LyX append \pagebreak[3] after each \end{choices}.

This solution might or might not be reproducible without LyX by copying the relevant sections of code from the exam.cls that pertain to choices, choice and correctchoice environments and then inserting the commands - you'd have to play around with it and see.
Post Reply