12

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
    \\
    \endhead
    Cell 1 & Cell 2 \\
\end{longtabu}

Additional content

\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.

11
  • Cant you just create a pagebreak \newpage in front of this code \nonumsubsubsection{Translation} Aug 20, 2013 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. Aug 20, 2013 at 14:31
  • 1
    Did you try something like \newrobustcmd{\nonumsubsubsection}[1]{\addtotoc{subsubsection}{#1}\needspace{2\baselineskip}\subsubsection*{#1}}? Requires needspace package. Aug 20, 2013 at 14:35
  • @StrawberryFieldsForever Youre completely right. Forgot to think about that. Aug 21, 2013 at 8:19
  • 1
    @StrawberryFieldsForever Can you please add a MWE including all loaded packages? Aug 21, 2013 at 15:18

3 Answers 3

8
+50

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.

6
  • 1
    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. Oct 31, 2013 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, 2013 at 9:01
  • 1
    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, 2013 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, 2013 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. Nov 11, 2013 at 16:44
2

From Absolutely, definitely, preventing page break:

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
    \\
    \endhead
    Cell 1 & Cell 2 \\
\end{longtabu}
\end{absolutelynopagebreak}

\end{document}
1
  • 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. Nov 6, 2013 at 18:30
1

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
        \\
        \endhead
        Cell 1 & Cell 2 \\
\end{longtabu}
\end{minipage}

\end{document}
3
  • 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. Nov 6, 2013 at 18:25
  • I have updated my MWE to take this aspect into account. Nov 6, 2013 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)? Jul 15, 2016 at 17:43

You must log in to answer this question.

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