#+TITLE: Tables #+DESCRIPTION: Creating and editing Org tables, column widths, import and export, and spreadsheet formulas. #+LEDE: Org tables are plain text that orgstar keeps aligned, edits by row and column, and recalculates with the same formulas Emacs uses. * Tables in Org A table is a run of lines that start with =|=. Fields are separated by =|=, and a line that starts with =|-= is a horizontal rule: #+BEGIN_SRC org | Name | Qty | Price | |-------+-----+-------| | Apple | 3 | 0.50 | | Pear | 12 | 0.75 | #+END_SRC orgstar follows =org-table.el= from Org 9.8.7, with =org-table-automatic-realign= on, =org-table-tab-jumps-over-hlines= on, and formulas adjusted without asking when rows and columns move (=org-table-fix-formulas-confirm= nil). Tables inside dynamic blocks count as tables; tables inside other blocks don't. * Creating a table Type =|=, a few field names separated by =|=, and press =TAB=: orgstar aligns the line as a table and moves to the next field, adding a row after the last one. Type =|-= on the line below a row and press =TAB= to turn it into a full-width rule. =org-table-create-or-convert-from-region= builds one for you: ~C-c |~ in the Emacs and Doom presets, =⌃⌘\= in the Mac preset. With no selection it asks =Table size Columns x Rows [e.g. 5x2]:=. An empty answer makes a 5 × 2 table. When there is more than one row, a rule follows the first row. With text selected, the command converts the selected lines to a table instead (=org-table-convert-region=). The separator is guessed: - tabs, when every line has a tab; - otherwise commas, when every line has a comma, read as CSV: a field in double quotes can hold commas, and a line break in it becomes a space; - otherwise runs of spaces. * Moving around and alignment | Key | Org command | Action | |---------+----------------------------+----------------------------------------------------------------------------| | =TAB= | =org-table-next-field= | Aligns the table and moves to the next field, skipping rules. In the last field it adds a row. | | =S-TAB= | =org-table-previous-field= | Aligns the table and moves to the previous field. | | =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. | These keys work the same in the Emacs and Mac presets. In Doom they apply in insert state; in normal state =RET= is Doom's "act at point" (see /Recalculating/). Alignment (=org-table-align=) pads every field to its column's width and redraws rules to match. A column is right-aligned when at least half of its non-empty fields are numbers, and left-aligned otherwise. A field that holds only a cookie ==, == or == (optionally with a width, such as ==) fixes the column's alignment. Widths count display columns, so wide characters and hidden link markup are measured as they appear. Alignment happens when you press =TAB=, =S-TAB= or =RET= in a table and after every table command, not while you type. To align without moving, use Org ▸ Align Table: | Preset | Key | |--------+------------------------------| | Emacs | =C-c C-c= in a table | | Mac | =⌃⌘X= in a table | | Doom | =C-c C-c=, or =SPC m b a= | =C-c C-c= (Mac =⌃⌘X=) in a table is =org-ctrl-c-ctrl-c=, as in Emacs. It first evaluates a formula typed in the current field (see /Entering formulas/). Then, in a row marked =#= (see /Names, parameters and constants/), it recalculates that row, which also aligns the table; in any other row it aligns the table. With the caret at the table's very first character, it recalculates the whole table instead. To align every table when a file opens, set =org-startup-align-all-tables= to =true= in the config file (default =false=), or put =#+STARTUP: align= in the file; =#+STARTUP: noalign= turns it off for that file. See [[file:13-configuration.org][Configuration]]. * Editing rows and columns | Action | Org command | Emacs | Mac | Doom | |-----------------------------------+----------------------------+----------------+-------------+-------------------------------| | Move row up | =org-table-move-row-up= | =M-= | =⌃⌘↑= | =M-=, =M-k= | | Move row down | =org-table-move-row-down= | =M-= | =⌃⌘↓= | =M-=, =M-j= | | Move column left | =org-table-move-column-left= | =M-= | =⌃⌘←= | =M-=, =M-h= | | Move column right | =org-table-move-column-right= | =M-= | =⌃⌘→= | =M-=, =M-l= | | Insert column | =org-table-insert-column= | =M-S-= | =⌃⌥⌘→= | =M-S-=, =SPC m b i c= | | Delete column | =org-table-delete-column= | =M-S-= | =⌃⌥⌘←= | =M-S-=, =SPC m b d c= | | Insert row above | =org-table-insert-row= | =M-S-= | =⌃⌥⌘↓= | =M-S-=, =SPC m b i r= | | Delete row | =org-table-kill-row= | =M-S-= | =⌃⌥⌘↑= | =M-S-=, =SPC m b d r= | | Insert rule below | =org-table-insert-hline= | =C-c -= | =⌃⌘-= | =C-c -=, =SPC m b -=, =SPC m b i h= | | Sort rows | =org-table-sort-lines= | =C-c ^= | =⌃⇧⌘S= | =C-c ^= | | Transpose | =org-table-transpose-table-at-point= | Org ▸ Transpose Table | Org ▸ Transpose Table | Org ▸ Transpose Table | | Edit field in its own editor | =org-table-edit-field= | =C-c `= | Org ▸ Edit Table Field | =C-c `= | The new column goes to the left of the caret's column, empty. Deleting a row deletes the line; it doesn't go to the clipboard. When rows and columns move, are inserted or are deleted, the =#+TBLFM= line is updated to match: references are renumbered, and formulas for a deleted row or column are removed. The Mac preset uses the same keys for headings and list items; in a table they act on the table. ** Sorting =C-c ^= in a table asks =Sort Table: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:= and sorts the rows between the rules around the caret by the caret's column. If the caret isn't in a field, it asks for the column first. Sorting is stable. - *a* compares text without case. - *n* compares the number at the start of each field. - *t* compares a timestamp in the field, else a duration such as =1:30= or =2h 15min=, else a clock time =H:MM=. Fields with none of these sort as 0. ** Transposing Org ▸ Transpose Table swaps rows and columns. Rules are dropped. ** The field editor =C-c `= opens the field at the caret in an editor of its own, which is easier for long text. =C-c '= or =⌘Return= puts it back; =Escape= or =C-c C-k= leaves it unchanged. Line breaks become single spaces and lines that start with =#= are dropped, as =org-table-finish-edit-field= does. * Narrow columns A field that holds a width cookie ==, optionally with an alignment letter (==, ==, ==), marks the column for narrowing. Narrowed fields show their first /N/ display columns followed by =…=. The text itself is unchanged. | Command | Org command | Keys | |------------------------------------------+-----------------------------------+------------------------------------------| | Org ▸ Shrink or Expand Table Column | =org-table-toggle-column-width= | Emacs and Doom =C-c TAB= | | Org ▸ Shrink Table Columns with Widths | =org-table-shrink= | No default key | | Org ▸ Expand Table Columns | =org-table-expand= | No default key | =C-c TAB= in a field toggles that column. Outside a column, for example on the leading =|=, it asks for =Column ranges (e.g. 2-4 6-):=, where =6-= means column 6 to the end. A column without a width cookie shrinks to a bare =…= when toggled. Typing in a narrowed field widens its column again, as editing Org's overlays does. =#+STARTUP: shrink= narrows every column with a width cookie when the file opens. The iOS editor narrows columns the same way, with =#+STARTUP: shrink= and the three commands, which are in Commands when the caret is in a table. The iOS reader shows tables at full width. * Importing and exporting | Command | Org command | |----------------------------------------+--------------------| | Import Table from File… (command palette) | =org-table-import= | | Export Table to File… (command palette) | =org-table-export= | Open the command palette with =⇧⌘P= (=M-x= in the Emacs preset, =SPC := in Doom). On iOS, both are under More › Table in the editor, as Import Table from File…, Export Table as CSV… and Export Table as TSV…, and in Commands; the file comes from, and goes to, the Files app. Import asks for a CSV, TSV or plain text file, inserts its contents at the caret on a line of its own, and converts them to a table with the separator guessed as for a region (see /Creating a table/). Export writes the table at the caret. A file name ending in =.csv= gets CSV: fields separated by commas, with fields that contain a comma or a double quote quoted and inner quotes doubled. Any other name gets TSV. Rules are left out. * Formulas orgstar evaluates Org's spreadsheet formulas, =org-table-recalculate= and =org-table-eval-formula=, natively: Calc expressions in a reimplementation of the part of Emacs Calc that tables use, and Lisp formulas in a small Emacs Lisp evaluator. What falls outside them is handed to Emacs on the Mac (see /When Emacs is needed/). Formulas live in a =#+TBLFM:= line right after the table, separated by =::=: #+BEGIN_SRC org | Item | Qty | Price | Total | |-------+-----+-------+-------| | Apple | 3 | 0.50 | 1.50 | | Pear | 12 | 0.75 | 9.00 | |-------+-----+-------+-------| | Sum | | | 10.50 | ,#+TBLFM: $4=$2*$3;%.2f::@>$4=vsum(@I..@II);%.2f #+END_SRC Blank lines between the table and =#+TBLFM:= are allowed. If there are several =#+TBLFM:= lines, recalculating the table uses the first; =C-c C-c= on another applies that line instead (=org-table-calc-current-TBLFM=). ** Kinds of formula | Left side | Kind | Applies to | |-------------------------+----------------------+------------------------------------------------------------------| | =$3= | Column formula | Every data row of column 3, except rows marked =!=, =^=, =_=, =$= or =/= in the first column. | | =$<=, =$>= | Column formula | The first or last column. | | =@2$3=, =@>$3= | Field formula | One field. Field formulas override column formulas. | | =@2$2..@4$3= | Range formula | Every field in the rectangle. | | =@4= | Row formula | Every field of data row 4. | | =$name= | Named field formula | The field named by a =^= or =_= row (see /Names, parameters and constants/). | A left side relative to the current row, such as =@-1$2=, is an error (=Unknown field=), and so is one relative to a rule, such as =@I$2=, as in Org. Two formulas for the same field are an error. ** References Rows count data lines from 1; rules don't count. Columns count from 1. | Reference | Meaning | |----------------------+----------------------------------------------------------------------| | =$2= | Column 2 in the current row. | | =$-1=, =$+1= | The column before or after the current one. | | =$<=, =$>=, =$>>= | The first column, the last, the one before last. | | =@3= | Row 3 in the current column. | | =@-1=, =@+1= | The row above or below. | | =@<=, =@>= | The first or last data row. | | =@I=, =@II=, =@III= | The first, second, third rule; as a row, the line after it. | | =@-I= | The rule above the current row. | | =@I+2= | Two data rows after the first rule. | | =@2$3= | Row 2, column 3. | | =$0= | The current column. | | =@0= | The current row: =@0$2= is =$2=. | | =@#=, =$#= | The current row's or column's number, as a value. | | =@2$1..@4$3= | A range: the fields of the rectangle, as a vector. | | =$1..$3= | Columns 1 to 3 of the current row. | | =@I..@II= | The current column from the first rule to the second. | In a Calc formula a single field becomes a number in parentheses, and a range becomes a vector such as =[1,2,3]=. Without the =E= flag, empty fields count as 0 on their own and are left out of ranges. A field's text goes into the formula as it is, so a field holding text such as =N/A= or =TBD= becomes Calc variables and the result is a formula: =$1+5= with =N/A= in =$1= gives =N / A + 5=. The =N= flag reads every field as a number instead, with text as 0. A field such as =21.11.2026= or =192.168.1.1=, with more than one decimal point, is =#ERROR=, as Calc can't read it as a number. A range whose only non-empty field holds text with a space, such as =5 kg=, needs Emacs: Calc reads a vector without commas as elements separated by spaces. ** Names, parameters and constants The first column can mark special rows, as in Org's spreadsheet: | Mark | Row | |-------+---------------------------------------------------------------------------------------| | =!= | Column names: =$qty= in a formula means the column whose =!= row field is =qty=. | | =^= | Names for the fields in the row above, usable as =$name=. | | =_= | Names for the fields in the row below. | | =$= | Parameters: fields like ~rate=0.2~, usable as =$rate=. | | =#= | Marked for recalculation. | | =*= | Marked for recalculation. | | =/= | Not recalculated. | When any row's first field is one of =!=, =^=, =_=, =$=, =#= or =*=, column formulas in a whole-table recalculation apply only to the rows marked =#= or =*=, as =org-table-calculate-mark-regexp= decides in Org. So a table with a =!= names row and no =#= rows gets no column formulas applied; mark the rows to calculate. Field formulas apply either way. Rows marked =!=, =^=, =_=, =$= or =/= are never changed by column formulas. #+BEGIN_SRC org | ! | qty | price | total | |---+-----+-------+-------| | # | 2 | 3 | 6 | | # | 4 | 0.5 | 2 | ,#+TBLFM: $4=$qty*$price #+END_SRC A =$name= that isn't a column name, parameter or named field is looked up as =org-table-get-constant= does: - in =#+CONSTANTS:= lines of the file or its setup file, written ~name=value~ and separated by spaces (~#+CONSTANTS: c=299792458 g=9.81~); - =$PROP_xyz= reads the =xyz= property of the entry holding the table, with inheritance. A name that isn't found becomes =#UNDEFINED_NAME=. A parameter named =%= in a =$= row, for example ~%=%.2f~, is put in front of the flags of every formula that has a =;=, so ~$3=$2/3;~ is formatted with =%.2f=. ** Remote references =remote(NAME, REF)= reads a field or range from another table: #+BEGIN_SRC org ,#+NAME: rates | item | rate | |------+------| | a | 2 | | b | 3 | | x | y | z | |---+---+---| | 4 | 8 | 5 | ,#+TBLFM: $2=$1*remote(rates,@2$2)::$3=vsum(remote(rates,@2$2..@>$2)) #+END_SRC =NAME= is found in this order: 1. a table after =#+NAME: NAME= or =#+TBLNAME: NAME= in the same file; 2. the first table in the entry whose =ID= property is =NAME=, in the same file; 3. the first table in the entry with that =ID= in any indexed file in your sidebar folders, read from the file on disk. =REF= may use spreadsheet-style references such as =B3= (column B, row 3), which become =@3$2=. A =REF= without a row, such as =$1..$2=, reads the row of the other table that has the current row's number. =NAME= can itself be a reference: in =remote($1,@1$1)=, the current row's first field holds the table's name. ** Calc expressions Calc formulas use Calc's number rules: integers are exact, and decimal numbers are rounded to 12 significant digits after every operation. Results are shown with up to 8 significant digits unless a format flag says otherwise (=org-calc-default-modes=). Operators, from lowest to highest precedence (the lowest, ~||~, is logical or: the left side if it is nonzero, else the right side): | Operator | Meaning | |------------------------------------+-----------------------------------------------------------| | =&&= | Logical and: the left side if it is zero, else the right side. | | ~==~, ~!=~, =<=, =>=, ~<=~, ~>=~ | Comparisons, giving 1 or 0. They can't be chained. | | =+=, =-= | Addition and subtraction. | | =/=, =%=, =\= | Division, modulo (sign of the divisor), integer division rounding down. | | =*= | Multiplication. Values side by side also multiply: =2 3=, =2 (x)=. It groups from the right: =a * b c= is =a * (b c)=. | | unary =-= | Negation. | | =^= | Power. | | =!=, =!!= after a value | Factorial, double factorial. | | =!= before a value | Logical not: =!x^2= is =(!x)^2= and =!x + 1= is =(!x) + 1=. | | =%= after a value | Percent: =5%= is =0.05=. Followed by a value, =%= is modulo instead. | Parentheses group, =[a, b, c]= writes a vector, =<2026-10-05 Mon>= writes a date, and =nan= is "not a number". =1:3= writes the fraction one third and =1:2:3= one and two thirds; =16#FF= writes an integer in base 16 (any base from 2 to 36). Functions: | Function | Result | |--------------------------------------------+---------------------------------------------------------------| | =vsum=, =vprod= | Sum or product of a vector. | | =vcount= | Number of elements. | | =vmean=, =vmedian= | Mean, median. | | =vgmean=, =vhmean= | Geometric mean, harmonic mean. | | =vmax=, =vmin= | Largest, smallest element. | | =vvar=, =vsdev= | Sample variance, sample standard deviation. | | =vpvar=, =vpsdev= | Population variance, population standard deviation. | | =vcov(x, y)=, =vpcov(x, y)=, =vcorr(x, y)= | Sample and population covariance, correlation coefficient. | | =vflat= | The arguments as one vector. | | =agmean(a, b)= | Arithmetic-geometric mean. | | =max(a, b, …)=, =min(a, b, …)= | Largest, smallest argument. | | =if(c, a, b)= | =a= if =c= is nonzero, else =b=. | | =abs= | Absolute value. | | =floor=, =ceil=, =trunc= | Round down, up, toward zero, to an integer. | | =round(x)=, =round(x, n)= | Round to an integer, or to /n/ decimal places. | | =rounde=, =roundu= | As =round=, with halves going to even, or up. | | =frac(x)= | The fraction nearest to /x/. | | =fdiv(a, b)= | /a/ divided by /b/ as a fraction. | | =float(x)=, =pfloat(x)= | /x/ as a decimal number. | | =sign= | -1, 0 or 1. | | =mod(a, b)=, =idiv(a, b)= | As =%= and =\=. | | =gcd(a, b)=, =lcm(a, b)= | Greatest common divisor, least common multiple. | | =fact=, =dfact= | Factorial and double factorial of a non-negative integer. | | =choose(n, m)=, =perm(n, m)= | Binomial coefficient and permutations, of integers. | | =sqrt=, =exp=, =ln=, =log10= | Square root, exponential, natural and base-10 logarithm. | | =log(x, b)= | Logarithm of /x/ to base /b/. | | =pow(x, y)=, =nroot(x, n)=, =exp10(n)= | /x/ to the power /y/, /n/-th root, 10 to an integer power. | | =hypot(a, b)= | The square root of /a/² + /b/². | | =sin=, =cos=, =tan= | Trigonometry, in degrees unless the =R= flag is set. | | =arcsin=, =arccos=, =arctan=, =arctan2(y, x)= | Inverse trigonometry, in degrees unless =R=. | | =sinh=, =cosh=, =tanh= | Hyperbolic functions. | | =arcsinh=, =arccosh=, =arctanh= | Inverse hyperbolic functions. | | =deg=, =rad= | Radians to degrees, degrees to radians. | | =eq=, =neq=, =lt=, =gt=, =leq=, =geq= | As ~==~, ~!=~, =<=, =>=, ~<=~ and ~>=~. | | =land=, =lor=, =lnot= | As =&&=, ~||~ and =!=. | The statistics functions take any number of vectors and numbers, as in =vmean($2..$4, $6)=. Calc leaves what it can't reduce to a number as a formula, and so does orgstar for sums, differences, products, quotients and powers: - A name is a variable: =2*x + x= is =3 x=. Calc's constants =pi=, =e=, =phi= and =gamma= stay names too: =$1^2*pi= gives =9 pi= for 3. - Division by zero stays unevaluated: =$1/$2= with 6 and an empty field gives =6/0=, and =x/0 + 1= gives =x / 0 + 1=. - =inf=, =-inf= and =uinf= (infinity of unknown sign) follow Calc's rules: =inf - inf= is =nan=, =1/inf= is =0=, =2^inf= is =inf=. - =vsum=, =vprod=, =vmean= and =vcount= take vectors holding such formulas. - =sqrt=, =abs=, =exp=, =ln=, =min=, =max=, =vmax=, =vmin= and =%= simplify a formula as Calc does, or keep it: =abs(-x)= is =abs(x)=, =sqrt(4 x)= is =2 sqrt(x)=, =vmax($2..$4)= over 4, =absent= and 8 is =max(max(4, absent), 8)=, and =x % 3= stays. - Comparisons, =&&=, =||=, =!= and =if= on a formula give a formula unless Calc can decide them: =x == x= is 1, =$2 > 10= with =TBD= in =$2= is =TBD > 10=, and =if($2 > 10, 1, 0)= is =TBD > 10 ? 1 : 0=. Other functions of a formula, =sqrt=, =exp= and =ln= of one under the =S= flag, and complex results such as =sqrt(-inf)= need Emacs. ** Mode flags and formats After the formula, a =;= starts its flags, as in ~$3=$1/$2;%.2f~ or ~$4=$1*2;f2~: | Flag | Meaning | |-----------+-------------------------------------------------------------------------------------------| | =nN= | Float format with /N/ significant digits. | | =fN= | Fixed format with /N/ decimal places. | | =sN= | Scientific format with /N/ digits. | | =eN= | Engineering format with /N/ digits. | | =pN= | Calc precision, 3 to 18 digits (12 by default). See below for limits. | | =D=, =R= | Angles in degrees (the default) or radians. | | =F= | Prefer fractions: =1/3= stays =1:3=. | | =N= | Treat every field as a number; text counts as 0. | | =S= | Symbolic: results that aren't exact stay as formulas, such as =sqrt(2)=. | | =E= | Keep empty fields: in ranges they stay in, and count as =nan= in Calc. | | =L= | Literal: in Lisp formulas, insert fields as they are written. | | =T= | Durations: read =H:MM= and =H:MM:SS= fields as times, show the result as =HH:MM:SS=. | | =U= | As =T=, showing =HH:MM=. | | =t= | As =T=, showing decimal hours with two places, such as =1.50=. | Above =p14=, functions worked out from decimal approximations (=exp=, =ln=, =log=, non-integer powers, =sinh= and =cosh=) may need Emacs, and so may =tanh= above =p12=. Above =p16= so may the statistics functions, powers of decimal numbers, =sqrt= of a non-square and the trigonometric functions. With =S=, the functions that may give an inexact result (=sqrt=, =exp=, =ln=, =log=, =log10=, =pow=, =nroot=, =hypot=, the inverse trigonometric and hyperbolic functions, =fact=, =dfact=, the standard deviations, =vgmean=, =vhmean=, the covariances and =agmean=), powers other than integer powers, and =sin=, =cos= and =tan= in radians need Emacs. The flag =u= needs Emacs. Any other text after the flags is a =format= string applied to the result, such as =%.2f= or =%d=; =format= supports =%s=, =%S=, =%d=, =%o=, =%x=, =%X=, =%c=, =%e=, =%f= and =%g=. ** Durations With =T=, =U= or =t=, fields like =1:30= or =10:00:30= are read as hours, minutes and seconds: #+BEGIN_SRC org | start | end | sum | diff | hours | |----------+---------+----------+-------+-------| | 1:30 | 0:45 | 02:15:00 | 00:45 | 3.00 | | 10:00:30 | 2:15:10 | 12:15:40 | 07:45 | 20.02 | ,#+TBLFM: $3=$1+$2;T::$4=$1-$2;U::$5=$1*2;t #+END_SRC ** Dates Timestamps in fields, active or inactive, take part in Calc arithmetic as dates. The difference of two dates is a number of days, with a fraction when the timestamps have times. A date plus a number is a date, written back as an inactive timestamp: #+BEGIN_SRC org | start | end | days | later | |------------------+------------------+------+------------------| | <2026-10-05 Mon> | <2026-10-12 Mon> | 7 | [2026-10-12 Mon] | ,#+TBLFM: $3=$2-$1::$4=$1+7 #+END_SRC A date is its day number underneath, counted from 1 January of year 1, so the sum of two dates is a number and =vmean= of two dates is the day number halfway between them. =vsum=, =vmax=, =vmin=, =max=, =min=, =abs= and the comparisons work on dates; =floor=, =ceil=, =trunc= and =round= keep a date and drop or round its time. A timestamp with a time, even =00:00=, keeps showing one. Calc leaves =abs= of day 0 (=<-1-12-31>=) unevaluated, so that needs Emacs. | Function | Result | |--------------------------------------------+------------------------------------------------------------------------------------| | =year=, =month=, =day= | Parts of the date. | | =hour=, =minute=, =second= | Parts of the time, 0 for a date without one. | | =weekday= | 0 for Sunday to 6 for Saturday. | | =yearday= | Day of the year, 1 for 1 January. | | =date(y, m, d)=, =date(y, m, d, h, mi, s)= | The date, with a time when an hour is given. =date(m, d)= is this year. | | =date(d)= | The day number of date /d/, or the date of day number /d/. | | =now()= | The current date and time. | | =incmonth(d, n)=, =incyear(d, n)= | /n/ months or years later (1 by default), on the last day if the month is shorter. | | =newmonth(d, n)=, =newyear(d, n)= | Day /n/ of the month or year (1 by default; 0 is the last day). | | =newweek(d, w)= | The day on or before /d/ that falls on weekday /w/ (0, Sunday, by default). | | =badd(d, n)=, =bsub(d, n)= | /n/ business days later or earlier, skipping Saturdays and Sundays. | | =bsub(d1, d2)= | Business days from /d2/ to /d1/. | | =julian(d)= | The Julian day number of /d/, or the date of a Julian day number. | | =unixtime(d)= | Seconds since 1970-01-01 00:00 UTC, or the date of a number of seconds. | As in Calc, a timestamp year under 100 is read as 19xx or 20xx: 00 to 39 is 20xx, 40 to 99 is 19xx. =badd= and =bsub= work only for dates in the years 1 to 2737, the range of Calc's holiday table; outside it they need Emacs, which leaves them unevaluated. =julian= of a date with a time and =unixtime= use the local time zone. Emacs applies the current year's daylight saving rules to every date, which orgstar doesn't, so in a time zone with daylight saving time these need Emacs. With =N=, =T=, =U= or =t= a timestamp field counts as 0. A result that is a date is =#ERROR= with =N=, and with =T=, =U= or =t= it stops the recalculation with the error Org gives. ** Lisp formulas A formula that starts with ='(= is Emacs Lisp. Each reference becomes a Lisp string, or a number with =N=, or the field's text inserted as is with =L=. A range becomes the values separated by spaces, so wrap it in a quoted list: #+BEGIN_SRC org | name | greeting | |-------+----------| | Ada | Ada! | | Grace | Grace! | ,#+TBLFM: $2='(concat $1 "!") | n | |---| | 1 | | 2 | |---| | 3 | ,#+TBLFM: @>$1='(apply '+ '(@I..@II));N #+END_SRC The evaluator supports: - special forms: =quote=, =function=, =lambda=, =progn=, =prog1=, =if=, =when=, =unless=, =cond=, =and=, =or=, =let=, =let*=, =setq=, =push=, =pop=, =while=, =dolist=, =dotimes=, =with-output-to-string=, =ignore-errors=, =condition-case=, =save-match-data=, =defun=, =defvar=, =defconst=, =let-alist=, =cl-incf= and =cl-decf= on a variable, with dynamic binding, as Org evaluates formulas; - =pcase= with literal, symbol, quoted, backquoted and =pred= patterns, where =pred= takes a function name or a lambda; - =cl-loop= with =for VAR in LIST=, =for VAR from START to END=, and one kind of =collect= or =sum=, with =do=; - arithmetic: =+=, =-=, =*=, =/=, =%=, =mod=, =1+=, =1-=, =abs=, =max=, =min=, =float=, =floor=, =ceiling=, =round=, =truncate=, ~=~, =<=, =>=, ~<=~, ~>=~, ~/=~, =zerop=, =expt=, =sqrt=, =exp=, =log=, =sin=, =cos=, =tan=, =asin=, =acos=, =atan=, =isnan=, =random=, =logand=, =logior=, =logxor=, =ash=, and the variables =float-pi=, =pi= and =float-e=; - predicates: =not=, =null=, =eq=, =eql=, =equal=, =numberp=, =integerp=, =floatp=, =stringp=, =listp=, =consp=, =symbolp=; - lists: =car=, =cdr=, =cadr=, =cddr=, =cons=, =list=, =nth=, =nthcdr=, =elt=, =aref=, =append=, =length=, =reverse=, =number-sequence=, =memq=, =member=, =memql=, =assoc=, =assq=, =delq=, =delete=, =mapcar=, =mapc=, =mapconcat=, =funcall=, =apply=, =identity=, =ignore=, =last=, =butlast=, =delete-dups=, =nreverse=, =sort= (both calling conventions; these three need Emacs when they would change a list that another variable, list or argument may share), =seq-reduce=, =seq-filter=, =seq-remove=, =seq-map=, =seq-uniq=, =cl-reduce= (with =:initial-value=), =cl-remove-if=, =cl-remove-if-not=, =cl-find= and =cl-position= (with =:test=); - strings: =concat=, =format=, =format-message=, ~string=~, =string-equal=, =string<=, =string-lessp=, =upcase=, =downcase=, =capitalize=, =substring=, =string-to-number=, =number-to-string=, =int-to-string=, =string-prefix-p=, =string-suffix-p=, =string-empty-p=, =string-trim=, =split-string=, =prin1-to-string=, =string-join=, =string-to-char=, =char-to-string=, =string-pad=, =string-width=, =string-search=, =string-replace=; - regular expressions: =string-match=, =string-match-p=, =match-beginning=, =match-end=, =match-string=, =replace-match= and =replace-regexp-in-string= on strings, and =regexp-quote=; - time: =current-time=, =format-time-string=, =float-time=, =time-add=, =time-subtract=, =time-to-days=, =date-to-time=, =days-between=, =org-time-string-to-time=, =org-time-string-to-absolute= and the =calendar-= functions that diary sexps use, in the local time zone and the proleptic Gregorian calendar; - output and errors: =princ=, =prin1=, =print=, =terpri=, =message=, =error=, =user-error=; - Org's lookup functions =org-lookup-first=, =org-lookup-last= and =org-lookup-all=. Regular expressions use Emacs's syntax and the syntax table of an Org buffer, and =case-fold-search= is on unless the formula binds it to =nil=. These need Emacs: numbered groups =\\(?N:=, categories =\\c=, syntax classes and character classes such as =\\w=, =\\b= or =[[:alpha:]]= against text with characters past ASCII, and case folding of a letter past ASCII, including =capitalize=. =condition-case= binds its variable to the error symbol and data, such as =(wrong-type-argument listp 1)=; reading that variable for an error whose data orgstar doesn't know, such as =args-out-of-range=, needs Emacs, as does a handler that names a specific condition when the error isn't one of =wrong-type-argument=, =args-out-of-range=, =arith-error=, =void-variable=, =invalid-function=, =wrong-number-of-arguments=, =error= and =user-error=. =format-time-string= takes =%Y=, =%y=, =%m=, =%d=, =%e=, =%H=, =%k=, =%I=, =%l=, =%M=, =%S=, =%p=, =%a=, =%A=, =%b=, =%h=, =%B=, =%u=, =%w=, =%j=, =%F=, =%D=, =%R=, =%T=, =%s=, =%z=, =%n=, =%t= and =%%=, without flags or widths; other codes need Emacs, as do float times in =time-add= and =time-subtract=. =date-to-time= and =days-between= read dates as =YYYY-MM-DD=, with an optional =HH:MM= or =HH:MM:SS=. Any other function, or a macro not listed, needs Emacs. As in Emacs 31, =lambda= and =function= make function objects rather than lists, so a formula whose value is one, or that prints one or takes it apart as a list, needs Emacs; calling one, or passing it to a function that calls it, doesn't. An error inside a Lisp formula writes =#ERROR= in the field, as does a Calc error. So does a value that is a non-empty list, ignoring the format; with =T=, =U= or =t= such a value needs Emacs. =org-sbe= writes the value of a named src block, run with the variables given: #+BEGIN_SRC org ,#+NAME: square ,#+begin_src emacs-lisp :var n=2 (* n n) ,#+end_src | n | square | |---+--------| | 3 | 9 | ,#+TBLFM: $2='(org-sbe square (n $1)) #+END_SRC As in Org, a field's text is read as a value, so =abc= names another block or table; write =$$1= instead of =$1= to pass the text as a string. A string before the variables holds header arguments, and the name may also be a named table or list in the file. As in Org, a name under a =COMMENT= heading is skipped, and the name's case must match. =org-sbe= needs Pro's source blocks feature. Emacs Lisp blocks run in orgstar's interpreter, with dynamic binding unless the block has =:lexical yes= or =:lexical t=, and =js= blocks run in JavaScriptCore, everywhere and without asking, since neither can reach files or other programs. After five seconds orgstar stops waiting for a =js= block and the table needs Emacs; a block that never finishes keeps running in the background until orgstar quits, and isn't started again while it runs. Blocks in other languages, blocks with =:cache yes=, =:file= results or column and row names, other =:lexical= values, values that hold a function, Lisp values with symbols in a variable with several values, a name given to more than one element, in any case, when the first isn't a src block, references to an =ID= or =CUSTOM_ID=, and =js= values that are arrays need Emacs. When Emacs runs a table in a file that uses =org-sbe=, it runs the blocks without asking again, so orgstar asks =This table's formulas run Lisp and source blocks. Run them? (yes, no)= each time. * Entering formulas There are three ways to set a formula. *In the field.* Type ~=expr~ in a field and press =TAB= or =RET=: orgstar stores ~$N=expr~ as the column's formula and evaluates it, as =org-table-maybe-eval-formula= does. Type ~:=expr~ instead to store a field formula ~@R$C=expr~. *With a prompt.* =org-table-eval-formula= asks for the formula (~Column formula $N=~ or ~Field formula @R$C=~) with the stored one filled in, stores it and evaluates it in the current field. Clearing the prompt and pressing =Return= removes the stored formula, as in Emacs, and the echo area shows =Formula removed=. You can also delete a formula in the formula editor below or from the =#+TBLFM:= line. | Command | Emacs | Mac | Doom | |-------------------------------+----------+-----------------------------+----------| | Org ▸ Set Column Formula | ~C-c =~ | Org ▸ Set Column Formula | ~C-c =~ | | Org ▸ Set Field Formula | none | Org ▸ Set Field Formula | none | Org's ~C-u C-c =~ for field formulas has no equivalent key, because orgstar has no prefix argument; use the menu item or type ~:=~ in the field. *In the formula editor.* =C-c '= in a table or on its =#+TBLFM:= line (=org-table-edit-formulas=) opens the formulas in an editor of their own, one per line, grouped under =# Column Formulas=, =# Field and Range Formulas= and =# Named Field Formulas=. A formula can continue on indented lines. =C-c '= or =⌘Return= installs the formulas; =Escape= or =C-c C-k= leaves them unchanged. Installing doesn't recalculate. The echo area says so and points to Recalculate Table: =C-c C-c= on the =#+TBLFM:= line, or =⌃⌘X= there in the Mac preset. The formulas are stored sorted the way =org-table-formula-less-p= sorts them. * Recalculating | Command | Org command | Emacs | Mac | Doom | |----------------------------------+-----------------------------------+-------------------------------+-----------------------------+-------------------------------------| | 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:= | | Recalculate Table Row | =org-table-recalculate= | =C-c *= | =⌃⌘*= (=⌃⇧⌘8= on a US keyboard) | =C-c *= | | Iterate Table Formulas | =org-table-iterate= | =C-u C-u C-c *= | — | — | In Doom's normal state, =RET= in a table recalculates it when it has a =#+TBLFM:= line and aligns it otherwise; on a =#+TBLFM:= line it recalculates. When the table has a rule below its first data row, recalculating the whole table leaves the rows above that rule, the header, alone. With marked rows, the marks decide instead (see /Names, parameters and constants/). Each command evaluates the column formulas row by row, then the field formulas, then aligns the table. Each formula is evaluated once. Iterate Table Formulas recalculates the whole table again and again until it stops changing, for formulas that depend on each other's results, at most ten times. The echo area says =Convergence after 3 iterations=, or =Table was already stable=; after ten passes that still change the table it says =No convergence after 10 iterations=, and the ten passes stay. =C-u C-c *= also recalculates the whole table in the Emacs preset. A formula that fails, such as one whose reference leads outside the table, stops the command there. As in Emacs, the fields written before it stay, the table isn't aligned, the caret goes to the start of its field (on a =#+TBLFM= line it stays put), and the echo area shows the error. =TAB=, =S-TAB= and =RET= in a row marked =#= stop there too, without moving on. A row marked =#= is recalculated when you press =TAB=, =S-TAB= or =RET= in it, before the caret moves (=org-table-maybe-recalculate-line=), and with =C-c C-c= (Mac =⌃⌘X=). Other rows are recalculated only by the commands above. Emacs skips a second automatic recalculation of the same row by the same command in a row; orgstar recalculates each time, which gives the same result unless a formula depends on its own value. * When Emacs is needed When a formula uses something the native evaluator doesn't have, the command reports what it was and the Mac recalculates the table in Emacs instead. This happens for: - symbolic results beyond those under /Calc expressions/, such as a function of a field holding text; - Calc functions not listed above, intervals such as =[1..5]=, precisions beyond those given under /Mode flags and formats/, parts of the =S= flag, and the =u= flag; - complex results, vectors of anything but numbers, numbers too large for 64-bit integers, and decimal numbers past Calc's limit of 10^±4000000; - Lisp functions not listed above. The echo area shows =Recalculating in Emacs (reason)…=, then =Recalculated in Emacs=. orgstar runs =emacs -Q --batch= on a copy of the file, so your Emacs init file, packages and customizations are not loaded, then replaces the table with Emacs's result. It looks for Emacs at the path in the =ORGSTAR_EMACS= environment variable, then =/opt/homebrew/bin/emacs=, =/usr/local/bin/emacs=, =/Applications/Emacs.app/Contents/MacOS/Emacs=, =/run/current-system/sw/bin/emacs= and =/usr/bin/emacs=. If none is found, the echo area shows the reason, as =This table's formulas can't be calculated here: reason.=, and the table is unchanged. Emacs gets your =PATH= with =/opt/homebrew/bin=, =/usr/local/bin=, =/Library/TeX/texbin=, =/usr/bin= and =/bin= added, as code blocks and export do, so programs a formula starts are found when orgstar was opened from the Dock. The run stops after 60 seconds. If you edit the table while Emacs is working, its result is discarded. Edit ▸ Cancel Running Task (=⌘.=) stops the recalculation; the echo area shows =Recalculation canceled= and the table is unchanged. Starting another recalculation or export in Emacs cancels the one that is running. The [[file:../install.org][Mac App Store version]] doesn't run Emacs, so it recalculates only the tables orgstar computes itself. In any other table it applies the formulas in Emacs's order up to the first one it can't compute and stops there, as Emacs does when a formula fails: the fields written before it stay, unaligned, and the rest are unchanged. The echo area shows =The table was recalculated up to (formula), which can't be calculated here: (reason).=, where /formula/ is the left-hand side, such as =$4=. When nothing was written, it shows =This table's formulas can't be calculated here: (reason).= and the table is unchanged. If the table's formulas contain Lisp, orgstar asks first: =This table's formulas run Lisp. Run them? (yes, no, always)=. =always= trusts this table's text in this file, so the question isn't asked again until the text changes. Entering a formula that needs Emacs with ~C-c =~, Set Field Formula or ~=~ in a field is not handed to Emacs. The formula is stored in the =#+TBLFM:= line and the field is left as it was; the echo area shows =The formula was stored; recalculating it needs Emacs= and the reason. =TAB= and =RET= still move to the next field. Recalculate the table to run the formula in Emacs. In the Mac App Store version the echo area shows =The formula was stored; it can't be calculated here:= and the reason, and recalculating doesn't run it. For how orgstar and Emacs share files, see [[file:15-alongside-emacs.org][Using orgstar alongside Emacs]]. * table.el tables Tables drawn with =+= corners and =-= and =|= borders, as the =table.el= package makes them, are recognized and kept as written: #+BEGIN_SRC org +-------+-------+ | Name | Value | +-------+-------+ | a | 1 | +-------+-------+ #+END_SRC Edit the text in the cells as usual. =TAB= and =S-TAB= line the borders up, then move to the next or previous cell, wrapping around at the ends of the table, as table.el's =table-forward-cell= and =table-backward-cell= do; the caret goes to the cell's first character. =C-c C-c= (Mac =⌃⌘X=) lines the borders up without moving. Lining up makes each column as wide as its widest text, and never narrower than its rules: text typed into a cell uses up the spaces at its end first, as table.el does when you type. Org edits table.el tables in table.el's own buffer, with =C-c '=, and says so for =TAB= and =C-c C-c=; orgstar does the editing in place instead. Tables with merged cells, which span rows or columns, can be moved through but not lined up ("Can't align a table.el table with merged cells"). Formulas, sorting and the other table commands don't apply. HTML export renders them as tables, with cells that span rows and columns, and Markdown export writes them as a code block. See [[file:12-export.org][Export]]. * Plotting Plot Table (=org-plot/gnuplot=, =C-c " g= in the Emacs and Doom presets, and in the command palette; More › Table on iOS) draws the table at the caret, or the first table after it, as the =#+PLOT:= lines above it say: #+BEGIN_SRC org #+PLOT: title:"Sales" ind:1 deps:(2 3) type:2d with:linespoints file:"sales.png" | month | north | south | |-------+-------+-------| | 1 | 20 | 12 | | 2 | 25 | 18 | #+END_SRC | Option | Meaning | |------------+-----------------------------------------------------------------------------| | =type= | =2d= (the default), =3d=, =grid= or =radar= | | =ind= | The column of x values, counting from 1; 0 (the default) plots against row numbers | | =deps= | The columns to plot, as a list such as =(2 3)=; all but =ind= by default | | =with= | =lines= (the default), =points=, =linespoints=, =impulses=, =boxes=, =histograms=, =steps=, =fsteps= | | =title= | The plot's title | | =file= | Where the image goes, relative to the Org file's folder; without it the plot shows in a sheet | | =labels= | Names for the columns, as a list; a header row above a rule names them otherwise | | =map= | With =t=, a =3d= plot is seen from above | | =ymin=, =ymax=, =xmin=, =xmax= | The axes' ranges; =min= and =max= are =ymin= and =ymax= | | =ticks= | How many marks the y axis has; for =radar=, how many rings the web has | | =transpose= | With =yes=, rows become columns | | =set=, =line=, =script=, =timefmt= | gnuplot commands, a line of gnuplot, a gnuplot script file and the time format, for gnuplot | Options are read as Org reads them, quirks included: an option's name also matches inside a longer word (=timeind:t= sets =ind=), an unquoted value ends at a =.=, =,= or =;=, =set:= options before the last =line:= are dropped, and each =set:= and =line:= that's kept comes twice, as Org reads the options twice. An ind column of timestamps is plotted as dates, and one of text as labels. A =radar= plot draws each row as an axis, clockwise from the top, and each column after the first as a series named by the header. Each axis is scaled from its row's lowest and highest values rounded outward (120 to 340 becomes 100 to 400), or from =ymin= to =ymax=. Org works out the number of rings from the values; where Org can't (it signals an error, as for a =ymin= above a row's values, or never finishes, as for a row whose values are all the same), orgstar reports it and asks for =ticks:=. =ticks:= can't be below 0, and without gnuplot it takes at most 1000. A row whose values or their range overflow a double is refused, as Emacs signals an error for it. On the Mac, when gnuplot is installed (orgstar looks on the same =PATH= as code blocks), the table goes to gnuplot as Emacs sends it: the same data file and script, so every option and gnuplot's own terminals and styles apply. A plot without =file:= is drawn to an image and shown in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d= as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a file), =radar= as a web. =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as gnuplot's default, or =.svg= for a =2d= plot. A log axis places values from 1e-307 to 1e308 and leaves out others; it needs at least one, and its range, if given, must lie within them. If inline images are showing, a link to the file shows the new plot. Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=: | Command | Effect | |----------------------------------------+-------------------------------------------------------------------------| | =title 'T'= | The title, over =title:= | | =xlabel 'X'=, =ylabel 'Y'= | The axes' names | | =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data | | =logscale=, =logscale x=, =logscale y= | Logarithmic axes | | =grid= | Grid lines at the marks | | =key off=, =key top left= | Hides or places the legend (top, bottom, left, right; else top right) | | =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic | =2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or log scale. =grid= and =3d map:t= plots follow the title and axis names; other =3d= plots and =radar= plots only the title. Other commands, other =line:= options and =script:= are left out, and the message after plotting names them, such as =Not followed without gnuplot: set:"style fill solid"=. The [[file:../install.org][Mac App Store version]] doesn't use gnuplot: it always draws the plot itself in the same way. * On iOS The iOS app evaluates formulas natively with the same engine. Tables that need Emacs are not recalculated there; the app reports =This table needs Emacs, which runs on the Mac= with the reason. Import and export are under More › Table, with Plot Table. Narrow columns work in the editor, as described under /Narrow columns/. See [[file:14-ios.org][iOS]].