I have an enumerate of questions. Each of those questions has some opening text and then a nested enumerate. I give an example below that results in a page-break in the middle of the second question in the PDF output. I mark with \newpage
where I would like the page-break to be (automatically) inserted. In this particular example, I want the first question to be on page 1, and the second and third to be on page 2 (just because they both happen to fit).
I also put my attempt commented out in below. I try to start a "samepage" environment right at the beginning of an \item
and I try to end it at the end of the (nested) enumerate environment. The code seems to work on a simpler example, but in the current example it fails with the error ! LaTeX Error: \begin{samepage} on input line 56 ended by \end{center}.
In any case, my approach seemed fragile anyway so I'm hoping there's a better solution.
I think the reason I find this hard is because of the way \item
works. If it were \item{contents}
then I think it would be easier to patch. That's why I tried to hack together an approach that hooks into the beginning of \item
and end of \enumerate
.
I'm looking for a solution that can patch \item
and base enumerate
(rather than using \newitem
or enumitem). i.e., I'm hoping that I can just drop some preamble code in.
There are related questions. For example, Page breaks within enumerated list is related but is not about nested enumerates and also the solution seems to use enumitem.
\documentclass{article}
\makeatletter
%% my attempt:
%\let\OldItem\item
%\renewcommand{\item}{\OldItem \ifnum \@listdepth=1 \begin{samepage}\fi}
%\AfterEndEnvironment{enumerate}{\ifnum \@listdepth=1 \end{samepage}\fi}
\makeatother
\begin{document}
\begin{enumerate}
\item 2 + 2 = ? \\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
\begin{enumerate}
\item 1
\item 2
\item 3
\item 4
\end{enumerate}
% I do not want to put this page break in manually:
% \newpage
\item Consider the joint probability distribution below:
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
\begin{center}
\begin{tabular}{|ccc|}
\hline
&
$X_{2}=0$ &
$X_{2}=1$\tabularnewline
\hline
$X_{1}=0$ &
$\frac{1}{10}$ &
$\frac{3}{10}$\tabularnewline
\hline
$X_{1}=1$ &
$\frac{2}{10}$ &
$\frac{4}{10}$\tabularnewline
\hline
\end{tabular}
\par\end{center}
What is $P(X_{1}=1)$?
\begin{enumerate}
\item I do not know.
\item 4
\item 17
\item 88
\end{enumerate}
\item 2 - 2 + 5 =
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
\begin{enumerate}
\item 1
\item 2
\item 3
\item 4
\end{enumerate}
\end{enumerate}
\end{document}
Best Answer
You could try the following. Don't use
\\
at the end of paragraphs. This only give underfull hbox messages.