64

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{} \\
\hline
       & \colsum{} & \colsum{} & \colsum{} & \\
\end{tabular}

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

78

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:

\begin{spreadtab}{{tabular}{llll|l}}
          & @ 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) &
\end{spreadtab}

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

5
  • 10
    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. 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... 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 ;) Oct 7, 2010 at 12:47
  • 1
    @Fabian @Juan This question could get you started.
    – Caramdir
    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
    Dec 27, 2020 at 0:58
26

Try your luck with calctab, spreadtab or tabularcalc.

3
  • Good suggestions! Juan's answer did provide a direct solution. If I could select two accepted answers, I would. 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? 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
    Sep 17, 2018 at 6:17
15

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:

\documentclass{article}
\usepackage{sagetex}

\begin{document}

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:

\begin{sageblock}
    f(x) = exp(x) * sin(2*x)
\end{sageblock}

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

\end{document}
3
  • 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, 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
    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
    Oct 8, 2010 at 22:09
8

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.

4

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

3

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

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{functional}
\IgnoreSpacesOn
\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}
  }
}
\IgnoreSpacesOff
\begin{document}
\begin{tblr}{colspec={lrrr|r},process=\funcSum}
       & Foo       & Bar       & Baz       & \\
Small  &  5        &  3        &  11       & rowsum \\
Medium &  9        &  2        &  23       & rowsum \\
Large  & 13        & 15        &  44       & rowsum \\
\hline
       & colsum    & colsum    & colsum    & \\
\end{tblr}
\end{document}

enter image description here

You must log in to answer this question.

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