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.

6 Answers 6


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.

  • 11
    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. Commented Oct 7, 2010 at 11:27
  • 1
    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... Commented Oct 7, 2010 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 ;) Commented Oct 7, 2010 at 12:47
  • 1
    @Fabian @Juan This question could get you started.
    – Caramdir
    Commented Oct 8, 2010 at 21:01
  • I've been using LaTeX since 1985 or so and this is the first time i've heard about texdoc. Wow. Thanks!
    – vy32
    Commented Dec 27, 2020 at 0:58

Try your luck with calctab, spreadtab or tabularcalc.

  • Good suggestions! Juan's answer did provide a direct solution. If I could select two accepted answers, I would. Commented Oct 7, 2010 at 12:01
  • 1
    I'm just gonna go ahead and comment on the choices of names here. Why aren't any of these packages named TeXcel? Commented May 16, 2018 at 15:16
  • @AndreasStorvikStrauman It could be because Excel is specific to Microsoft while the term spreadsheet is more generic.
    – Ébe Isaac
    Commented Sep 17, 2018 at 6:17

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

  • 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
    Commented Oct 8, 2010 at 21:58
  • 2
    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
    Commented Oct 8, 2010 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
    Commented Oct 8, 2010 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.


With tabularray package of version 2022B (2022-06-01), it is possible to do calculation in tables, by using process option of functional library:

\prgNewFunction \funcSum {} {
  \intStepOneInline {2} {\arabic{rowcount}-1} {
    \intZero \lTmpaInt
    \intStepOneInline {2} {\arabic{colcount}-1} {
      \intAdd \lTmpaInt {\cellGetText {##1} {####1}}
    \cellSetText {##1} {\expWhole{\arabic{colcount}}} {\intUse\lTmpaInt}
  \intStepOneInline {2} {\arabic{colcount}-1} {
    \intZero \lTmpaInt
    \intStepOneInline {2} {\arabic{rowcount}-1} {
      \intAdd \lTmpaInt {\cellGetText {####1} {##1}}
    \cellSetText {\expWhole{\arabic{rowcount}}} {##1} {\intUse\lTmpaInt}
       & Foo       & Bar       & Baz       & \\
Small  &  5        &  3        &  11       & rowsum \\
Medium &  9        &  2        &  23       & rowsum \\
Large  & 13        & 15        &  44       & rowsum \\
       & colsum    & colsum    & colsum    & \\

enter image description here


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .