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.


\the\numexpr 12 + 34 + 23 + 5\relaxto typeset 74 but do you need to pick the values up from a table? – David Carlisle Apr 11 '17 at 23:02Could I, next to each row, add to \numexpr, and display the tally on the last row?
– init_js Apr 12 '17 at 00:20spreadtabpackage. – egreg Apr 12 '17 at 08:27\numexpr) . Not sure if it allows display of table cells to be different than the number itself (e.g. display number 4 as "four"). edge case, admittedly. I'll give it a shot. If I end up writing a formal answer, I'll probably include both approaches.Anyone knows other quick and dirty tricks by defining custom lengths (I have a gut feeling there's a way there)?
– init_js Apr 12 '17 at 22:52