Passing Variables to Source Blocks
In which I document ways to define variables in an org file, and use those
variables in SRC
blocks throughout the rest of the file.
Global header args
Define variables globally in an org file using #+PROPERTY
. Notice that the
values can be elisp expressions. values.
NOTE: when changing a variable defined with #+PROPERTY
, the buffer needs to
be reloaded to take effect. Use M-x revert-buffer
to reload the buffer.
echo $globalVar1 echo $globalVar2 echo $globalVar3
Global Var 1 Value 5 4
Elisp association list
Define an elisp association list (alist) that maps variable names to values, and a lookup function for looking up a variable's value by name.
'(("alistVar1" "Alist Var 1 Value", "Alist var 1 description") ("alistVar2" 7 "Alist var 2 description"))
(nth 1 (assoc v data))
echo $v1 echo $v2
Alist Var 1 Value 7
Org table
Define an org table that maps variable names to values, and a lookup function for looking up a variable's value by name. Under the covers, the table is just an elisp alist.
Var Name | Value | Description |
---|---|---|
tableVar1 | Table Var 1 Value | Table var 1 description |
tableVar2 | 6 | Table var 2 description |
(nth 1 (assoc v data))
echo $v1 echo $v2
Table Var 1 Value 6
Drawer header args
These variables apply to all source blogs within this section.
echo $drawerVar1 echo $drawerVar2
Drawer Var 1 8
Drawer header args using table
Same as above, but look up variable values from a table (specifically, the table in section Org table
).
NOTE Calling functions in header-args can be problematic because of a bug with org mode, described here.
echo $drawerTable1 echo $drawerTable2
Table Var 1 Value 6