9

I'd like to prevent page breaks between a question and its answer choices in the Exam class. There's a good solution to the problem of how to prevent page breaks within the answer choices here, but that question did not address how to prevent breaks between the question and the answers.

2
  • The easy solution is to put a single minipage around both the question and the choices. It isn't automatic, but it isn't exactly onerous either. Jan 23, 2015 at 22:09
  • I managed to implement the solution sugested by @Kormylo. The following code, adapts the right margin to the available width of questions or parts: ``` \begin{minipage}{\linewidth} aquí las preguntas o partes \end{minipage} ```
    – djnavas
    Mar 24, 2016 at 5:23

2 Answers 2

7

Add these lines

\usepackage{etoolbox}
\BeforeBeginEnvironment{checkboxes}{\par\nopagebreak\minipage{\linewidth}}
\AfterEndEnvironment{checkboxes}{\endminipage}

to the preamble to make question and checkboxes to go together..

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{etoolbox}
\usepackage{etoolbox}
\BeforeBeginEnvironment{checkboxes}{\par\nopagebreak\minipage{\linewidth}}
\AfterEndEnvironment{checkboxes}{\endminipage}

\CorrectChoiceEmphasis{}
\textheight3.4in   %% just for demo
\begin{document}
\begin{questions}
\question Random text for question 1
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 2
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\CorrectChoice answer 4
\choice answer 5
\choice answer 6
\end{checkboxes}

\question Random text for question 3
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 4
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\question Random text for question 5
\begin{checkboxes}
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}

\end{questions}

\end{document}
1
  • This works for the checkboxes environment. However, \question is not an environment. Is there a solution for avoiding page breaks between the question and - for example - \solutionorlines?
    – Black
    Jul 9, 2019 at 10:46
0

Since \question isn't an environment, there's not a good way to insert a \minipage before and \endminipage after every question. But \filbreak before \question does a pretty good job. As Frank Mittelbach notes on another answer, \filbreak inserts a low level of stretching (overcome by \fill) and a strong suggestion of pagebreaking. If LaTeX insists on breaking between a question stem and its options, hardcoding the minipage around it is still available. (Note 1)

The next challenge is that the \question command isn't provided at the top level of the exam class. It's only inside a questions environment. Luckily, a \questionshook command, expanded inside this environment, is provided. So putting this in the preamble seems to work:

\usepackage{etoolbox}
\appto{\questionshook}{\preto{\question}{\filbreak}}

Here is a complete working example, maybe not minimal, but long enough to stretch across several pages. (Note 2)

\documentclass{exam}
\title{Know your US States and Capitals!}

\usepackage{etoolbox}
\appto{\questionshook}{\preto{\question}{\filbreak}}

\begin{document}

\begin{questions}

\question What is the capital of California?
\begin{choices}
    \choice Los Angeles
    \choice Sacramento
    \choice San Francisco
    \choice San Diego
\end{choices}

\question Which city serves as the capital of Texas?
\begin{choices}
    \choice Dallas
    \choice Houston
    \choice Austin
    \choice San Antonio
\end{choices}

\question What is the capital of Florida?
\begin{choices}
    \choice Miami
    \choice Orlando
    \choice Jacksonville
    \choice Tallahassee
\end{choices}

\question Which city is the capital of New York?
\begin{choices}
    \choice Buffalo
    \choice Albany
    \choice New York City
    \choice Syracuse
\end{choices}

\question What is the capital of Nevada?
\begin{choices}
    \choice Reno
    \choice Henderson
    \choice Carson City
    \choice Las Vegas
\end{choices}

\question Which city serves as the capital of Colorado?
\begin{choices}
    \choice Denver
    \choice Boulder
    \choice Colorado Springs
    \choice Aurora
\end{choices}

\question What is the capital of Massachusetts?
\begin{choices}
    \choice Springfield
    \choice Boston
    \choice Worcester
    \choice Cambridge
\end{choices}

\question Which city is the capital of Washington state?
\begin{choices}
    \choice Seattle
    \choice Tacoma
    \choice Spokane
    \choice Olympia
\end{choices}

\question What is the capital of Hawaii?
\begin{choices}
    \choice Maui
    \choice Honolulu
    \choice Kauai
    \choice Hilo
\end{choices}

\question Which city serves as the capital of Ohio?
\begin{choices}
    \choice Cincinnati
    \choice Columbus
    \choice Cleveland
    \choice Dayton
\end{choices}

% Additional Questions (11-20)

\question What is the capital of Arizona?
\begin{choices}
    \choice Tucson
    \choice Mesa
    \choice Phoenix
    \choice Flagstaff
\end{choices}

\question Which city serves as the capital of Pennsylvania?
\begin{choices}
    \choice Philadelphia
    \choice Pittsburgh
    \choice Allentown
    \choice Harrisburg
\end{choices}

\question What is the capital of Illinois?
\begin{choices}
    \choice Chicago
    \choice Springfield
    \choice Rockford
    \choice Peoria
\end{choices}

\question Which city is the capital of Oregon?
\begin{choices}
    \choice Eugene
    \choice Salem
    \choice Portland
    \choice Bend
\end{choices}

\question What is the capital of Tennessee?
\begin{choices}
    \choice Memphis
    \choice Knoxville
    \choice Chattanooga
    \choice Nashville
\end{choices}

\question Which city serves as the capital of New Jersey?
\begin{choices}
    \choice Newark
    \choice Trenton
    \choice Jersey City
    \choice Atlantic City
\end{choices}

\question What is the capital of Wisconsin?
\begin{choices}
    \choice Milwaukee
    \choice Green Bay
    \choice Madison
    \choice Kenosha
\end{choices}

\question Which city is the capital of Minnesota?
\begin{choices}
    \choice Rochester
    \choice St. Paul
    \choice Duluth
    \choice Minneapolis
\end{choices}

\question What is the capital of Utah?
\begin{choices}
    \choice Salt Lake City
    \choice Provo
    \choice Ogden
    \choice St. George
\end{choices}

\question Which city serves as the capital of Maryland?
\begin{choices}
    \choice Baltimore
    \choice Annapolis
    \choice Rockville
    \choice Frederick
\end{choices}

\end{questions}    
\end{document}

Notes:

  1. LaTeX permits you to pretend a command is an environment. \begin{question} starts a group and expands \question, and (since \endquestion isn't defined) \end{question} ends the group and does nothing else. Kind of nice if you prefer to use environments.

  2. ChatGPT knows how to use the exam class!

You must log in to answer this question.

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