# How to prevent page break between section title and table

I have several consecutive subsections which only contain one longtable each, right after the subsection title. So it happens that some of the subsection titles are at the bottom of the page and the corresponding table on the next page, which is clearly ugly.

Can I prevent page breaks after the subsection titles in this case?

Of course I could do it manually for the problematic sections, but I am looking for a generic solution that I can apply to all such sections.

Here is a MWE that demonstrates the issue:

\documentclass{article}
\usepackage{longtable}

\begin{document}
\vspace*{17cm}
\section{First section}
\begin{longtable}{l}
row1\\\hline
row2\\\hline
row3
\end{longtable}
\end{document}


There are two problems, the main one being that longtable doesn't handle this well. (Blame the author.) The other problem is that even if longtable accurately detected the problem and tried to prevent a break there, the page before would be underfull and TeX wouldn't want to break before the section unless there was sufficient flexible glue to fill the space.

Probably the easiest thing to do is use needspace to check that there is enough space before the heading and force a page break if not. In theory this should be safe to put before every section and it'll only force the page break if needed. If it doesn't work you can blame a different package author, which is an advantage.

This puts the section head and the table on page 2.

\documentclass{article}
\usepackage{longtable,needspace}

\begin{document}
\vspace*{17cm}
\Needspace{5\baselineskip}
\section{First section}
\begin{longtable}{l}

• OK, I will blame the author of longtable :-) – nplatis Oct 1 '13 at 15:14
• +1 for taking the blame ;-) I will blame the author for not making longtable compatible with twocolumn though. Is there any plan David? :) – user11232 Oct 2 '13 at 0:15