17

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}
Header
\endhead\hline\hline
row1\\\hline
row2\\\hline
row3
\end{longtable}
\end{document}

1 Answer 1

22

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}
Header
\endhead\hline\hline
row1\\\hline
row2\\\hline
row3
\end{longtable}
\end{document}
5
  • OK, I will blame the author of longtable :-)
    – nplatis
    Oct 1, 2013 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, 2013 at 0:15
  • Thanks, that worked, but is there any way of adding it in automatically for each section heading? Even better if the amount of baselineskip could be automated.
    – abeverley
    Oct 5, 2016 at 19:54
  • Will the recommended implementation be any different for an ordinary table environment?
    – tvk
    Aug 6, 2021 at 14:09
  • @tvk table is a floating environment so completely different to longtable. But in general it is better to post a question as a question with a real example, not as a comment on an unrelated years-old answer. Aug 6, 2021 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .