0

Prelude

I understand that org-mode has two distinct styles of tables. The two styles can be identified visually.

One looks like this

|     |     |     |
|     |     |     |
|     |     |     |
|     |     |     |
|     |     |     |

and the other looks like that

+-----+-----+-----+
|     |     |     |
+-----+-----+-----+
|     |     |     |
+-----+-----+-----+
|     |     |     |
+-----+-----+-----+

Aside from the cosmetic difference, how do I determine which of the two to use?

Specifically

In particular, suppose that the look I am after is neither of the above.

Suppose that I want to enter a mundane table of phone numbers and addresses.

|-------------+--------------+--------------|
| Name        |        Phone | Address      |
|-------------+--------------+--------------|
| Pat Green   | 519-555-0161 | 31 Main St   |
| Chris Brown | 915-555-0016 | 63 Broad Ave |
|-------------+--------------+--------------|

(Notice that the style I'm after looks like neither of the styles above.) Which of the two styles should I use, and why?

Next, suppose that I want a slightly less mundane table, but certainly one that in no way warrants using an actual spreadsheet program.

|----------+-----+-----+----------|
| Month    |  In | Out | In - Out |
|----------+-----+-----+----------|
| January  | 161 |  31 |          |
| February |  16 |  63 |          |
| March    |  82 |  10 |          |
|----------+-----+-----+----------|
| Total    | vsum| vsum|     vsum |
|----------+-----+-----+----------|

(Once again , notice that the style I'm after looks like neither of the styles above.)

I gathered that to enable ongoing recalculation after modifying a number (via C-c*(org-ctrl-c-star)), I need to use the style that makes it possible to append

#+TBLFM: @5$2=vsum($2)

Can you clarify?

1
  • 1
    No: Emacs has two styles, the Org mode style and the table.el style. Org mode only knows and cares about its own style tables (with some half-hearted support for exporting table.el tables more or less correctly).
    – NickD
    Commented Mar 6, 2023 at 19:36

2 Answers 2

0

In both cases, you want Org mode tables. You can have headers and separator lines exactly as you show and you can apply formulas using the spreadsheet facility of Org mode tables. Your Org mode table with the formulae line should look like this:

|----------+-----+-----+----------|
| Month    |  In | Out | In - Out |
|----------+-----+-----+----------|
| January  | 161 |  31 |          |
| February |  16 |  63 |          |
| March    |  82 |  10 |          |
|----------+-----+-----+----------|
| Total    | 259 | 104 |      155 |
|----------+-----+-----+----------|
#+TBLFM: @>$2=vsum(@II..III) :: @>$3 = vsum(@II..III) :: @>$4 = $2 - $3

In words: set the cell in the last row and the second column to be the sum of the values in the rows between the second and third separator lines and the (implicitly specified) second column; and similarly for the third column. Finally, set the cell on the last row and last column to be the difference between the cells in columns 2 and 3 on the same row (implicitly specified).

See Tables in the Org mode manual.


What Org mode tables do not provide is cells will multiple lines. You can type long text into table.el cells (including newlines) and the cells will expand to accommodate the text. See Text-based Tables in the Emacs manual.


Note that you can tell these two types of table apart by looking at the beginning and/or end of each row: Org mode tables have | both at the beginning and the end (even at the intersections of them with separator lines - aka hlines); they have + at the intersections of vertical and horizontal lines, but only inside the table. OTOH, table.el tables have `+' at every intersection of vertical and horizontal lines, even on the left and right boundaries.

0
0

Expanding on NickD's great answer, it's possible to add iterations (through ranges) on the left hand side of an assignment, and not only on the right hand side.

|----------+-----+-----+----------|
| Month    |  In | Out | In - Out |
|----------+-----+-----+----------|
| January  | 161 |  31 |          |
| February |  16 |  63 |          |
| March    |  82 |  10 |          |
|----------+-----+-----+----------|
| Total    |     |     |          |
|----------+-----+-----+----------|
#+TBLFM: @>$2..@>$4=vsum(@II..III)::@2$4..@4$4=$2-$3

With the table above, pressing C-c* will produce the table below.

|----------+-----+-----+----------|
| Month    |  In | Out | In - Out |
|----------+-----+-----+----------|
| January  | 161 |  31 |      130 |
| February |  16 |  63 |      -47 |
| March    |  82 |  10 |       72 |
|----------+-----+-----+----------|
| Total    | 259 | 104 |      155 |
|----------+-----+-----+----------|
#+TBLFM: @>$2..@>$4=vsum(@II..III)::@2$4..@4$4=$2-$3

Amazingly, the order of the range-assignments does not matter, which reveals that the underlying calculator is clever enough to build a DAG and execute the instructions following its topological ordering.

3
  • "...the order of the range-assignments does not matter, which reveals that the underlying calculator is clever enough to build a DAG and execute the instructions following its topological ordering": that's not true and you may need to re-evaluated the table repeatedly before the calculations converge. See [Updating the table ](orgmode.org/manual/Updating-the-table.html) in the manual and check the doc string of org-table-iterate.
    – NickD
    Commented Mar 6, 2023 at 23:15
  • @NickD I see. Yes, indeed. Since consecutive executions of (org-ctrl-c-star) can produce different results, we know that the equations dependency graph is allowed to have cycles. Still, since the outcome in the table above is the same whether the line is #+TBLFM: @>$2..@>$4=vsum(@II..III)::@2$4..@4$4=$2-$3 or #+TBLFM: @2$4..@4$4=$2-$3::@>$2..@>$4=vsum(@II..III) (only reversing the order) suggests that the required order is detected. Thoughts?
    – Sam7919
    Commented Mar 7, 2023 at 0:02
  • You can try the Org mode mailing list for discussion. As the Site Tour says: "This site is all about getting answers. It's not a discussion forum. There's no chit-chat. Just questions... ...and answers."
    – NickD
    Commented Mar 7, 2023 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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