4

Consider this example:

\documentclass{report}
\usepackage{lipsum}
\usepackage{longtable}
\begin{document}
\lipsum[1-4]

another paragraph

another paragraph

another paragraph

another paragraph

anoteher paragraph

another paragraph

another paragraph

%anoteher paragraph

%another paragraph

\begin{longtable}{lr}
  \caption{long table}\\
  A&B\\
  c&D
  \end{longtable}
\end{document}

When compiled, the caption of the long table is on one page, while the table itself on another page:

Enter image description here

How do I avoid such an awkward situation?

Comment on answers

An additional problem is introduced when using \hline immediately after the caption:

    \documentclass{report}
    \usepackage{lipsum}
    \usepackage{longtable}
    \usepackage{needspace}
    \begin{document}
    \lipsum[1-4]

    another paragraph

    another paragraph

    another paragraph

    another paragraph

    anoteher paragraph

    another paragraph

    another paragraph

    %anoteher paragraph

    %another paragraph
    %\Needspace{5\baselineskip}
    \begin{longtable}{lr}
      \caption{some table}\\*
      \hline
      A&B\\
      c&D\\
      E&F
      \end{longtable}
    \end{document}

Even when using \\* we revert to the initial state of affairs:

Enter image description here

It appears the problem can be solved by uncommenting the line %\Needspace{5\baselineskip}

However, it seems some manual adjustment is needed to \Needspace in the case of multiline captions.

Consider this example:

\documentclass{report}
\usepackage{lipsum}
\usepackage{longtable}
\usepackage{needspace}
\begin{document}
\lipsum[1-4]

another paragraph

another paragraph

another paragraph

another paragraph

anoteher paragraph

%another paragraph

%another paragraph

%anoteher paragraph

%another paragraph
\Needspace{5\baselineskip}
\begin{longtable}{lr}
  \caption{\lipsum[1][1-3]}\\*
  \hline
  A&B\\
  c&D\\
  E&F
  \end{longtable}
\end{document}

The result is again the same:

Enter image description here

This case is fixed by changing 5 in \Needspace{5\baselineskip} to 6.

Viesturs
  • 7,895

2 Answers2

8

Assuming you want to have at least 3 rows of the longtable to show up before considering a page break to be unacceptable, you could load the needspace package and issue the directive

\Needspace{5\baselineskip}

immediately before \begin{longtable}. That way, unless there are at least 5 lines of text still free at the bottom of the page, the longtable will start at the top of the next page.

Why 5\baselineskip? Because the longtable's caption and the blank line between the caption and the start of the body of the text take up 2 more lines.

Mico
  • 506,678
  • Please see a comment to your answer in my post. – Viesturs Mar 02 '19 at 18:54
  • @Viesturs - You wrote, "it seems some manual adjustment is needed to [the argument of] \Needspace in the case of multiline captions." Indeed: I had based my answer on the information you provided. If that information doesn't apply, a different, and quite likely larger, value should be used as the argument of \Needspace. – Mico Mar 02 '19 at 19:06
6

You can simply use \\* instead of \\ after the \caption. From the manual:

\\*: The same as \\ but disallows a page break after the row.


longtable has some macros to control headings (cf. manual section 3). Depending on your use-case, have a look at \endhead or \endfirsthead. This code works for me:

\documentclass{report}
\usepackage{lipsum}
\usepackage{longtable}

\begin{document}
\lipsum[1-4]

another paragraph

another paragraph

another paragraph

another paragraph

anoteher paragraph

\begin{longtable}{lr}
  \caption{\lipsum[1][1-3]}\\
  \hline
  \endfirsthead
  A&B\\
  c&D\\
  E&F
  \end{longtable}
\end{document}

enter image description here

Arash Esbati
  • 7,416