Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

\def\add#1{%
  \FPadd\0\totalone{#1}
  \global\let\totalone\0
  \FPadd\0\totaltwo{#1}  
  \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}
      \add{0.25} \\
      \add{0.5}  \\
      \add{1}    \\
      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
share|improve this question
    
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
2  
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
add comment

2 Answers

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").

share|improve this answer
add comment

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

\def\add#1{%
\relax\ifx\origwrite\write
  \FPadd\0\totalone{#1}
  \global\let\totalone\0
  \FPadd\0\totaltwo{#1}  
  \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}
     \add{0.25} \\
      \add{0.5}  \\
      \add{1}    \\
      total one: \ptotalone
    \end{longtabu}
  \end{table}
  total two: \ptotaltwo

\end{document}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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