docs/manual/guide/10-tables.org

635 lines · 36708 bytes

  1#+TITLE: Tables
  2#+DESCRIPTION: Creating and editing Org tables, column widths, import and export, and spreadsheet formulas.
  3#+LEDE: Org tables are plain text that orgstar keeps aligned, edits by row and column, and recalculates with the same formulas Emacs uses.
  4
  5* Tables in Org
  6
  7A table is a run of lines that start with =|=. Fields are separated by =|=, and a line
  8that starts with =|-= is a horizontal rule:
  9
 10#+BEGIN_SRC org
 11| Name  | Qty | Price |
 12|-------+-----+-------|
 13| Apple |   3 |  0.50 |
 14| Pear  |  12 |  0.75 |
 15#+END_SRC
 16
 17orgstar follows =org-table.el= from Org 9.8.7, with =org-table-automatic-realign= on,
 18=org-table-tab-jumps-over-hlines= on, and formulas adjusted without asking when rows
 19and columns move (=org-table-fix-formulas-confirm= nil). Tables inside dynamic blocks
 20count as tables; tables inside other blocks don't.
 21
 22* Creating a table
 23
 24Type =|=, a few field names separated by =|=, and press =TAB=: orgstar aligns the line
 25as a table and moves to the next field, adding a row after the last one. Type =|-= on
 26the line below a row and press =TAB= to turn it into a full-width rule.
 27
 28=org-table-create-or-convert-from-region= builds one for you: ~C-c |~ in the Emacs
 29and Doom presets, =⌃⌘\= in the Mac preset.
 30
 31With no selection it asks =Table size Columns x Rows [e.g. 5x2]:=. An empty answer
 32makes a 5 × 2 table. When there is more than one row, a rule follows the first row.
 33
 34With text selected, the command converts the selected lines to a table instead
 35(=org-table-convert-region=). The separator is guessed:
 36
 37- tabs, when every line has a tab;
 38- otherwise commas, when every line has a comma, read as CSV: a field in double quotes
 39  can hold commas, and a line break in it becomes a space;
 40- otherwise runs of spaces.
 41
 42* Moving around and alignment
 43
 44| Key     | Org command                | Action                                                                     |
 45|---------+----------------------------+----------------------------------------------------------------------------|
 46| =TAB=   | =org-table-next-field=     | Aligns the table and moves to the next field, skipping rules. In the last field it adds a row. |
 47| =S-TAB= | =org-table-previous-field= | Aligns the table and moves to the previous field.                         |
 48| =RET=   | =org-table-next-row=       | Aligns the table and moves down a row in the same column. Before a rule or at the end of the table it inserts a row. |
 49
 50These keys work the same in the Emacs and Mac presets. In Doom they apply in insert
 51state; in normal state =RET= is Doom's "act at point" (see /Recalculating/).
 52
 53Alignment (=org-table-align=) pads every field to its column's width and redraws
 54rules to match. A column is right-aligned when at least half of its non-empty fields
 55are numbers, and left-aligned otherwise. A field that holds only a cookie =<l>=, =<c>=
 56or =<r>= (optionally with a width, such as =<r10>=) fixes the column's alignment.
 57Widths count display columns, so wide characters and hidden link markup are measured
 58as they appear.
 59
 60Alignment happens when you press =TAB=, =S-TAB= or =RET= in a table and after every
 61table command, not while you type. To align without moving, use Org ▸ Align Table:
 62
 63| Preset | Key                          |
 64|--------+------------------------------|
 65| Emacs  | =C-c C-c= in a table         |
 66| Mac    | =⌃⌘X= in a table             |
 67| Doom   | =C-c C-c=, or =SPC m b a=    |
 68
 69=C-c C-c= (Mac =⌃⌘X=) in a table is =org-ctrl-c-ctrl-c=, as in Emacs. It first evaluates a formula
 70typed in the current field (see /Entering formulas/). Then, in a row marked =#= (see
 71/Names, parameters and constants/), it recalculates that row, which also aligns the
 72table; in any other row it aligns the table. With the caret at the table's very first
 73character, it recalculates the whole table instead.
 74
 75To align every table when a file opens, set =org-startup-align-all-tables= to =true=
 76in the config file (default =false=), or put =#+STARTUP: align= in the file;
 77=#+STARTUP: noalign= turns it off for that file. See
 78[[file:13-configuration.org][Configuration]].
 79
 80* Editing rows and columns
 81
 82| Action                            | Org command                | Emacs          | Mac         | Doom                          |
 83|-----------------------------------+----------------------------+----------------+-------------+-------------------------------|
 84| Move row up                       | =org-table-move-row-up=    | =M-<up>=       | =⌃⌘↑=       | =M-<up>=, =M-k=               |
 85| Move row down                     | =org-table-move-row-down=  | =M-<down>=     | =⌃⌘↓=       | =M-<down>=, =M-j=             |
 86| Move column left                  | =org-table-move-column-left= | =M-<left>=   | =⌃⌘←=       | =M-<left>=, =M-h=             |
 87| Move column right                 | =org-table-move-column-right= | =M-<right>= | =⌃⌘→=       | =M-<right>=, =M-l=            |
 88| Insert column                     | =org-table-insert-column=  | =M-S-<right>=  | =⌃⌥⌘→=      | =M-S-<right>=, =SPC m b i c=  |
 89| Delete column                     | =org-table-delete-column=  | =M-S-<left>=   | =⌃⌥⌘←=      | =M-S-<left>=, =SPC m b d c=   |
 90| Insert row above                  | =org-table-insert-row=     | =M-S-<down>=   | =⌃⌥⌘↓=      | =M-S-<down>=, =SPC m b i r=   |
 91| Delete row                        | =org-table-kill-row=       | =M-S-<up>=     | =⌃⌥⌘↑=      | =M-S-<up>=, =SPC m b d r=     |
 92| Insert rule below                 | =org-table-insert-hline=   | =C-c -=        | =⌃⌘-=       | =C-c -=, =SPC m b -=, =SPC m b i h= |
 93| Sort rows                         | =org-table-sort-lines=     | =C-c ^=        | =⌃⇧⌘S=      | =C-c ^=            |
 94| Transpose                         | =org-table-transpose-table-at-point= | Org ▸ Transpose Table | Org ▸ Transpose Table | Org ▸ Transpose Table |
 95| Edit field in its own editor      | =org-table-edit-field=     | =C-c `=        | Org ▸ Edit Table Field | =C-c `=            |
 96
 97The new column goes to the left of the caret's column, empty. Deleting a row deletes
 98the line; it doesn't go to the clipboard. When rows and columns move, are inserted or
 99are deleted, the =#+TBLFM= line is updated to match: references are renumbered, and
