# Exam package: How can I stick part and solutionbox together?

I'm using the exam class and have the problem with the parts enviroment in combination with the solutionorbox enviroment. The part and the corresponding solutionorbox should be on the same page. Here is an example that demonstrate the problem:

\documentclass{exam}
\usepackage{blindtext}
\begin{document}
\begin{questions}
\titledquestion{Farben}
\begin{parts}
\part[4] Question 1 \blindtext
\begin{solutionorbox}[16cm]
\blindtext
\end{solutionorbox}
\part[4] Question 2 \blindtext
\begin{solutionorbox}[10cm]
\blindtext
\end{solutionorbox}
\end{parts}
\end{questions}
\end{document}


I have tried to modify the \part command by using etoolbox package but it does not work with:

\preto{\part}{\par\nobreak\vfil\penalty 9999\vfilneg\vtop\bgroup}


This is the part definition in the exam.cls:

\def\part{%
\@bonusfalse
\process@part
}%


How can I add a \newpage if needed that the question and the space for a solution is on the same page without adding the \newpage?

• I thik it is easier to use '\clearpage' between both parts, and get a second page started with the question two. May 2, 2019 at 23:49

Probably not really a good solution, but it's a start.

First of all, you have to move you \preto patch within the parts environment, because \part is defined there. Then, because \part now has a \bgroup, you have to end your item with an \egroup.

\documentclass{exam}
\usepackage{blindtext}
\usepackage{etoolbox}
\begin{document}
\begin{questions}
\titledquestion{Farben}
\begin{parts}
\preto{\part}{\par\nobreak\vfil\penalty 9999\vfilneg\vtop\bgroup}
\part[4] Question 1 \blindtext
\begin{solutionorbox}[16cm]
\blindtext
\end{solutionorbox}
\egroup
\part[4] Question 2 \blindtext
\begin{solutionorbox}[10cm]
\blindtext
\end{solutionorbox}
\egroup
\end{parts}
\end{questions}
\end{document}


You could try putting a \filbreak before each \part command. That tries to have all the page breaks be at a \filbreak command, but it doesn't break the page if there's room remaining for all the text up to the next \filbreak.