3

I have a table, and I want one column to show a running total on each line, i.e., it has to show the total of the values of another column up to that row. I thought about accumulating the total into a length variable (the numbers are floating point) and use a command to add values to it.

So, when I used to use a longtable (not longtabu) for the table, it looked fine. Here's the example that worked correctly:

\documentclass[11pt,a4paper]{article}
\usepackage{tabu}
\usepackage{longtable}

\newlength\yyy
\setlength{\yyy}{0pt}
\newcommand{\yy}[1]{#1\global\addtolength{\yyy}{#1pt}\relax}

\begin{document}
\the\yyy
\begin{longtable}{l|r|r}
ID & Amount & Total\endhead
a & \yy{100} & \the\yyy\\
b & \yy{100} & \the\yyy\\
c & \yy{100} & \the\yyy
\end{longtable}

\end{document}

and its output (ignore the "pt", I haven't stripped it yet in this MWE): Correct result However, if I use longtabu (which I need to use for other reasons), the values in the third column start from 700. Here's the example that bugs:

\documentclass[11pt,a4paper]{article}
\usepackage{tabu}
\usepackage{longtable}

\newlength\yyy
\setlength{\yyy}{0pt}
\newcommand{\yy}[1]{#1\global\addtolength{\yyy}{#1pt}\relax}

\begin{document}
\the\yyy
\begin{longtabu} to \textwidth {X[l]X[r]X[r]}
ID & Amount & Total\endhead
a & \yy{100} & \the\yyy\\
b & \yy{100} & \the\yyy\\
c & \yy{100} & \the\yyy
\end{longtabu}

\end{document}

and the output I get: Result not working

Any idea of why this behaviour comes up? Thanks in advance.

2

A less sophisticated way to fix your problem (than finding out a way to check when longtabu is measuring) is simply to add

\global\yyy0pt\relax

inside the first cell of your tabular.

The computations will be redone each time but always start from the same zero initial value.

  • Thanks! Sometimes the simplest idea never come to mind - this does the job, at least in my case, since I calculate totals on a table-by-table basis. – risoldi May 26 '14 at 7:59
4
\global\addtolength

Isn't supported syntax in LaTeX (it will break if you load calc which is part of the core LaTeX distribution) however that isn't the problem. \longtabu (like tabularx) sets the body several times to find the column widths so if you make global assignments the effect is incremented depending on how many trial runs the package needs to find the right widths,

tabularx redefines footnotes during trials (otherwise you would get multiple footnotes for the same reason) so in tabularx you can test

 \ifx\@footnotetext\TX@trial@ftn

and if that is false you are on the final typesetting run so can do your global increment.

longtabu will have a similar redefinition but with probably a different internal name.

  • Thanks David for the answer. I had a look at tabu's docs and seem to understand that \iftabu@measuring should be able to tell me if tabu is still measuring or not. However I'm finding it difficult to use it in my \yy command, as the interpreter complains that the sequence \iftabu@measuring is not defined. Still working on it... – risoldi May 23 '14 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.