# Stopping table legend running over end of page when using longtable

My tables are formatted with a title at the top and a sometimes long legend at the bottom.

When using a longtable, because there can be a page break at any point of the table, it is possible to have the end of the table at the bottom of the page resulting in the legend running off the page entirely.

Does anyone know how to solve this? My only workaround at the moment is simply forcing the table to begin at such a point of one page that the legend fits correctly where it is displayed - I would rather a solution that did this automatically.

Thank you.

\documentclass[12pt]{memoir}

\usepackage{lipsum}
\usepackage{longtable}

\begin{document}

\lipsum[1-2] %% chosen to force the legend to run off the page

\begin{center}
\begin{longtable}{|c|c|c|c|}
\caption{A simple longtable example}\\ %% TABLE TITLE
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\hline
\caption*{\lipsum[1]} %% TABLE LEGEND
\endlastfoot
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
\end{longtable}
\end{center}

\end{document}

• Off-topic but somewhat relevant: There's no need to encase a longtable environment inside a center environment.
– Mico
Sep 23, 2018 at 15:07
• Possible duplicate of tex.stackexchange.com/questions/359903/… Sep 23, 2018 at 15:16
• @Mico it's not just that there is no need, it is wrong, it doesn't centre it and it adds spurious vertical space. Sep 23, 2018 at 15:56

I think the large foot going off page must count as a bug, however it works better if you place it in the table body and have an empty foot. The answer then ought to be that you should use \\* for the last few rows (depending on the minimum number of rows that you want to always carry over with the final legend). Unfortunately though you have a \hline and longtable's hline always allows a page break so here i have used an internal version that does not.

Unrelated but never put a longtablein acenterenvironment (orflushleft/flushright`) so I removed that.

\documentclass[12pt]{memoir}

\usepackage{lipsum}
\usepackage{longtable}

\begin{document}

\lipsum[1-2] %% chosen to force the legend to run off the page

\begin{longtable}{|c|c|c|c|}
\caption{A simple longtable example}\\ %% TABLE TITLE
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\endlastfoot
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\* 1 & 2 & 3 & 4 \\*
%\hline
\nopagebreak
\multispan{4}\unskip\leaders\hrule height\arrayrulewidth\hfill\cr % no break \hline
\nopagebreak
\caption*{\lipsum[1]} %% TABLE LEGEND
\end{longtable}

\end{document}
• This is really helpful, however, ff a table has quite wide rows this method means you can end up with a (possibly) really big gap on the page with the page break on. Is there any way of putting the page break in the legend? Sep 23, 2018 at 18:10
• @mr_phd lt can not break mid row, you could take the legend out of the table and just set it as a paragraph after the table (even normal \caption boxes its content so would stop page breaking) Sep 23, 2018 at 18:46