计算器在环境中不起作用

计算器在环境中不起作用

为什么calculator在环境中不起作用?

\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}

这导致:

在此处输入图片描述

这与环境中的设置长度问题有关吗?我该如何修复它?

答案1

如果您受限于calculator,则定义一个\GADD“全局加法”命令,因为对齐单元格形成组并calculator进行本地分配。

\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}

使用“更现代”的工具:

\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}

在此处输入图片描述

相关内容