Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 38 ยท Org Mode

Org Tables, Code Blocks and Export

Build aligned Org tables with formulas, run code in Org Babel source blocks, tangle code into files, and export Org documents to HTML, PDF, Markdown and more.

This lesson covers the three features that make Org a document system, not just a task list: tables with spreadsheet-like formulas, Babel for running code inside your notes, and export for turning an Org file into HTML, PDF and other formats.

Tables

A table is lines that start with |. Type the first row, then press TAB and Org aligns the table and moves to the next cell.

ORG
| Item     | Qty | Price |
|----------+-----+-------|
| Notebook |   3 |  2.50 |
| Pen      |  10 |  0.80 |

Type |- and press TAB to create the horizontal rule. Keys:

Keys Effect
TAB / S-TAB Next / previous cell, re-aligning the table
RET Next row (creating one at the end)
M-<left> / M-<right> Move the column
M-<up> / M-<down> Move the row
M-S-<right> / M-S-<down> Insert a column / row
M-S-<left> / M-S-<up> Delete a column / row
C-c - Insert a horizontal line
C-c ^ Sort by the column under point
C-c | Turn the region (CSV, tab-separated) into a table
C-c C-c Re-align and recalculate

Select CSV text and press C-c | to convert it. A table can be written in any width: Org re-aligns it. Numbers are right-aligned automatically.

JS Stand-in: align an Org table

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Formulas

Org tables can calculate. A #+TBLFM: line below the table holds formulas. A column is $1, $2, ... and a row is @1, @2, ...; @> is the last row and $> the last column.

ORG
| Item     | Qty | Price | Total |
|----------+-----+-------+-------|
| Notebook |   3 |  2.50 |  7.50 |
| Pen      |  10 |  0.80 |  8.00 |
|----------+-----+-------+-------|
| Sum      |     |       | 15.50 |
#+TBLFM: $4=$2*$3::@>$4=vsum(@2..@-1)

Press C-c C-c on the #+TBLFM line (or inside the table) to recompute. The first formula, $4=$2*$3, fills each data row's fourth column with quantity times price. After ::, a second formula puts the sum of the column above (@2..@-1, from row 2 to the row before the current one) into the last row. Row numbers in @2 count data rows from the top; horizontal rules are not counted. Functions include vsum, vmean, vmax, vmin and vcount, and you can embed Calc (C-c = evaluates a formula in the current field, C-u C-c = for the whole column). For anything complicated, switch to a source block that outputs a table, the next section's subject.

JS Stand-in: a tiny TBLFM evaluator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Babel: code blocks that run

A source block is delimited by #+begin_src LANGUAGE and #+end_src. Press C-c C-c inside it to run it. The result is inserted under the block in a #+RESULTS: section.

ORG
#+begin_src python :results output
for i in range(3):
    print(i * i)
#+end_src

#+RESULTS:
: 0
: 1
: 4

Org asks you to confirm each run, because executing code from a file is a security decision (org-confirm-babel-evaluate). Languages must be enabled first, for example in your init file:

ELISP
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t) (python . t) (shell . t)))

Emacs Lisp is available by default. Others require the language to be installed on your system.

Header arguments

Settings after the language name control behavior:

Argument Meaning
:results output Capture printed output (default value takes the last expression's value)
:results table / :results silent Treat the result as a table / do not insert it
:var n=5 Pass a variable, which may be a table or another block's result
:session Keep one interpreter running between blocks
:exports code / results / both / none What export shows
:tangle file.py Write the code to a file (see below)
:dir ~/project Set the working directory

Editing a block in its own buffer with the right mode helps: C-c ' opens it, and C-c ' again returns. Setting org-src-fontify-natively colors the code inline.

Literate programming with tangle

Organize code and explanation in one Org file and tangle it: C-c C-v t (org-babel-tangle) writes every block that has a :tangle target into its file. Many people keep their entire Emacs configuration in config.org and load it from init.el with org-babel-load-file; the configuration chapter shows an init.el alongside.

JS Stand-in: tangle blocks to files

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Export

C-c C-e (org-export-dispatch) opens a menu. Press a format letter and then an action letter:

Keys Result
C-c C-e h h HTML file
C-c C-e h o HTML file and open it in a browser
C-c C-e l p LaTeX to PDF (needs a TeX installation)
C-c C-e m m Markdown (you may need to enable the exporter: (require 'ox-md))
C-c C-e t u Plain text
C-c C-e O o OpenDocument text (opens in LibreOffice)

Headings become document sections, lists become lists, links become links, tables become tables and source blocks are formatted, with results if :exports allows. Document-wide settings go in keywords at the top: #+title:, #+author:, #+options: toc:2 num:nil, #+language: en. Subtrees can be exported alone (C-c C-e C-s limits to the subtree). TODO keywords and tags appear in exported text unless you turn them off with #+options: todo:nil tags:nil.

Export is where Org earns its place as a writing tool: one plain-text source, many outputs, and the same file doubles as your task list and agenda. You now have the Org essentials; the track overview shows where the course goes next.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does TAB do inside an Org table?

    TAB re-aligns the table and moves to the next cell, adding a row at the end.
  2. 2. Where is a table formula stored?

    A #+TBLFM line placed right below the table holds its formulas.
  3. 3. Which key runs the source block at point?

    C-c C-c evaluates the block and inserts a #+RESULTS: section.
  4. 4. What does :tangle hello.py do?

    org-babel-tangle (C-c C-v t) writes blocks with a :tangle header to their target files.

Summary

  • Org tables align with TAB; formulas live in #+TBLFM: lines and run with C-c C-c.
  • Babel runs code blocks with C-c C-c (languages must be enabled; confirmation protects you) and controls them with header arguments such as :results, :var, :session, :exports and :tangle.
  • Tangling writes code to files; C-c C-e exports to HTML, PDF, Markdown, text and more.
  • That ends the Org chapter; the track continues with configuration and Emacs Lisp.

Next: The Emacs Init File, early-init.el and the Customize Interface