so, I have a longtabu table spanning several pages. The table has a header, that needs to be repeated on each page. Some of the columns are numerical amounts, and I would like to put a row presenting a running total at the top of each page, just below the header.
So, I started off of what I found here: How to print subtotals in each page of a long table? and modified the code a little to be able to sum floating point numbers (the modification is in one of the answers to that question).
Now, I figured out how to move the running total to the header, instead of the footer, of the page. Still, as I said, I need also the original header to show up at each page, before the running total. However, when I typeset the MWE that you can find below here, I see two problems in the generated PDF:
- The running total row is badly aligned
- after the end of the table, I get an additional page with just the header and the (badly aligned) running total, which is undesireable.
I understand (I think!) the cause of the second problem, but cannot find a solution (it is probably beyond my understanding of longtabu's innards). As for the first problem, I can't figure it out.
So, without further ado, here's the MWE:
\documentclass{article}
\usepackage{longtable}
\usepackage[textheight=9\baselineskip]{geometry}
\newcount\zzz
\def\foo{}
\def\zz#1{#1%
\global\advance\zzz#1\relax
\marks1 {\the\zzz}%
}
\marks1{0}
\newlength\yyy
\setlength{\yyy}{0pt}
\def\yy#1{#1
\global\addtolength{\yyy}{#1pt}\relax
\marks2 {\the\yyy}
}
\marks2{0}
\makeatletter
\def\LT@output{
\global\advance\c@LT@chunks\@M
\LT@bchunk
\edef\tmp{\topmarks1 }
\copy\LT@head\\
running total: & \ifx\tmp\@empty\else \botmarks1 \fi& \ifx\tmp\@empty\else \strip@pt\dimexpr\botmarks2 \fi
\LT@echunk
\setbox\LT@head\box\z@
\setbox\LT@foot\box\z@
\LT@get@widths
\global\advance\c@LT@chunks-\@M
\setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
\@makecol
\@outputpage
\global\vsize\@colroom
\copy\LT@head\nobreak
}
\makeatother
\begin{document}
\begin{longtable}{l|l|l}
Var&Val1&Val2 \endhead
xxxxxxxx&yyyyyyy&yyyyyy\\
xxxxzzzzzzzzzzzzzxx&yyyyyyy&yyyyyy\endfoot% not typeset but needs to be bigger than the foot that is used.
&&\endlastfoot
a & \zz{1} & \yy{0}\\
b & \zz{2} & \yy{0}\\
c & \zz{5} & \yy{0}\\
d & \zz{7} & \yy{0}\\
e & \zz{1} & \yy{0}\\
f & \zz{9} & \yy{0}\\
g & \zz{2} & \yy{0}\\
h & \zz{3} & \yy{0}\\
i & \zz{1} & \yy{1}\\
j & \zz{6} & \yy{6}\\
k & \zz{8} & \yy{8}\\
l & \zz{3} & \yy{3.2}\\
m & \zz{2} & \yy{2.67}\\
n & \zz{1} & \yy{1.1}\\
o & \zz{4} & \yy{4}\\
p & \zz{8} & \yy{8.05}\\
q & \zz{3} & \yy{3}
\end{longtable}
\end{document}
And here's a screenshot of the result:

Thanks in advance for any help that might come. Regards!