[Tex/LaTex] How to prevent page breaks in lists? (“Exam” class)

examlistspage-breaking

I'm using the exam class to prepare a test.

Unfortunately, the package allows page breaks inside the solution list of a question.
Considering this toy example:

\question Example of question. Choose a solution:
\begin{checkboxes}
\choice A
\choice B
\choice C
\CorrectChoice D
\end{checkboxes}

It could be that a pagebreak happens between \choice B and \choice C.
How could I prevent that?

Update

The solution proposed by Mico works unless we want to print the solutions to the test AND the last answer of a question is a \CorrectChoice.

Here's a MWE that reproduces the problem:

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{etoolbox}
\AtBeginEnvironment{checkboxes}{\par\medskip%
     \begin{minipage}{\textwidth}}
\AtEndEnvironment{checkboxes}{%
     \end{minipage}}


\CorrectChoiceEmphasis{}

\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
\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 3
\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
\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
\choice answer 1
\choice answer 2
\choice answer 3
\choice answer 4
\choice answer 5
\CorrectChoice answer 6
\end{checkboxes}
    
\end{questions}

\end{document}

I've solved it by following the 2nd suggestion given here, that is, surround what you don't want to be broken by \parbox. (For the records, using \samepage didn't work.)

Best Answer

I suggest you modify the checkboxes environment to place them in a LaTeX minipage automatically, as is done in the following MWE. Note that the amount of vertical spacing introduced by the \medskip command can be changed, e.g., to \smallskip or \bigskip, or whatever amount you prefer.

Addendum, posted 10/25/2011: The OP has noted that my originally-provided answer causes a problem if the list of choices ends with a \CorrectChoice option. The answer below is updated to incorporate this possibility; the solution consists of adding the line

\if@correctchoice \endgroup \fi

to the code in \AtEndEnvironment{...}.

The full MWE is:

\documentclass[answers]{exam}
\usepackage{etoolbox}
\AtBeginEnvironment{checkboxes}{%
   \par\medskip\begin{minipage}{\linewidth}}
\makeatletter
\AtEndEnvironment{checkboxes}{%
   \if@correctchoice \endgroup \fi%
   \end{minipage}}
\makeatother

\begin{document}
\begin{questions}

\question Random text for question 1.

Choose a solution: 
\begin{checkboxes}
\choice A
\choice B
\choice C
\CorrectChoice D
\end{checkboxes}
\end{questions}
\end{document}

Observe that this solution implements Werner's suggestion to use the commands \AtBeginEnvironment and \AtEndEnvironment commands provided by the etoolbox package.

Related Question