Org-mode, table coordinates and formulae

Org tables support coordinates. $ represents columns, and @ represents rows. @x$y represents column x, row y. Remember that @ comes before $.

| @1$1 | @1$2 | @1$3 | @1$4 |
| @2$1 | @2$2 | @2$3 | @2$4 |
| @3$1 | @3$2 | @3$3 | @3$4 |
| @4$1 | @4$2 | @4$3 | @4$4 |
| @5$1 | @5$2 | @5$3 | @5$4 |

Looking back at the example from the previous article:

| Fruits | Amount |
|--------+--------|
| Apple  |      2 |
| Banana |      3 |
| Pear   |      4 |
|--------+--------|
| Total  |        |

The amount of apples is @2$2, bananas are @3$2 and pears are @4$2. The dividers in this table don't take up coordinates, but allow the ability to specify ranges between them. Ranges are defined with Roman numerals, so the first is @I and the second is @II. The range between them is @I..II. The number of fruits in this table is the sum of @I..II$2. Org uses vsum() to add all values in a range, and we can use that to embed a formula.

| Fruits | Amount |
|--------+--------|
| Apple  |      2 |
| Banana |      3 |
| Pear   |      4 |
|--------+--------|
| Total  | :=vsum(@I..II$2)       |

Typing := followed by a formula will add it to the cell. Hold Ctrl and press C twice to run it, which will move the formula to the bottom of the table.

| Fruits | Amount |
|--------+--------|
| Apple  |      2 |
| Banana |      3 |
| Pear   |      4 |
|--------+--------|
| Total  |      9 |
#+TBLFM: @5$2=vsum(@I..II$2)

All formulae are stored in their own tag at the bottom of each table. This allows org-mode to keep track of them while still showing the answers to them in the correct cells. As one last tip, you can replace @5$2 in the formula definition with @>$2. @> is a shorthand for “The last cell in the column”, so when your table grows you won't have to redo it.

In the next article I will elaborate on using org-mode tables.