# How to prevent page breaks within between a question and its answer choices in Exam class?

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.

• 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. – John Kormylo Jan 23 '15 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 '16 at 5:23

\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}
\end{checkboxes}

\question Random text for question 2
\begin{checkboxes}
\end{checkboxes}

\question Random text for question 3
\begin{checkboxes}
\end{checkboxes}

\question Random text for question 4
\begin{checkboxes}
\end{checkboxes}

\question Random text for question 5
\begin{checkboxes}