100formulas for a deleted row or column are removed.
101
102The Mac preset uses the same keys for headings and list items; in a table they act on
103the table.
104
105** Sorting
106
107=C-c ^= in a table asks =Sort Table: [a]lphabetic, [n]umeric, [t]ime.  A/N/T means
108reversed:= and sorts the rows between the rules around the caret by the caret's
109column. If the caret isn't in a field, it asks for the column first. Sorting is stable.
110
111- *a* compares text without case.
112- *n* compares the number at the start of each field.
113- *t* compares a timestamp in the field, else a duration such as =1:30= or =2h 15min=,
114  else a clock time =H:MM=. Fields with none of these sort as 0.
115
116** Transposing
117
118Org ▸ Transpose Table swaps rows and columns. Rules are dropped.
119
120** The field editor
121
122=C-c `= opens the field at the caret in an editor of its own, which is easier for long
123text. =C-c '= or =⌘Return= puts it back; =Escape= or =C-c C-k= leaves it unchanged.
124Line breaks become single spaces and lines that start with =#= are dropped, as
125=org-table-finish-edit-field= does.
126
127* Narrow columns
128
129A field that holds a width cookie =<N>=, optionally with an alignment letter
130(=<l10>=, =<c8>=, =<r12>=), marks the column for narrowing. Narrowed fields show their
131first /N/ display columns followed by =…=. The text itself is unchanged.
132
133| Command                                  | Org command                       | Keys                                     |
134|------------------------------------------+-----------------------------------+------------------------------------------|
135| Org ▸ Shrink or Expand Table Column      | =org-table-toggle-column-width=   | Emacs and Doom =C-c TAB=                 |
136| Org ▸ Shrink Table Columns with Widths   | =org-table-shrink=                | No default key                           |
137| Org ▸ Expand Table Columns               | =org-table-expand=                | No default key                           |
138
139=C-c TAB= in a field toggles that column. Outside a column, for example on the leading
140=|=, it asks for =Column ranges (e.g. 2-4 6-):=, where =6-= means column 6 to the end.
141A column without a width cookie shrinks to a bare =…= when toggled. Typing in a narrowed
142field widens its column again, as editing Org's overlays does. =#+STARTUP: shrink=
143narrows every column with a width cookie when the file opens.
144
145The iOS editor narrows columns the same way, with =#+STARTUP: shrink= and the three
146commands, which are in Commands when the caret is in a table. The iOS reader shows
147tables at full width.
148
149* Importing and exporting
150
151| Command                                | Org command        |
152|----------------------------------------+--------------------|
153| Import Table from File… (command palette) | =org-table-import= |
154| Export Table to File… (command palette)   | =org-table-export= |
155
156Open the command palette with =⇧⌘P= (=M-x= in the Emacs preset, =SPC := in Doom). On
157iOS, both are under More › Table in the editor, as Import Table from File…, Export Table
158as CSV… and Export Table as TSV…, and in Commands; the file comes from, and goes to, the
159Files app.
160
161Import asks for a CSV, TSV or plain text file, inserts its contents at the caret on a
162line of its own, and converts them to a table with the separator guessed as for a
163region (see /Creating a table/).
164
165Export writes the table at the caret. A file name ending in =.csv= gets CSV: fields
166separated by commas, with fields that contain a comma or a double quote quoted and
167inner quotes doubled. Any other name gets TSV. Rules are left out.
168
169Import and export are Mac only.
170
171* Formulas
172
173orgstar evaluates Org's spreadsheet formulas, =org-table-recalculate= and
174=org-table-eval-formula=, natively: Calc expressions in a reimplementation of the part
175of Emacs Calc that tables use, and Lisp formulas in a small Emacs Lisp evaluator. What
176falls outside them is handed to Emacs on the Mac (see /When Emacs is needed/).
177
178Formulas live in a =#+TBLFM:= line right after the table, separated by =::=:
179
180#+BEGIN_SRC org
181| Item  | Qty | Price | Total |
182|-------+-----+-------+-------|
183| Apple |   3 |  0.50 |  1.50 |
184| Pear  |  12 |  0.75 |  9.00 |
185|-------+-----+-------+-------|
186| Sum   |     |       | 10.50 |
187,#+TBLFM: $4=$2*$3;%.2f::@>$4=vsum(@I..@II);%.2f
188#+END_SRC
189
190Blank lines between the table and =#+TBLFM:= are allowed. If there are several
191=#+TBLFM:= lines, recalculating the table uses the first; =C-c C-c= on another applies
192that line instead (=org-table-calc-current-TBLFM=).
193
194** Kinds of formula
195
196| Left side               | Kind                 | Applies to                                                       |
197|-------------------------+----------------------+------------------------------------------------------------------|
198| =$3=                    | Column formula       | Every data row of column 3, except rows marked =!=, =^=, =_=, =$= or =/= in the first column. |
199| =$<=, =$>=              | Column formula       | The first or last column.                                        |
200| =@2$3=, =@>$3=          | Field formula        | One field. Field formulas override column formulas.               |
201| =@2$2..@4$3=            | Range formula        | Every field in the rectangle.                                    |
202| =@4=                    | Row formula          | Every field of data row 4.                                       |
203| =$name=                 | Named field formula  | The field named by a =^= or =_= row (see /Names, parameters and constants/). |
204
205A left side relative to the current row, such as =@-1$2=, is an error
206(=Unknown field=), and so is one relative to a rule, such as =@I$2=, as in Org. Two
207formulas for the same field are an error.
208
209** References
210
211Rows count data lines from 1; rules don't count. Columns count from 1.
212
213| Reference            | Meaning                                                              |
214|----------------------+----------------------------------------------------------------------|
215| =$2=                 | Column 2 in the current row.                                         |
216| =$-1=, =$+1=         | The column before or after the current one.                          |
217| =$<=, =$>=, =$>>=    | The first column, the last, the one before last.                     |
218| =@3=                 | Row 3 in the current column.                                         |
219| =@-1=, =@+1=         | The row above or below.                                              |
220| =@<=, =@>=           | The first or last data row.                                          |
221| =@I=, =@II=, =@III=  | The first, second, third rule; as a row, the line after it.          |
222| =@-I=                | The rule above the current row.                                      |
223| =@I+2=               | Two data rows after the first rule.                                  |
224| =@2$3=               | Row 2, column 3.                                                     |
225| =$0=                 | The current column.                                                  |
226| =@0=                 | The current row: =@0$2= is =$2=.                                     |
227| =@#=, =$#=           | The current row's or column's number, as a value.                    |
228| =@2$1..@4$3=         | A range: the fields of the rectangle, as a vector.                   |
229| =$1..$3=             | Columns 1 to 3 of the current row.                                   |
230| =@I..@II=            | The current column from the first rule to the second.                |
231
232In a Calc formula a single field becomes a number in parentheses, and a range becomes
233a vector such as =[1,2,3]=. Without the =E= flag, empty fields count as 0 on their own
234and are left out of ranges.
235
236Fields in a Calc formula must hold numbers, timestamps or =nan=, unless the =N= flag
237reads every field as a number. A reference to a field with other text needs Emacs,
238because Calc would treat the text as a symbol.
239
240** Names, parameters and constants
241
242The first column can mark special rows, as in Org's spreadsheet:
243
244| Mark  | Row                                                                                   |
245|-------+---------------------------------------------------------------------------------------|
246| =!=   | Column names: =$qty= in a formula means the column whose =!= row field is =qty=.      |
247| =^=   | Names for the fields in the row above, usable as =$name=.                            |
248| =_=   | Names for the fields in the row below.                                               |
249| =$=   | Parameters: fields like ~rate=0.2~, usable as =$rate=.                               |
250| =#=   | Marked for recalculation.                                                            |
251| =*=   | Marked for recalculation.                                                            |
252| =/=   | Not recalculated.                                                                    |
253
254When any row's first field is one of =!=, =^=, =_=, =$=, =#= or =*=, column formulas
255in a whole-table recalculation apply only to the rows marked =#= or =*=, as
256=org-table-calculate-mark-regexp= decides in Org. So a table with a =!= names row and
257no =#= rows gets no column formulas applied; mark the rows to calculate. Field
258formulas apply either way. Rows marked =!=, =^=, =_=, =$= or =/= are never changed by
259column formulas.
260
261#+BEGIN_SRC org
262| ! | qty | price | total |
263|---+-----+-------+-------|
264| # |   2 |     3 |     6 |
265| # |   4 |   0.5 |     2 |
266,#+TBLFM: $4=$qty*$price
267#+END_SRC
268
269A =$name= that isn't a column name, parameter or named field is looked up as
270=org-table-get-constant= does:
271
272- in =#+CONSTANTS:= lines of the file or its setup file, written ~name=value~ and
273  separated by spaces (~#+CONSTANTS: c=299792458 g=9.81~);
274- =$PROP_xyz= reads the =xyz= property of the entry holding the table, with
275  inheritance.
276
277A name that isn't found becomes =#UNDEFINED_NAME=. A parameter named =%= in a =$= row,
278for example ~%=%.2f~, is put in front of the flags of every formula that has a =;=,
279so ~$3=$2/3;~ is formatted with =%.2f=.
280
281** Remote references
282
283=remote(NAME, REF)= reads a field or range from another table:
284
285#+BEGIN_SRC org
286,#+NAME: rates
287| item | rate |
288|------+------|
289| a    |    2 |
290| b    |    3 |
291
292| x | y | z |
293|---+---+---|
294| 4 | 8 | 5 |
295,#+TBLFM: $2=$1*remote(rates,@2$2)::$3=vsum(remote(rates,@2$2..@>$2))
296#+END_SRC
297
298=NAME= is found in this order:
299
3001. a table after =#+NAME: NAME= or =#+TBLNAME: NAME= in the same file;
3012. the first table in the entry whose =ID= property is =NAME=, in the same file;
3023. the first table in the entry with that =ID= in any indexed file in your sidebar
303   folders, read from the file on disk.
304
305=REF= may use spreadsheet-style references such as =B3= (column B, row 3), which
306become =@3$2=. A =REF= without a row, such as =$1..$2=, reads the row of the other
307table that has the current row's number. =NAME= can itself be a reference: in
308=remote($1,@1$1)=, the current row's first field holds the table's name.
309
310** Calc expressions
311
312Calc formulas use Calc's number rules: integers are exact, and decimal numbers are
313rounded to 12 significant digits after every operation. Results are shown with up to
3148 significant digits unless a format flag says otherwise (=org-calc-default-modes=).
315
316Operators, from lowest to highest precedence (the lowest, ~||~, is logical or, giving
3171 or 0):
318
319| Operator                           | Meaning                                                   |
320|------------------------------------+-----------------------------------------------------------|
321| =&&=                               | Logical and.                                              |
322| =!=                                | Logical not.                                              |
323| ~==~, ~!=~, =<=, =>=, ~<=~, ~>=~    | Comparisons, giving 1 or 0. They can't be chained.        |
324| =+=, =-=                           | Addition and subtraction.                                 |
325| =/=, =%=, =\=                      | Division, modulo (sign of the divisor), integer division rounding down. |
326| =*=                                | Multiplication.                                           |
327| unary =-=                          | Negation.                                                 |
328| =^=                                | Power.                                                    |
329
330Parentheses group, =[a, b, c]= writes a vector, =<2026-10-05 Mon>= writes a date, and
331=nan= is "not a number".
332
333Functions:
334
335| Function                                   | Result                                                        |
336|--------------------------------------------+---------------------------------------------------------------|
337| =vsum=, =vprod=                            | Sum or product of a vector.                                   |
338| =vcount=                                   | Number of elements.                                           |
339| =vmean=, =vmedian=                         | Mean, median.                                                 |
340| =vmax=, =vmin=                             | Largest, smallest element.                                    |
341| =vvar=, =vsdev=                            | Sample variance, sample standard deviation.                   |
342| =vpvar=, =vpsdev=                          | Population variance, population standard deviation.           |
343| =max(a, b, …)=, =min(a, b, …)=             | Largest, smallest argument.                                   |
344| =if(c, a, b)=                              | =a= if =c= is nonzero, else =b=.                              |
345| =abs=                                      | Absolute value.                                               |
346| =floor=, =ceil=, =trunc=                   | Round down, up, toward zero, to an integer.                   |
347| =round(x)=, =round(x, n)=                  | Round to an integer, or to /n/ decimal places.                |
348| =mod(a, b)=, =idiv(a, b)=                  | As =%= and =\=.                                               |
349| =fact=                                     | Factorial of a non-negative integer.                          |
350| =sqrt=, =exp=, =ln=, =log10=               | Square root, exponential, natural and base-10 logarithm.      |
351| =sin=, =cos=, =tan=                        | Trigonometry, in degrees unless the =R= flag is set.          |
352| =arcsin=, =arccos=, =arctan=               | Inverse trigonometry, in degrees unless =R=.                  |
353
354Other Calc functions, variables such as =pi= or =e=, symbolic results, complex results
355and division by zero need Emacs.
356
357** Mode flags and formats
358
359After the formula, a =;= starts its flags, as in ~$3=$1/$2;%.2f~ or ~$4=$1*2;f2~:
360
361| Flag      | Meaning                                                                                   |
362|-----------+-------------------------------------------------------------------------------------------|
363| =nN=      | Float format with /N/ significant digits.                                                 |
364| =fN=      | Fixed format with /N/ decimal places.                                                     |
365| =sN=      | Scientific format with /N/ digits.                                                        |
366| =eN=      | Engineering format with /N/ digits.                                                       |
367| =pN=      | Calc precision. Only =p12=, the default, is evaluated natively; others need Emacs.        |
368| =D=, =R=  | Angles in degrees (the default) or radians.                                               |
369| =F=       | Prefer fractions: =1/3= stays =1:3=.                                                      |
370| =N=       | Treat every field as a number; text counts as 0.                                          |
371| =E=       | Keep empty fields: in ranges they stay in, and count as =nan= in Calc.                    |
372| =L=       | Literal: in Lisp formulas, insert fields as they are written.                             |
373| =T=       | Durations: read =H:MM= and =H:MM:SS= fields as times, show the result as =HH:MM:SS=.      |
374| =U=       | As =T=, showing =HH:MM=.                                                                  |
375| =t=       | As =T=, showing decimal hours with two places, such as =1.50=.                            |
376
377The flags =S= (symbolic) and =u= need Emacs. Any other text after the flags is a
378=format= string applied to the result, such as =%.2f= or =%d=; =format= supports
379=%s=, =%S=, =%d=, =%o=, =%x=, =%X=, =%c=, =%e=, =%f= and =%g=.
380
381** Durations
382
383With =T=, =U= or =t=, fields like =1:30= or =10:00:30= are read as hours, minutes and
384seconds:
385
386#+BEGIN_SRC org
387| start    | end     | sum      | diff  | hours |
388|----------+---------+----------+-------+-------|
389| 1:30     | 0:45    | 02:15:00 | 00:45 |  3.00 |
390| 10:00:30 | 2:15:10 | 12:15:40 | 07:45 | 20.02 |
391,#+TBLFM: $3=$1+$2;T::$4=$1-$2;U::$5=$1*2;t
392#+END_SRC
393
394** Dates
395
396Timestamps in fields, active or inactive, take part in Calc arithmetic as dates. The
397difference of two dates is a number of days, with a fraction when the timestamps have
398times. A date plus a number is a date, written back as an inactive timestamp:
399
400#+BEGIN_SRC org
401| start            | end              | days | later            |
402|------------------+------------------+------+------------------|
403| <2026-10-05 Mon> | <2026-10-12 Mon> |    7 | [2026-10-12 Mon] |
404,#+TBLFM: $3=$2-$1::$4=$1+7
405#+END_SRC
406
407** Lisp formulas
408
409A formula that starts with ='(= is Emacs Lisp. Each reference becomes a Lisp string,
410or a number with =N=, or the field's text inserted as is with =L=. A range becomes the
411values separated by spaces, so wrap it in a quoted list:
412
413#+BEGIN_SRC org
414| name  | greeting |
415|-------+----------|
416| Ada   | Ada!     |
417| Grace | Grace!   |
418,#+TBLFM: $2='(concat $1 "!")
419
420| n |
421|---|
422| 1 |
423| 2 |
424|---|
425| 3 |
426,#+TBLFM: @>$1='(apply '+ '(@I..@II));N
427#+END_SRC
428
429The evaluator supports:
430
431- special forms: =quote=, =function=, =lambda=, =progn=, =prog1=, =if=, =when=,
432  =unless=, =cond=, =and=, =or=, =let=, =let*=, =setq=, =push=, =pop=, =while=,
433  =dolist=, =dotimes=, =with-output-to-string=, =ignore-errors=, =condition-case=;
434- arithmetic: =+=, =-=, =*=, =/=, =%=, =mod=, =1+=, =1-=, =abs=, =max=, =min=, =float=,
435  =floor=, =ceiling=, =round=, =truncate=, ~=~, =<=, =>=, ~<=~, ~>=~, ~/=~, =zerop=;
436- predicates: =not=, =null=, =eq=, =eql=, =equal=, =numberp=, =integerp=, =floatp=,
437  =stringp=, =listp=, =consp=, =symbolp=;
438- lists: =car=, =cdr=, =cadr=, =cddr=, =cons=, =list=, =nth=, =nthcdr=, =elt=, =aref=,
439  =append=, =length=, =reverse=, =number-sequence=, =memq=, =member=, =memql=, =assoc=,
440  =assq=, =delq=, =delete=, =mapcar=, =mapc=, =mapconcat=, =funcall=, =apply=,
441  =identity=, =ignore=;
442- strings: =concat=, =format=, =format-message=, ~string=~, =string-equal=, =string<=,
443  =string-lessp=, =upcase=, =downcase=, =capitalize=, =substring=, =string-to-number=,
444  =number-to-string=, =int-to-string=, =string-prefix-p=, =string-suffix-p=,
445  =string-empty-p=, =string-trim=, =split-string=, =prin1-to-string=;
446- output and errors: =princ=, =prin1=, =print=, =terpri=, =message=, =error=,
447  =user-error=;
448- Org's lookup functions =org-lookup-first=, =org-lookup-last= and =org-lookup-all=.
449
450Any other function needs Emacs. An error inside a Lisp formula writes =#ERROR= in the
451field, as does a Calc error.
452
453* Entering formulas
454
455There are three ways to set a formula.
456
457*In the field.* Type ~=expr~ in a field and press =TAB= or =RET=: orgstar stores
458~$N=expr~ as the column's formula and evaluates it, as
459=org-table-maybe-eval-formula= does. Type ~:=expr~ instead to store a field formula
460~@R$C=expr~.
461
462*With a prompt.* =org-table-eval-formula= asks for the formula (~Column formula $N=~
463or ~Field formula @R$C=~) with the stored one filled in, stores it and evaluates it in
464the current field. Clearing the prompt and pressing =Return= removes the stored
465formula, as in Emacs, and the echo area shows =Formula removed=. You can also delete a
466formula in the formula editor below or from the =#+TBLFM:= line.
467
468| Command                       | Emacs    | Mac                         | Doom     |
469|-------------------------------+----------+-----------------------------+----------|
470| Org ▸ Set Column Formula      | ~C-c =~  | Org ▸ Set Column Formula    | ~C-c =~  |
471| Org ▸ Set Field Formula       | none     | Org ▸ Set Field Formula     | none     |
472
473Org's ~C-u C-c =~ for field formulas has no equivalent key, because orgstar has no
474prefix argument; use the menu item or type ~:=~ in the field.
475
476*In the formula editor.* =C-c '= in a table or on its =#+TBLFM:= line
477(=org-table-edit-formulas=) opens the formulas in an editor of their own, one per line,
478grouped under =# Column Formulas=, =# Field and Range Formulas= and
479=# Named Field Formulas=. A formula can continue on indented lines. =C-c '= or
480=⌘Return= installs the formulas; =Escape= or =C-c C-k= leaves them unchanged.
481Installing doesn't recalculate. The echo area says so and points to Recalculate Table:
482=C-c C-c= on the =#+TBLFM:= line, or =⌃⌘X= there in the Mac preset.
483
484The formulas are stored sorted the way =org-table-formula-less-p= sorts them.
485
486* Recalculating
487
488| Command                          | Org command                       | Emacs                         | Mac                         | Doom                                |
489|----------------------------------+-----------------------------------+-------------------------------+-----------------------------+-------------------------------------|
490| Recalculate Table                | =org-table-recalculate= with =C-u= | =C-c C-c= on =#+TBLFM:=      | =⌃⌘X= on =#+TBLFM:=         | =SPC m b r=, =C-c C-c= on =#+TBLFM:= |
491| Recalculate Table Row            | =org-table-recalculate=           | =C-c *=                       | =⌃⌘*= (=⌃⇧⌘8= on a US keyboard) | =C-c *=                             |
492| Iterate Table Formulas           | =org-table-iterate=               | =C-u C-u C-c *=               | —                           | —                                   |
493
494In Doom's normal state, =RET= in a table recalculates it when it has a =#+TBLFM:=
495line and aligns it otherwise; on a =#+TBLFM:= line it recalculates.
496
497When the table has a rule below its first data row, recalculating the whole table
498leaves the rows above that rule, the header, alone. With marked rows, the marks decide
499instead (see /Names, parameters and constants/). Each
500command evaluates the column formulas row by row, then the field formulas, then aligns
501the table. Each formula is evaluated once. Iterate Table Formulas recalculates the whole
502table again and again until it stops changing, for formulas that depend on each other's
503results, at most ten times. The echo area says =Convergence after 3 iterations=, or
504=Table was already stable=; after ten passes that still change the table it says =No
505convergence after 10 iterations=, and the ten passes stay. =C-u C-c *= also recalculates
506the whole table in the Emacs preset.
507
508A row marked =#= is recalculated when you press =TAB=, =S-TAB= or =RET= in it, before
509the caret moves (=org-table-maybe-recalculate-line=), and with =C-c C-c= (Mac =⌃⌘X=).
510Other rows are recalculated only by the commands above. Emacs skips a second automatic
511recalculation of the same row by the same command in a row; orgstar recalculates each
512time, which gives the same result unless a formula depends on its own value.
513
514* When Emacs is needed
515
516When a formula uses something the native evaluator doesn't have, the command reports
517what it was and the Mac recalculates the table in Emacs instead. This happens for:
518
519- references to fields holding text in a Calc formula, and symbolic results;
520- Calc functions and variables not listed above, precision other than =p12=, and the
521  =S= and =u= flags;
522- division by zero, complex results, vector results, and numbers too large for
523  64-bit integers;
524- Lisp functions not listed above.
525
526The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=.
527orgstar runs =emacs -Q --batch= on a copy of the file, so your Emacs init file,
528packages and customizations are not loaded, then replaces the table with Emacs's
529result. It looks for Emacs at the path in the =ORGSTAR_EMACS= environment variable,
530then =/opt/homebrew/bin/emacs=, =/usr/local/bin/emacs=,
531=/Applications/Emacs.app/Contents/MacOS/Emacs=, =/run/current-system/sw/bin/emacs= and
532=/usr/bin/emacs=. If none is found, the echo area says Emacs isn't installed and the
533table is unchanged. Emacs gets your =PATH= with =/opt/homebrew/bin=, =/usr/local/bin=,
534=/Library/TeX/texbin=, =/usr/bin= and =/bin= added, as code blocks and export do, so
535programs a formula starts are found when orgstar was opened from the Dock. The run
536stops after 60 seconds. If you edit the table while Emacs is working, its result is
537discarded.
538
539Edit ▸ Cancel Running Task (=⌘.=) stops the recalculation; the echo area shows
540=Recalculation canceled= and the table is unchanged. Starting another recalculation or
541export in Emacs cancels the one that is running.
542
543If the table's formulas contain Lisp, orgstar asks first:
544=This table's formulas run Lisp. Run them? (yes, no, always)=. =always= trusts this
545table's text in this file, so the question isn't asked again until the text changes.
546
547Entering a formula that needs Emacs with ~C-c =~, Set Field Formula or ~=~ in a field
548is not handed to Emacs. The formula is stored in the =#+TBLFM:= line and the field is
549left as it was; the echo area shows =The formula was stored; recalculating it needs
550Emacs= and the reason. =TAB= and =RET= still move to the next field. Recalculate the
551table to run the formula in Emacs.
552
553For how orgstar and Emacs share files, see
554[[file:15-alongside-emacs.org][Using orgstar alongside Emacs]].
555
556* table.el tables
557
558Tables drawn with =+= corners and =-= and =|= borders, as the =table.el= package makes
559them, are recognized and kept as written:
560
561#+BEGIN_SRC org
562+-------+-------+
563| Name  | Value |
564+-------+-------+
565| a     | 1     |
566+-------+-------+
567#+END_SRC
568
569Edit the text in the cells as usual. =TAB= and =S-TAB= line the borders up, then move to
570the next or previous cell, wrapping around at the ends of the table, as table.el's
571=table-forward-cell= and =table-backward-cell= do; the caret goes to the cell's first
572character. =C-c C-c= (Mac =⌃⌘X=) lines the borders up without moving. Lining up makes
573each column as wide as its widest text, and never narrower than its rules: text typed
574into a cell uses up the spaces at its end first, as table.el does when you type.
575
576Org edits table.el tables in table.el's own buffer, with =C-c '=, and says so for =TAB=
577and =C-c C-c=; orgstar does the editing in place instead. Tables with merged cells, which
578span rows or columns, can be moved through but not lined up ("Can't align a table.el
579table with merged cells"). Formulas, sorting and the other table commands don't apply.
580
581HTML export renders them as tables, with cells that span rows and columns, and Markdown
582export writes them as a code block. See [[file:12-export.org][Export]].
583
584* Plotting
585
586Plot Table (=org-plot/gnuplot=, =C-c " g= in the Emacs and Doom presets, and in the
587command palette; More › Table on iOS) draws the table at the caret, or the first table
588after it, as the =#+PLOT:= lines above it say:
589
590#+BEGIN_SRC org
591#+PLOT: title:"Sales" ind:1 deps:(2 3) type:2d with:linespoints file:"sales.png"
592| month | north | south |
593|-------+-------+-------|
594|     1 |    20 |    12 |
595|     2 |    25 |    18 |
596#+END_SRC
597
598| Option     | Meaning                                                                     |
599|------------+-----------------------------------------------------------------------------|
600| =type=     | =2d= (the default), =3d= or =grid=                                          |
601| =ind=      | The column of x values, counting from 1; 0 (the default) plots against row numbers |
602| =deps=     | The columns to plot, as a list such as =(2 3)=; all but =ind= by default    |
603| =with=     | =lines= (the default), =points=, =linespoints=, =impulses=, =boxes=, =histograms=, =steps=, =fsteps= |
604| =title=    | The plot's title                                                            |
605| =file=     | Where the image goes, relative to the Org file's folder; without it the plot shows in a sheet |
606| =labels=   | Names for the columns, as a list; a header row above a rule names them otherwise |
607| =map=      | With =t=, a =3d= plot is seen from above                                    |
608| =ymin=, =ymax=, =xmin=, =xmax= | The axes' ranges; =min= and =max= are =ymin= and =ymax=   |
609| =ticks=    | How many marks the y axis has                                               |
610| =transpose= | With =yes=, rows become columns                                            |
611| =set=, =line=, =script=, =timefmt= | gnuplot commands, a line of gnuplot, a gnuplot script file and the time format, for gnuplot |
612
613Options are read as Org reads them, quirks included: an option's name also matches inside
614a longer word (=timeind:t= sets =ind=), an unquoted value ends at a =.=, =,= or =;=, =set:=
615options before the last =line:= are dropped, and each =set:= and =line:= that's kept comes
616twice, as Org reads the options twice. An ind column of timestamps is plotted as dates, and one of text as labels.
617
618On the Mac, when gnuplot is installed (orgstar looks on the same =PATH= as code blocks), the
619table goes to gnuplot as Emacs sends it: the same data file and script, so every option and
620gnuplot's own terminals and styles apply. A plot without =file:= is drawn to an image and shown
621in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d=
622as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a
623file). =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as gnuplot's default; the
624gnuplot-only options are ignored. If inline images are showing, a link to the file shows the
625new plot.
626
627=type:radar= needs gnuplot; orgstar reports it.
628
629* On iOS
630
631The iOS app evaluates formulas natively with the same engine. Tables that need Emacs
632are not recalculated there; the app reports =This table needs Emacs, which runs on the
633Mac= with the reason. Import and export are under More › Table, with Plot Table.
634Narrow columns work in the editor, as described under /Narrow columns/. See
635[[file:14-ios.org][iOS]].