在 longtable/longtabu 中用空行填充最后一页

在 longtable/longtabu 中用空行填充最后一页

我正在从数据库自动生成 PDF,我想用空行填充最后一页。文档如下所示

\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}

问题是如何在不增加文档页数的情况下自动在两个评论之间创建尽可能多的空行?一个合适的近似值就足够了,例如 5 行而不是 6 行仍然足以达到此目的。预先创建 100 行并抑制溢出行也可能是可以接受的。

答案1

您可以使用它\pdfsavepos来查看桌脚落在哪里并生成适当数量的空白行。可能需要运行几次才能稳定下来...

在此处输入图片描述

\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}

相关内容