TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes I would like LaTeX to automatically calculate sums (or other simple arithmetic) for me, eg. in a table. I would like to ensure that when I update the table, I don't accidentally forget to update some sums. I also wouldn't want to maintain the table in another program, eg. a spreadsheet, or use some external script to generate LaTeX output. Is this possible without writing a lot of macros and completely bloating the tabular syntax?

For example:

\begin{tabular}{l l l l | l}
       & Foo       & Bar       & Baz       & \\
Small  &  5        &  3        &  11       & \rowsum{} \\
Medium &  9        &  2        &  23       & \rowsum{} \\
Large  & 13        & 15        &  44       & \rowsum{} \\
       & \colsum{} & \colsum{} & \colsum{} & \\

Here, \rowsum and \colsum would somehow calculate the sum of their respective rows and columns, and I would not need to worry about calculating them by hand.

share|improve this question
up vote 50 down vote accepted

LaTeX is a typesetting system, and trying to use it for anything other than that will probably lead you to frustration at some point or another. Unless your table is really very simple, I think going for a spreadsheet and then exporting that to LaTeX is definitely the best way to go.

Now, having said that, for a simple table you can use, as Thorsten suggested, a package like spreadtab and write something like:

          & @ Foo      & @ Bar      & @ Baz      & \\
@ Small   & 5          & 3          & 11         & sum(b2:d2) \\
@ Medium  & 9          & 2          & 23         & sum(b3:d3) \\
@ Large   & 13         & 15         & 44         & sum(b4:d4) \\ \hline
          & sum(b2:b4) & sum(c2:c4) & sum(d2:d4) &

Run texdoc spreadtab on a command line to get its documentation and read the full details.

share|improve this answer
I haven't seen this package before. Not only is it neat, but the tricks it must pull to allow such a nice input syntax would be very interesting to study. – Will Robertson Oct 7 '10 at 11:27
This is exactly what I was looking for. Good point about the limitations of LaTeX, but sometimes you just need all the data in one place. And sometimes the LaTeX export of your favourite spreadsheet doesn't produce as nice output as you can get with LaTeX's typesetting functions -- when you update the table, your nice typesetting is gone... That could be another question -- how to effortlessly sync only the data from a spreadsheet to a LaTeX table... – Fabian Fagerholm Oct 7 '10 at 12:01
“That could be another question -- how to effortlessly sync only the data from a spreadsheet to a LaTeX table” -- indeed! I wanted to say more about that, but probably the question will pop up soon, one day or another ;) – Juan A. Navarro Oct 7 '10 at 12:47
@Fabian @Juan This question could get you started. – Caramdir Oct 8 '10 at 21:01

Try your luck with calctab, spreadtab or tabularcalc.

share|improve this answer
Good suggestions! Juan's answer did provide a direct solution. If I could select two accepted answers, I would. – Fabian Fagerholm Oct 7 '10 at 12:01

If you want symbolic calculations (instead of the very cool spreadsheet options Thorsten gave) both Sage and Mathematica have ways of mixing themselves with LaTeX.

Here's an example from the SageTex tutorial:



Using Sage\TeX, one can use Sage to compute things and put them into
your \LaTeX{} document. For example, there are
$\sage{number_of_partitions(1269)}$ integer partitions of $1269$.
You don't need to compute the number yourself, or even cut and paste
it from somewhere.

Here's some Sage code:

    f(x) = exp(x) * sin(2*x)

The second derivative of $f$ is

  \frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} \sage{f(x)} =
  \sage{diff(f, x, 2)(x)}.

Here's a plot of $f$ from $-1$ to $1$:

\sageplot{plot(f, -1, 1)}

share|improve this answer
This is not really an answer to the question posed by Fabian (so I won’t upvote it) but nevertheless something useful I didn’t know. So unofficially thank you for the post ;). – Caramdir Oct 8 '10 at 21:58
I know that it's almost off topic... but the terms in the OP's table could have been symbolic and the rows infinitely long, then my answer would have been perfect! – Simon Oct 8 '10 at 22:07
Also, the whole table could be read into the computer algebra system as a matrix, processed and outputted into table form. – Simon Oct 8 '10 at 22:09

If you're an Emacs user, you can try the Orgtbl minor mode (the part which handles the tables in Org-mode); see this example from the manual. Together with Emacs Calc you can make advanced spreatsheets in Emacs; see this section in the manual.

share|improve this answer

Sweave and R is another cool option. You could create a code chunk in an Rnw file that imports the table, calculates anything you want, and then writes the result as a tex table.

See Friedrich Leisch's notes and my own introduction

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.