# Fill last page with empty lines in longtable/longtabu

I am generating a PDF automatically from a database and I want to fill the last page up with empty rows. The document looks like

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}

\begin{document}
\begin{longtabu}{l|l|l}
normal & text & here \\
% add fillers until page is full
\hline
& & \\
\hline
& & \\
\hline
% table footer should just fit on the page
\end{longtabu}

\end{document}


The question is how to create as many empty rows between the two comments automatically without increasing the page number of the document? A decent approximation would be good enough, e.g. 5 rows instead of 6 would still be good enough for this purpose. Having to pre-create 100 rows and suppressing the overflowing ones would likely also be acceptable.

• If your table spans multiple pages, then you have an idea from the previous page(s) how many lines fit on each page of the table. If the lines are all about the same same height and if you know how many lines are on the last page of the table, you can estimate how many blank lines you need to include to get the table footer at the bottom of the page. Or is there something more complex at work that wasn't obvious from the example? Nov 3, 2014 at 18:55
• The programmatic glue is what I am missing, especially for the single page case. In some cases, rows will span more than one line. Nov 3, 2014 at 22:35
• please give us a more representative example of your table, so we can give you a better answer. Nov 4, 2014 at 2:47

## 1 Answer

You can use \pdfsavepos to see where the table foot landed and and generate an appropriate number of blank rows. It may take a few runs to stabilise...

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}

\makeatletter
\let\xwrite\write
\def\foo#1#2{\noalign{\gdef\tmp{}\xfoo{#1}{#2}\aftergroup\tmp}}

\def\xfoo#1#2{%
\ifnum#1>\z@
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\expandafter\gdef\expandafter\tmp\expandafter{\tmp#2}%
\xfoo{\numexpr#1-1\relax}{#2}}%
}
\makeatother
\begin{document}
%\tracingall

\ifx\zz\undefined
\def\zz{0}
\fi
\ifx\zpos\undefined
\else
% height in sp - target height of the foot
% / 2^16 to get space to fill in pt
% / 21 as roughly the height of a line+\hline + a bit for luck
\edef\zz{\the\numexpr(\zz+((\zpos-11598372)/65536)/21)\relax}
\write\csname @auxout\endcsname{\gdef\string\zz{\zz}}
\fi
\typeout{extra lines = \zz}
\begin{longtabu}{l|l|l}
THe&head&line\endhead
\pdfsavepos\write\csname @auxout\endcsname{\gdef\string\zpos{\the\pdflastypos}}
The&foot&line\endfoot
normal & text & here \\
% add fillers until page is full
\hline
\foo{\zz}{& & \\\hline}
& & \\
\hline
% table footer should just fit on the page
\end{longtabu}

\end{document}