# Forming totals in cell of tabular environment. Macro expansion or inline eval possible for simple arithmetic?

I have a tabular environment with a dozen or so itemized lines, each with a numeric integer cost. I would like to display a total in the last row. I've edited items in the table several times over a period of time, each time tediously recomputing the total.

Is there any way to have inline evaluation for a simple arithmetic formula, e.g. something akin to bash's $(()): The total is$((12 + 34 + 23 + ... + 5)) where the evaluated expression would be shown, rather than the literal equation. I'd be happy with changing an item's line value and also change the corresponding number in the equation (I don't think I need full spreadsheet expressiveness here).

I'd also rather not add a preprocessing step to generate the table from a template. The size of the table does not warrant that yet.

I figure there has to be a simple builtin way in tex to display the result of a calculation defined inline with the source document. By abusing a custom counter maybe, or by defining a new named length, adding units to it, and then displaying its value?

Trying to achieve something like this (pseudocode):

% initialize the current total to 0 before the table
{\thetotal = 0}

\begin{tabular}{@{}lp{4cm}r@{}} \toprule
Store & Purpose & Items\\
\midrule
Safeway & for the salad   &    10 tomatoes  {\thetotal += 10} \\
Whole Foods & for dessert &     4 muffins   {\thetotal += 4}  \\
% ...
% many more rows here.
% in each row, \thetotal is incremented. This way, if I change
% one line's value, I can change just that line and I don't need
% to change anything else.
% ...
Home Depot & leftovers &     17 mouse traps {\thetotal += 17} \\
\bottomrule
% Lastly, show the value of the counter at that point in the document
\multicolumn{2}{r}{Total} & \thetotal items
\end{tabular}


I would like the last row's last column to show the running tally at that point in the document, i.e., Total 31 in this example.

• it isn't clear how your inline expression question relates to table cells, you can do \the\numexpr 12 + 34 + 23 + 5\relax to typeset 74 but do you need to pick the values up from a table? Apr 11, 2017 at 23:02
• I think this is what I need! As I said, I'm fine with some local duplication of numbers. Could I, next to each row, add to \numexpr, and display the tally on the last row? Apr 12, 2017 at 0:20
• made question more concrete with an example Apr 12, 2017 at 0:30
• @init_js: What should the last columns entries be? 10, 14, 31?
– Werner
Apr 12, 2017 at 3:48
• Maybe you can have a look at the spreadtab package. Apr 12, 2017 at 8:27

Since only integer values are involved, the \addtocounter command should be sufficient.

I slightly changed the table and added the \additems command, that performs the calculation automatically. The final table design is left to the O.P.

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}
\newcounter{total}
\num{#1} & #2 &   \addtocounter{total}{#1} \thetotal
}

\begin{document}

\begin{tabular}{@{}lp{4cm}rlr@{}} \toprule
Store & Purpose & \multicolumn{3}{c}{Items} \tabularnewline
\midrule
Whole Foods & for dessert &    \additem{4}{muffins}   \tabularnewline
Home Depot & leftovers &     \additem{17}{mouse traps} \tabularnewline
\bottomrule
\multicolumn{4}{r}{Total} & \thetotal\ items
\end{tabular}

\end{document}


Another version without running sum

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}
\newcounter{total}
}

\begin{document}

\begin{tabular}{@{}lp{4cm}rl@{}} \toprule
Store & Purpose & \multicolumn{2}{c}{Items} \tabularnewline
\midrule
Whole Foods & for dessert &    \additem{4}{muffins}   \tabularnewline
Home Depot & leftovers &     \additem{17}{mouse traps} \tabularnewline
\bottomrule
\multicolumn{2}{r}{Total} & \thetotal & %items
\end{tabular}

\end{document}


• Unfortunately 10 + 14 + 31 ≠ 31.
– Werner
Apr 12, 2017 at 3:43
• @Werner: The last column is the accumulated sum
– user31729
Apr 12, 2017 at 3:43
• Nicely put. The last non-running-total table is closer to what I had in mind, but I guess doing running totals where the counter approach shines over a single \numexpr 10 + 4 + 17 \relax at the end. Apr 18, 2017 at 19:54
• @init_js: \numexpr is an eTeX extension (which is activated for every up-to-date TeX distribution most likely anyway), but you still have to store the values to add in between, in order to retrieve them later on to inject them the to \numexpr primitive? Is that easier? Don't forgot that you would have to store the individual numbers globally since you're using them in table cell, which is a group. So this is exactly what \addtocounter does, a global addition if the local number is needed only for the summation but not for other purposes other than display later on.
– user31729
Apr 18, 2017 at 20:02
• This is perhaps a different question, but for floating point, would you abuse integer counters (e.g. $1.30 +$2.70 ~= (130 + 270) and move that period when displaying), or is there another arcane trick? I sensed you had something in mind. Apr 18, 2017 at 20:18