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.

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
  • 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
  • 1
    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 08:19
  • @SanderVanderZeeuw: I edited my question a bit after your answer — the original question was formulated as if I wanted to have a page break there no matter what, so that was actually my bad. – StrawberryFieldsForever Aug 21 '13 at 15:12
  • @karlkoeller: I don't know what your code does exactly, but it inserted a new paragraph with the text "baselineskip!" in it right before the subsubsection, and there is still a page break right after the subsubsection heading. – StrawberryFieldsForever Aug 21 '13 at 15:16
  • 1
    @StrawberryFieldsForever Can you please add a MWE including all loaded packages? – karlkoeller Aug 21 '13 at 15:18
  • @karlkoeller: I can add a MWE in the weekend. – StrawberryFieldsForever Aug 21 '13 at 16:25
  • @karlkoeller: There we go! – StrawberryFieldsForever Oct 31 '13 at 21:24
  • @karlkoeller: Should it not be first \needspace and then \addtotoc? Otherwise the entry in the Table of Contents could show one page before the real \subsubsection, I think. – Stephen Nov 05 '13 at 19:33
  • In theory, the right solution would be to adjust TeX's page-breaking penalty so that TeX is less eager to insert a page break after a section (I've always wondered why it's not higher). But longtable might be inserting its own, low penalties... so this needs more looking into. – alexis Nov 06 '13 at 11:14

3 Answers3

8

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.

jonalv
  • 11,466
  • 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. – 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 01 '13 at 09: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 04 '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 06 '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
  • But that's annoying! Every single time using a table needing to calculate or try the spacing? Or imagine some text way up the document changed and suddenly you need to change the spacings? Isn't there a generic (better) way for this? – Zelphir Kaltstahl Jul 15 '16 at 17:39
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}
David Carlisle
  • 757,742
  • 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 06 '13 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}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
jonalv
  • 11,466
  • 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 06 '13 at 18:25
  • I have updated my MWE to take this aspect into account. – StrawberryFieldsForever Nov 06 '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