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.

David Carlisle
  • 757,742

6 Answers6

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.

David Carlisle
  • 757,742
Juan A. Navarro
  • 62,139
  • 32
  • 140
  • 169
  • 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. – Will Robertson Oct 07 '10 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... – Fabian Fagerholm Oct 07 '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 07 '10 at 12:47
  • 1
    @Fabian @Juan This question could get you started. – Caramdir Oct 08 '10 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 '20 at 00:58
26

Try your luck with calctab, spreadtab or tabularcalc.

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}
Simon
  • 4,262
  • 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 08 '10 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 08 '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 08 '10 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

Jeromy Anglim
  • 7,450
  • 6
  • 46
  • 63
4

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

L.J.R.
  • 10,932