# Longtable last page shows only a table header and bottom line

I Updated the MWE to address comments by David. Now I am using a CSV-file ('unknown number of records'). This give the same 'header-only/bottom line' (tested in Overleaf) on page last page. How to get this always working nicely (independent of number of records)?

I have a problem with longtable. A longtable sometimes produces an "empty" last table page (that is only a header and a bottom line, so no data lines).

(credits) I changed the MWE of this tex.stackexchange.com post to demonstrate the problem.

More in general: how can I get the longtable behaviour: "if the remaining number of records is less than x skip them to the next page even if the current page is not full". So then I can say do the last [2,3,4 or 5] (and total count is higher else we are in an endless loop) on a new page to avoid I get a ugly 1 item table on the last page.

MWE Last page screenshot (Overleaf generated):

MWE Code

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{demo.csv}
col
a
b
c
d
e
f
g
h
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
z1
\end{filecontents}
\usepackage{array}
\usepackage{setspace}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[english]{babel}
\usepackage[pangram]{blindtext}
\usepackage{lscape}
\begin{document}
\setstretch{1.03}
\blindtext[5]
\begin{landscape}
\begin{longtable}{>{\raggedright}p{7cm}>{\centering}p{7cm}}
\toprule
\midrule
\bottomrule
\endfoot
\endlastfoot
\DTLforeach*{dset}{\COL=col}%
{\COL & t\tabularnewline}%
\end{longtable}\end{landscape}
\end{document}


The posted code does not show the problem, and has a last row with Jack... as well as the header on the final page 3, but as documented in the longtable manual to keep rows together you can use \\* or (more strongly) put more rows into the \endlastfoot section. Also note that \sfamily does not take an argument so you should remove the {} group around the table.

Note that the issue of a final break at a rule is more likely with booktabs rules as they, by design, have vertical space around them, and in longtable this introduces additional breakpoints.

• Ok, I made a more advanced MWE, now with CSV-file/datatool. I still get this empty header/footer on the last page. How to get this always working nicely independent of the number of records? – robert Oct 17 '16 at 9:15
• @robert that's rather changed the question as the standard longtable answer to ensuring that at least (say) 5 rows stay on the last page is to put them in the \endlasthead but the new question is not really about longtable at all it's about datatool and whether it can do that (which I don't know, offhand. – David Carlisle Oct 17 '16 at 9:31
• OK, thanks for your contribution. The real question relates to datatool/longtable with empty header/footer. So beside taking an another MWE with errors (sfamily) and change that, I updated it to reflect my real issue. – robert Oct 17 '16 at 9:35

DataTool/LongTable best practice here!!

I found the issue. If you experience this issue, you need to make the following changes. Background is that the original MWE is always emiting a \tabularnewline after each row, also the last one. In the new approach this is outputed before the start of a new row (with exception of the first row).

Never use (as in MWE):

\DTLforeach*{dset}{\COL=col}%
{\COL & t\tabularnewline}%


But use:

\DTLforeach*{dset}{\COL=col}%
{\DTLiffirstrow{}{\tabularnewline}%
\COL & t}%