# Prevent page breaks between item heading and item content

I have headings that appear before items in an enumerated list. The problem is that I think it looks ugly when a heading appears at the end of a page with the content of that heading on the next page. I'd like it so that at least one line of text from the item contents appears after a heading. I've tried \nopagebreak[4] (doesn't appear to do anything?) and begin{samepage} wrapping the heading and entire paragraph (left big empty spaces on pages). I'm hoping there's a nice way to solve it!

Here's sample code to reproduce the issue:

\documentclass[letterpaper]{article}
\usepackage{enumitem}
\usepackage{lipsum}
\begin{document}
\begin{enumerate}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\end{enumerate}
\end{document}

And an image of the problematic output (see bottom of page). In this case, I'd either like the heading to be pushed to the next page or at least the first line of text from the paragraph to be pulled onto the first page.

-
–  barbara beeton Feb 21 '14 at 21:38

The needspace package allows reserving vertical space before doing the next thing. If the space does not exist, it issues a page break. Here, I redefined \item to perform a \needspace before it. You can change the value I used (\baselineskip), but it works for your MWE.

\documentclass[letterpaper]{article}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{needspace}
\let\svitem\item
\def\item{\needspace{\baselineskip}\svitem}
\begin{document}
\begin{enumerate}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\end{enumerate}
\end{document}

It is possible that you might still get burned, since the lipsum paragraphs will also issue a \needspace and could break a page on that account. A better way would be as follows, where a separate \Hitem is defined which alone issues the \needspace:

\documentclass[letterpaper]{article}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{needspace}
\def\Hitem#1{\needspace{\baselineskip}\item[]{\Huge{#1}}}
\begin{document}
\begin{enumerate}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\item{\lipsum[1]}
\end{enumerate}
\end{document}
-
This seems to have done the trick for me, thanks very much! –  aardvarkk Feb 21 '14 at 20:07

A solution using the stackengine package: it's by construction that the "Heading" part cannot be separated from the first line of the content since it belongs to the first line. It also has the advantage to greatly simplify the code – unless you have specific reasons to type the heading with its own \item command:

\documentclass[a4paper]{article}
\usepackage{enumitem}
\usepackage{stackengine}
\usepackage{lipsum}
\usepackage[bottom=45mm]{geometry}
\clubpenalty = 3500
\setstackgap{S}{\baselineskip}

\begin{document}

\lipsum[1]