Skip to content

Emacs Org-Mode Table Snippets

Source: Org tutorial for tables - orgmode.org, james-stoup/org-mode-table-tutorial: A simplified guide to mastering Org Mode table functions · GitHub. Examples below are by james-stoup and under GPL-3.0 license

See Emacs org-mode shortcuts - Emacs Shortcuts for org-mode for table notation like @ symbol to denote rows and the $ symbol to denote columns

Restrict column width org-table-shrink to reduce column width given cookie length in table like <10>. Use org-table-expand to restore normal column width.

A very wide cellAnother very very wide cell

Create a formula by adding #+TBLFM: <insert formula here> to the end of any table.

Example: The formula should be read as “column 2 equals column 1 multiplied by 3”:

13
26
39
412
515

We need a way to tell the formula to always write to the last line, regardless of how many lines are in the table. What we need is the @> operator, that signifies the last row of the table.

Summing column with @>$2

ITEMCOSTPRICE
Bike50100
Sword2035
Drill3060
Cooler1070
TV5040
Blender2545
Boots1020
195

Summing all columns into last row:

ITEMCOSTPRICE
Bike50100
Sword2035
Drill3060
Cooler1070
TV5040
Blender2545
Boots2020
Bike + Sword + Drill + Cooler + TV + Blender + Boots205370

Summing only columns 2 and 3

ITEMCOSTPRICE
Bike50100
Sword2035
Drill3060
Cooler1070
TV5040
Blender2545
Boots2020
205370

Summing only columns 2 and beyond

ITEMCOSTPRICESHIPPINGMILEAGE
Bike50100023
Sword20351012
Drill3060551
Cooler1070032
TV50402019
Blender254509
Boots2020038
20537035184
PLAYERCMP%PASSING YARDSRUSHING YARDSTOTAL YARDSPASSING TDsRUSHING TDsTOTAL TDs
Joe Burrow69.84641202484342244
Lamar Jackson67.93955852480739443
Josh Allen63.637315314262281240
Jayden Daniels69.43530864439425631

The formula is smart enough to know that we don’t want to include something before the separator. However, it still isn’t correct. Even though it didn’t show the first value, it still incremented it, which leaves our starting value at 2. We can do better. Modify the formula to subtract one from each value to produce the result we want.

BOOKSREAD?
1ITyes
2The Hobbityes
3The Black Companyyes
4Salem’s Lotno
5Leaves of grassno

Horizontal Separators and Referencing Rows

Section titled “Horizontal Separators and Referencing Rows”

Horizontal lines can make your table much easier to read but they have a hidden use as well. You can specify all of the values between the first and second lines using @I..@II. These are NOT Roman numerals, so you must put IIIII for the 5th horizontal line, not V which would be the correct Roman numeral. In the example below you have decided to host a party and are trying to tally up the costs of different types of expenses.

ITEMCOST
rum20
gin18
beer50
coke10
sprite5
chips10
cookies20
pizza60
plates10
napkins8
cups12
ALCOHOL88
SODA15
FOOD90
MISC30

See Calc package for other math libraries info-display-manual and select calc.

The field marked AVERAGE GRADE should contain an average of the 3 tests and the final exam grades. The CLASS GRADE is computed similarly but the final exam is now weighted in respect to the other grades.

STUDENTTEST 1TEST 2TEST 3FINAL EXAMAVERAGE GRADECLASS GRADE
Alice8993757783.582.2
Bob7899698081.581.2
Cathy9190907586.584.2
Doug4890858276.2577.4

Here is three different things in one formula:

  1. There are several math functions (such as vsdev - standard deviation of the data values per calc manual)
  2. The output lines are being referenced with the @> operator. Each additional > references one more line above the last line. Thus @>>> should be read as “third line from the last line of the table”
  3. All four functions are executed at once because each is separated by a :: operator.
INDEXVALUE
11
22
34
42
53
61
74
81
95
MEAN2.5555556
MEDIAN2
STD DEV1.5092309
SUM23
  • Standard Deviation with prettier table if the output was but to only three decimal places using ;%.3f
  • Medium with 3 digits with ;%.3d
INDEXVALUE
11
22
34
42
53
61
74
81
95
MEDIAN002
STD DEV1.509
  • $1=0 simply clears everything in the first column by setting each value to 0. Useful for resetting table values

  • @2$1..@>>$1 = random(1000);%.3d, creates random numbers. Here we create a random number between 1 and 1,000. Then we ensure that at least 3 digits are displayed with ;%.3d. This formula generates random numbers that will be used as student IDs. When testing out this formula (or any similar one) you will find that it is helpful to use the previous formula to reset everything to 0 while testing out your code.

  • ‘(length’(@I..@II)), simply counts all the records between the two horizontal lines and returns them. This is a handy way of dealing with large tables that require a count, but don’t require a column dedicated to indexes.

STUDENT NUMBERNAMETEST 1TEST 2TEST 3HOMEWORKFINAL EXAM
221Amy7882919477
931Bob7783899070
067Clara84889910080
632Dylan6974839165
921Ed7470778569
726Fiona8081868874
663Gareth7985848968
7

Continuing with our theme of a teacher checking grades, look at this table of grades. Some of the students have missing work. We need a breakdown of which students have missing assignments and how many assignments are missing.

We are looking for empty cells ("") and returning lists for each record. Then we count how many empty records were found, and set that number to the value in the MISSING column.

STUDENT NUMBERNAMETEST 1TEST 2TEST 3HOMEWORKFINAL EXAMMISSING
409Amy7882942
016Bob77838990700
432Clara848899100800
869Dylan698391651
925Ed74707785690
723Fiona808186741
688Gareth79858489680
7