# How to prevent page break between section heading and longtabu table?

I have created a minimal working example (MWE) consisting of a section and a longtabu table.

\documentclass{report}

\usepackage{longtable,tabu}

\begin{document}
An initial line of text

\vskip 47.5em % Offset the section to close to the bottom of the page

\section{Test section}

\begin{longtabu}{l X}
\textbf{Column 1} & \textbf{Column 2} \\
\hline
\\
Cell 1 & Cell 2 \\
\end{longtabu}

\end{document}


In the compiled document, which becomes two pages long, the section heading ends up at the end of the first page while the table (longtabu) ends up at the other. Why doesn't LaTeX prevent that a page break is inserted directly after the section heading, and how can it be fixed? If LaTeX has to break the page before the table has been finished, I want the page break to be inserted before the section heading.

Ideally, I want to obtain the same result as if a clearpage had been inserted right before the section heading started, if there would otherwise have been a page break between the section heading and the table.

• Cant you just create a pagebreak \newpage  in front of this code \nonumsubsubsection{Translation} – Sander Van der Zeeuw Aug 20 '13 at 12:41
• @Sander: What would happen if the section instead would start in the middle of the page? Then adding a \newpage before the section would create a strange page break. I only want a page break before the section heading if it is really needed. – StrawberryFieldsForever Aug 20 '13 at 14:31
• Did you try something like \newrobustcmd{\nonumsubsubsection}[1]{\addtotoc{subsubsection}{#1}\needspace{2\baselineskip}\subsubsection*{#1}}? Requires needspace package. – karlkoeller Aug 20 '13 at 14:35
• @StrawberryFieldsForever Youre completely right. Forgot to think about that. – Sander Van der Zeeuw Aug 21 '13 at 8:19
• @StrawberryFieldsForever Can you please add a MWE including all loaded packages? – karlkoeller Aug 21 '13 at 15:18

Have a look at the needspace package. The functionality also exists in for example memoir. With it you can add something like: \needspace{5\baselineskip} before the headline. If not the given space is left on the page a page break will be inserted and in your case the header will end up on the next page.

• How do you know the right value to pass to the function? If it's too little it might not work every time and if it's too large it might create a page break when it doesn't need to. – StrawberryFieldsForever Oct 31 '13 at 20:09
• Yes, that is always the problem when using this approach. One has to make a judgement call. How far down on a page is one okay with this new thing being placed... – jonalv Nov 1 '13 at 9:01
• Generally, you'd have to calculate the height of the upper construct, then add enough to encompass the vertical skip in between and overlap a bit with the lower construct. You can do this in (La)TeX itself. --- In your case, though, the height of a section title is pretty much fixed and the right value for \needspace can be found experimentally. – mhelvens Nov 4 '13 at 13:27
• You also need to allow space for some table rows: guesstimate the minimal height of what longtable can leave on the first page, and ask for that plus the height of the subsection (including vertical space). For unbreakable tables, it's the whole thing of course. Usually I just eyeball it, though. In the rare cases that it fails when the document changes, it can be increased or decreased as needed. – alexis Nov 6 '13 at 11:23
• I ended up using a value that I had found was the least value for which LaTeX would not under any circumstances insert an unappropriate page break. – StrawberryFieldsForever Nov 11 '13 at 16:44
Just enclose your text in an unbreakable unit; before it issue a combination of glue and penalties that will fill the page if the unit has to go to the next one.


This works for your situation, and does not create awkward, unnecessary page breaks.

\documentclass{report}
\usepackage{longtable,tabu}

\newenvironment{absolutelynopagebreak}
{\par\nobreak\vfil\penalty0\vfilneg
\vtop\bgroup}
{\par\xdef\tpd{\the\prevdepth}\egroup
\prevdepth=\tpd}

\begin{document}
An initial line of text

\vskip 47.5em % Offset the section to close to the bottom of the page

\begin{absolutelynopagebreak}
\section{Test section}
\nopagebreak
\begin{longtabu}{l X}
\textbf{Column 1} & \textbf{Column 2} \\
\hline
\\
Cell 1 & Cell 2 \\
\end{longtabu}
\end{absolutelynopagebreak}

\end{document}

• If you try this on my MWE, it puts the section heading much further down on the page compared to if you had just used a \clearpage before the section heading. I want there to be no apparent difference between the two cases. – StrawberryFieldsForever Nov 6 '13 at 18:30

I have used a somewhat ugly trick in similar situations before. If you are sure that you always want the table and the headline on the same page then put them in for example a minipage then they can not be set on different pages. This is a rather ugly hack full of problems when you start editing things though cause that minipage is always gonna be there...

\documentclass{report}

\usepackage{longtable,tabu}

\begin{document}
An initial line of text

\vskip 47.5em % Offset the section to close to the bottom of the page
foo

\noindent
\begin{minipage}{\textwidth}
\section{Test section}

\begin{longtabu}{l X}
\textbf{Column 1} & \textbf{Column 2} \\
\hline
\\
Cell 1 & Cell 2 \\
\end{longtabu}
\end{minipage}

\end{document}

• If you try this on my MWE, it gives a very similar result as if you had just put a \clearpage right before where the section starts, which is quite good, although it would have been better if there was no difference at all. However, if you put additional content after the table, there is almost no spacing between the table and the additional content, which is not good. – StrawberryFieldsForever Nov 6 '13 at 18:25
• I have updated my MWE to take this aspect into account. – StrawberryFieldsForever Nov 6 '13 at 18:34
• Is it theoretically possible to not group the heading and the longtabu together semantically, but still not let them go on separate pages (no linebreak in between them)? – Zelphir Kaltstahl Jul 15 '16 at 17:43