0

Why does the calculator not work in environments?

\documentclass{scrlttr2}

\usepackage{calculator}

\newcommand{\resetinvoicesum}{\ADD{0}{0}{\invoicesum}} \newcommand{\addtoinvoicesum}[1]{\ADD{\invoicesum}{#1}{\invoicesum}}

\begin{document}

\resetinvoicesum

\begin{tabular}{l}

    15.78\addtoinvoicesum{15.78}\\
    15.89\addtoinvoicesum{15.89}\\
    sum:\invoicesum

\end{tabular}

~\\
15.78\addtoinvoicesum{15.78}\\
15.89\addtoinvoicesum{15.89}\\
sum:\invoicesum

\end{document}

This leads to:

enter image description here

Is that related to the problem with settings lengths in environments? How do I fix it?

mrCarnivore
  • 1,505
  • 1
    It is probably not environments, but rather the fact that cells are groups, so unless \ADD does a global assignment you will never get the result you are after – daleif Mar 22 '23 at 12:39
  • 3
    You need \newcommand{\addtoinvoicesum}[1]{\ADD{\invoicesum}{#1}{\invoicesum}\GLOBALCOPY{\invoicesum}{\invoicesum}} – Marijn Mar 22 '23 at 12:41
  • @Marijn: Please add that as an answer and I will accept it. It works! – mrCarnivore Mar 22 '23 at 12:42
  • @Marijn: But it does not work with tabularx. Somehow the result is 2 or 3 times higher than expected... – mrCarnivore Mar 22 '23 at 13:35
  • 1
    @mrCarnivore that was not your question :) however, this is a known issue, tabularx processes the contents of the table multiple times (each time executing the \ADD statements) in order to get the layout right. There was a recent question about that, using a different calculation package but the same overall issue, let me see if I can find that. – Marijn Mar 22 '23 at 13:42
  • 1
    https://tex.stackexchange.com/questions/227142/tabularx-processes-its-body-multiple-times-how-do-i-know-which-cycle-i-am-in?noredirect=1&lq=1 describes the issue (and a workaround), it is not the one I was looking for but it is still relevant here. – Marijn Mar 22 '23 at 13:48
  • 1
    https://tex.stackexchange.com/questions/511191/etoolbox-and-tabularx-problem-with-appto/511224#511224 is a bit more complete regarding the code provided in the answer. – Marijn Mar 22 '23 at 13:49
  • @Marijn: Thank you. Sometimes I find a minimal example that shows the same behaviour than my original problem but the solution does not fit to the more complicated problem. I guess you have to understand the limits of LaTeX quite a bit to be able to frame a good minimal example. The solution you linked to seems to look fitting but is quite complex and needs to be adapted. I'll try to figure it out. Thanks! – mrCarnivore Mar 22 '23 at 14:49

1 Answers1

1

If you're tied to calculator, then define a \GADD command for “global addition”, because alignment cells form groups and calculator does local assignments.

\documentclass{scrlttr2}

\usepackage{calculator}

\newcommand{\GADD}[3]{% \ADD{#1}{#2}{\tmpcalc}\global\let#3\tmpcalc }

\newcommand{\resetinvoicesum}{\GADD{0}{0}{\invoicesum}} \newcommand{\addtoinvoicesum}[1]{\GADD{\invoicesum}{#1}{\invoicesum}}

\begin{document}

\resetinvoicesum

\begin{tabular}{l} 15.78\addtoinvoicesum{15.78}\ 15.89\addtoinvoicesum{15.89}\ sum: \invoicesum \end{tabular}

\end{document}

With “more modern” tools:

\documentclass{scrlttr2}

\ExplSyntaxOn

\NewDocumentCommand{\resetfpvar}{m} { \fp_zero_new:c { g_carnivore_fpvar_#1_fp } } \NewDocumentCommand{\addtofpvar}{mm} {% #1 = var name, #2 = amount \fp_gadd:cn { g_carnivore_fpvar_#1_fp } { #2 } } \NewExpandableDocumentCommand{\printfpvar}{m} { \fp_use:c { g_carnivore_fpvar_#1_fp } }

\ExplSyntaxOff

\newcommand{\resetinvoicesum}{\resetfpvar{invoicesum}} \newcommand{\addtoinvoicesum}[1]{\addtofpvar{invoicesum}{#1}}

\begin{document}

\resetinvoicesum

\begin{tabular}{l} 15.78\addtoinvoicesum{15.78}\ 15.89\addtoinvoicesum{15.89}\ sum: \printfpvar{invoicesum} \end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thank you for that alternative solution. In my real problem I want to use tabularx with which even the your alternative solution does not work. Do you also have an easy solution for that? Or should I ask a new question for that? – mrCarnivore Mar 22 '23 at 22:12