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?

8
  • 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
    Commented Mar 22, 2023 at 12:39
  • 3
    You need \newcommand{\addtoinvoicesum}[1]{\ADD{\invoicesum}{#1}{\invoicesum}\GLOBALCOPY{\invoicesum}{\invoicesum}}
    – Marijn
    Commented Mar 22, 2023 at 12:41
  • 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
    Commented Mar 22, 2023 at 13:42
  • 1
    tex.stackexchange.com/questions/227142/… describes the issue (and a workaround), it is not the one I was looking for but it is still relevant here.
    – Marijn
    Commented Mar 22, 2023 at 13:48
  • 1
    tex.stackexchange.com/questions/511191/… is a bit more complete regarding the code provided in the answer.
    – Marijn
    Commented Mar 22, 2023 at 13:49

1 Answer 1

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

1
  • 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? Commented Mar 22, 2023 at 22:12

Not the answer you're looking for? Browse other questions tagged .