# longtabu: put running total of some columns under the header on each page

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%
\marks1 {\the\zzz}%
}
\marks1{0}

\newlength\yyy
\setlength{\yyy}{0pt}
\def\yy#1{#1
\marks2 {\the\yyy}
}
\marks2{0}

\makeatletter
\def\LT@output{
\LT@bchunk
\edef\tmp{\topmarks1 }
running total: & \ifx\tmp\@empty\else \botmarks1 \fi& \ifx\tmp\@empty\else \strip@pt\dimexpr\botmarks2 \fi
\LT@echunk
\setbox\LT@foot\box\z@
\LT@get@widths
\setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
\@makecol
\@outputpage
\global\vsize\@colroom
}
\makeatother

\begin{document}

\begin{longtable}{l|l|l}
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!

Ok, actually I found the answer. If you look at the question I started from (link provided above), you'll see that D. Carlisle had provided a more complete example. I had removed the whole first part of the if, which basically deals with the end of the table. Putting that back in (and adapting it) solved problem number 2. :) As for problem number 1, I still don't understand, but I settled for explicitly repeating the header line in my added chunk of table, that will be good enough for now. Here's the code:

\documentclass{article}

\usepackage{longtable}

\usepackage[textheight=9\baselineskip]{geometry}
\newcount\zzz
\def\foo{}
\def\zz#1{#1%
\marks1 {\the\zzz}%
}
\marks1{0}

\newlength\yyy
\setlength{\yyy}{0pt}
\def\yy#1{#1
\marks2 {\the\yyy}
}
\marks2{0}

\makeatletter
\def\LT@output{
\ifnum\outputpenalty <-\@Mi
\ifnum\outputpenalty > -\LT@end@pen
\LT@err{floats and marginpars not allowed in a longtable}\@ehc
\else
\LT@bchunk
Var&Val1&Val2 \\
running total: & \ifx\tmp\@empty\else \botmarks1 \fi& \ifx\tmp\@empty\else \strip@pt\dimexpr\botmarks2 \fi
\LT@echunk
\setbox\LT@foot\box\z@
\LT@get@widths
\setbox\z@\vbox{\unvbox\@cclv}%
\ifdim \ht\LT@lastfoot>\ht\LT@foot
\dimen@\pagegoal
\ifdim\dimen@<\ht\z@
\setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
\@makecol
\@outputpage
\fi
\fi
\global\@colroom\@colht
\global\vsize\@colht
%\vbox
{\unvbox\z@\nobreak\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
\fi
\else
\LT@bchunk
\edef\tmp{\topmarks1 }
Var&Val1&Val2 \\
running total: & \ifx\tmp\@empty\else \botmarks1 \fi& \ifx\tmp\@empty\else \strip@pt\dimexpr\botmarks2 \fi
\LT@echunk
\setbox\LT@foot\box\z@
\setbox\LT@lastfoot\box\z@
\LT@get@widths
\setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
\@makecol
\@outputpage
\global\vsize\@colroom
\fi
}
\makeatother

\begin{document}

\begin{longtable}{l|l|l}
xxxxzzzzzzzzzzzzzxx&yyyyyyy&yyyyyy\endfoot% not typeset but needs to be bigger than the foot that is used.
a & b & c \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}