Sum of values spread across tables in document? (like in Word)
\documentclass{article}
\usepackage{array}
\def\q#1{\gdef\thisq{#1}#1}
\def\p#1{\gdef\thisp{#1}#1}
\def\total{0}
\makeatletter
\def\itemcost{%
\strip@pt\dimexpr\thisp\p@*\thisq\relax
\xdef\total{\strip@pt\dimexpr\total\p@+\thisp\p@*\thisq\relax}}
\makeatother
\begin{document}
Bla bla bla
\begin{tabular}{rcrr}
Quantity & Item & Price per item & Total price\\
\hline
\q{5} & Sandwich & \p{5.00} & \itemcost
\end{tabular}
More bla bla bla
\begin{tabular}{rcrr}
Quantity & Item & Price per item & Total price\\
\hline
\q{3} & Coke & \p{1.00} & \itemcost
\end{tabular}
Grand total: \total
\end{document}
If you are using tabularx
you need to only do the arithmetic on the final run, not when it is doing trial runs to calculate the widths.
\def\q#1{\gdef\thisq{#1}#1}
\def\p#1{\gdef\thisp{#1}#1}
\def\total{0}
\makeatletter
\let\normalwrite\write
\def\itemcost{%
\strip@pt\dimexpr\thisp\p@*\thisq\relax
\ifx\write\normalwrite\xdef\total{\strip@pt\dimexpr\total\p@+\thisp\p@*\thisq\relax}\fi}
\makeatother
\usepackage{tabularx}
Here's a solution the takes advantage of some tricks available through pgfmath
\documentclass{article}
\usepackage{pgfmath}
\pgfkeys{/pgf/number format/precision=2,
/pgf/number format/fixed zerofill=true}
\def\grandtotal{0}
\def\getgrandtotal{\pgfmathroundtozerofill{\grandtotal}\$\pgfmathresult}
%% #1=quantity
%% #2=description
%% #3=price per item
\def\receipt#1#2#3{%%
\par
\vspace{1.25\baselineskip}
\begin{tabular}{cp{1.5in}rr}
Quantity & Item & Price per item & Total price \\\hline
#1 & #2 & \$#3 &
\pgfmathparse{#1*#3}\pgfmathroundtozerofill{\pgfmathresult}\$\pgfmathresult
\pgfmathsetmacro{\grandtotal}{\pgfmathresult+\grandtotal}%%
\xdef\grandtotal{\grandtotal}%%
\end{tabular}
\vspace{1.25\baselineskip}
\par
}
\usepackage{lipsum}
\pagestyle{empty}
\begin{document}
Some random text.
\receipt{5}{Sandwich}{5.00}
Some more random text.
\receipt{3}{Coke}{1.00}
Grand total: \getgrandtotal
\end{document}
UPDATE
After seeing your comments about Arithmetic overflow
, here's a variation on the above pgfmath
approach which uses the fp
package together with siunitx
to make the numbers more readable:
\documentclass{article}
\usepackage{fp}
\usepackage{siunitx}
\def\grandtotal{0}
\def\currentAmount{}
\def\dollars#1{\SI[per-mode=symbol,group-separator={,}]{#1}[\$]{}}
\def\getgrandtotal{\dollars{\grandtotal}}
%% #1=quantity
%% #2=description
%% #3=price per item
\def\receipt#1#2#3{%%
\par
\vspace{1.25\baselineskip}
\begin{tabular}{cp{1.5in}rr}
Quantity & Item & Price per item & Total price \\\hline
#1 & #2 & \dollars{#3} &
\FPmul\currentAmount{#1}{#3}%%
\FPround\currentAmount{\currentAmount}{2}%%
\dollars{\currentAmount}%%
\FPadd\grandtotal{\currentAmount}{\grandtotal}%%
\FPround\grandtotal{\grandtotal}{2}%%
%% make the `\grandtotal` available outside of environment.
\xdef\grandtotal{\grandtotal}%%
\end{tabular}
\vspace{1.25\baselineskip}
\par
}
\begin{document}
Some random text.
\receipt{5}{Sandwich}{5.00}
Some more random text.
\receipt{3}{Coke}{1.00}
Some more random text.
\receipt{300}{Something expensive}{16179.52}
Grand total: \getgrandtotal
\end{document}
You can use the \STsavecell
macro of the spreadtab package. This macro permit to use the numerical value of a cell outside of the table.
Update
After seeing the comment of @cgnieder, I add an example based on the provided MWE
\documentclass{article}
\usepackage{spreadtab}
\begin{document}
Bla bla bla
\begin{spreadtab}[%
\STsavecell\TotFood{d2}]{{tabular}{cccc}}
@Quantity & @Item & @Price per item & @Total price\\
\hline
5 & @Sandwich & 5 & a2*c2
\end{spreadtab}
More bla bla bla
\begin{spreadtab}[%
\STsavecell\TotBevreage{d2}]{{tabular}{cccc}}
@Quantity & @Item & @Price per item & @Total price\\
\hline
3 & @Coke & 1 & a2*c2
\end{spreadtab}
\FPadd{\foo}{\TotFood}{\TotBevreage}
\FPclip{\result}{\foo}
Grand total: \FPprint{\result}
\end{document}