# Odd results using FP package inside table environment

I'm trying to create a number of running totals using the FP package. I define a macro for adding numbers to a running total and then I define a macro to output the final value.

The problem I'm having is that I receive a different output from the totals macro depending on where I call it. If it's inside the table I receive the correct value however if it's outside then the value doubles.

Any ideas?

\documentclass[11pt]{report}

\usepackage{fp}
\usepackage{tabu,longtable}

\FPset\totalone{0}
\FPset\totaltwo{0}

\global\let\totalone\0
\global\let\totaltwo\0
#1
}

\def\ptotalone{%
\totalone
\FPset\0{0}\global\let\totalone\0
}

\def\ptotaltwo{%
\totaltwo
\FPset\0{0}\global\let\totaltwo\0
}

\begin{document}

\begin{table}[htbp]
\begin{longtabu}{c}
total one: \ptotalone
\end{longtabu}
\end{table}
total two: \ptotaltwo

\end{document}


Example output:

0.25
0.5
1
total one: 1.75
total two: 3.5

-
longtabu makes two passes over the tabular material. –  egreg Jan 24 '13 at 11:44
I seem to get the same results with both tabu and longtabu so I guess this is common to both? If this is the case what would be the best way to only increment the total on a single pass? –  user24909 Jan 24 '13 at 11:49
Not using tabu. –  egreg Jan 24 '13 at 11:57
Unfortunately I'm forced to use tabu to achieve the desired layout –  user24909 Jan 24 '13 at 15:13
Unfortunately you're out of luck. As far as I can tell, there's no conditional that's set when doing the last pass. –  egreg Jan 24 '13 at 15:16

tabu inherits some code from tabularx to disable \write during its trial runs, so you can just do your arithmetic on the final run when \write has its normal value.

\documentclass[11pt]{report}

\usepackage{fp}
\usepackage{tabu,longtable}

\FPset\totalone{0}
\FPset\totaltwo{0}
\let\origwrite\write

\relax\ifx\origwrite\write
\global\let\totalone\0
\global\let\totaltwo\0
\fi
#1
}

\def\ptotalone{%
\totalone
\FPset\0{0}\global\let\totalone\0
}

\def\ptotaltwo{%
\totaltwo
\FPset\0{0}\global\let\totaltwo\0
}

\begin{document}

\begin{table}[htbp]
\begin{longtabu}{c}
total one: \ptotalone
\end{longtabu}
\end{table}
total two: \ptotaltwo

\end{document}

-
Nice, but I'd prefer that tabularx (and tabu) had a proper switch like \ifmeasuring@ in amsmath for distinguishing between the two stages, rather than relying on \ifx\origwrite\write. –  egreg Oct 18 '13 at 15:13
@egreg I could do something about one of those:-) –  David Carlisle Oct 18 '13 at 15:19

The tabu (and so also longtabu) environments do two passes over the material, so your operations are performed twice. Unfortunately there doesn't seem to exist a conditional that's set in only one of the passes in order to do register settings or assignments only once (similar to \ifmeasuring@ of amsmath).

So the answer seems to be: don't use tabu. Note that, as said by the author, the next version of tabu will not guarantee back compatibility, so I can't recommend using it (the author answered me saying, about back compatibility, "I don't care").

-