- publishing free software manuals
The Org Mode 7 Reference Manual
by Carsten Dominik and others
Paperback (6"x9"), 282 pages
ISBN 9781906966089
RRP £9.95 ($14.95)

Sales of this book support the Org project! Get a printed copy>>>

14.8.2.1 :var

The :var header argument is used to pass arguments to code blocks. The specifics of how arguments are included in a code block vary by language; these are addressed in the language-specific documentation. However, the syntax used to specify arguments is the same across all languages. The values passed to arguments can be literal values, values from org-mode tables and literal example blocks, or the results of other code blocks.

These values can be indexed in a manner similar to arrays--see the “indexable variable values” heading below.

The following syntax is used to pass arguments to code blocks using the :var header argument.

:var name=assign

where assign can take one of the following forms

Alternate argument syntax

It is also possible to specify arguments in a potentially more natural way using the #+source: line of a code block. As in the following example arguments can be packed inside of parenthesis, separated by commas, following the source name.

#+source: double(input=0, x=2)
#+begin_src emacs-lisp
(* 2 (+ input x))
#+end_src

Indexable variable values

It is possible to reference portions of variable values by “indexing” into the variables. Indexes are 0 based with negative values counting back from the end. If an index is separated by ,s then each subsequent section will index into the next deepest nesting or dimension of the value. The following example assigns the last cell of the first row the table example-table to the variable data:

#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |

#+begin_src emacs-lisp :var data=example-table[0,-1]
  data
#+end_src

#+results:
: a

Ranges of variable values can be referenced using two integers separated by a :, in which case the entire inclusive range is referenced. For example the following assigns the middle three rows of example-table to data.

#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | 3 |

#+begin_src emacs-lisp :var data=example-table[1:3]
  data
#+end_src

#+results:
| 2 | b |
| 3 | c |
| 4 | d |

Additionally, an empty index, or the single character *, are both interpreted to mean the entire range and as such are equivalent to 0:-1, as shown in the following example in which the entire first column is referenced.

#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |

#+begin_src emacs-lisp :var data=example-table[,0]
  data
#+end_src

#+results:
| 1 | 2 | 3 | 4 |

It is possible to index into the results of code blocks as well as tables. Any number of dimensions can be indexed. Dimensions are separated from one another by commas, as shown in the following example.

#+source: 3D
#+begin_src emacs-lisp
  '(((1  2  3)  (4  5  6)  (7  8  9))
    ((10 11 12) (13 14 15) (16 17 18))
    ((19 20 21) (22 23 24) (25 26 27)))
#+end_src

#+begin_src emacs-lisp :var data=3D[1,,1]
  data
#+end_src

#+results:
| 11 | 14 | 17 |
ISBN 9781906966089The Org Mode 7 Reference ManualSee the print edition