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 solve what is clearly a name scoping problem and don't know how to do it. (This discussion is extracted from a poorly-formed question about the same code that I posted yesterday; I've asked to have that post deleted.)

I am trying to use the fp package to maintain a running total of a list that gets displayed in a table (the real application is somewhat more complicated, of course; what is below is a minimal [non-]working example.) The issue appears to be that the accumulator, \total, gets recreated as a local inside each row of the table [if I understood the comment of a poster on the previous question correctly].

\documentclass{article}
\usepackage[autolanguage]{numprint}
\usepackage{fp}

\FPset\total{0}
%
\def\item#1{%
 \FPadd\total\total{#1}
 $\FPprint{#1}$\\\\
}
%
\def\itotal{$\FPprint\total$}

\begin{document}
\begin{tabular}{l}
  \item{17.5}
  \item{12}
  \itotal
\end{tabular}

\end{document}

This code produces (ignoring spacing and the like)
17.5
12
0
What I want, of course, is
17.5
12
29.5
What's going on and how can I fix it?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

This happens because each entry in a tabular is typeset in its own group, and \FPadd does a local assignment. You can globalize it as follows:

\def\item#1{%
 \FPadd\0\total{#1}\global\let\total\0
 $\FPprint{#1}$\\\\
}
share|improve this answer
    
This solution works great, thanks - but how does it work? –  rogerl Jan 23 '11 at 21:27
    
@rogerl: It works by globally assigning the value in \0 to \total. @Harald: There's no need for \0. You can just write \global\let\total\total. –  TH. Jan 23 '11 at 21:48
    
@TH: Looking for education...\FPadd\0\total... would seem to me to be trying to put the result of some operation in \0. Does \0 have a special meaning? Where does one find all of this? –  rogerl Jan 23 '11 at 23:23
    
@rogerl: There is nothing special about \0, except that it is reserved for temporary use. So if you assign something to it, then call some macro not of your own design, and then expect to find the definition of \0 unchanged, you may be in for a surprise. Yes, what I do is to use \0 as a temporary variable to hold the result of adding #0 to \total, then I assign the result to \total with a global assignment. (You find all this primarily in Knuth's TeX book.) In any case, as TH pointed out, I could just have replaced both occurrences of \0 by \total. –  Harald Hanche-Olsen Jan 24 '11 at 0:08
\documentclass{article}
\usepackage[autolanguage]{numprint}
\usepackage{fp}

\FPset\total{0}
%
\def\Item#1{%
 \FPadd\total\total{#1}
 \xdef\total{\total}
 $\FPprint{#1}$\\\\
}
%
\def\itotal{$\FPprint\total$}

\begin{document}
\begin{tabular}{l}
  \Item{17.5}
  \Item{12}
  \itotal
\end{tabular}

\end{document}
share|improve this answer

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.