Search code examples
emacsorg-mode

Is it possible to name variables throughout an org mode document and then calculate it somewhere without using tables?


I would like to set variables scattered throughout a document and then total them at some place in the document.

For example,

#+NAME: apples 10

And then I have some

#+NAME: pears 5

And a some point later

Total: $apples + $pears

Result: 15


Solution

  • You could define the variables inline using elisp (or any other language, eg. src_emacs-lisp{...}).

    For example,

    I have src_emacs-lisp{(setq apples 10)} apples.
    
    and src_emacs-lisp{(setq pears 5)} pears.
    
    for a total of: src_emacs-lisp{(+ apples pears)} fruits.
    

    Alternatively, using named blocks like you're example, the values could be defined in source blocks and summed later in another block or inline. This seems like a bit more work - I'm not sure how to define the name/value inline without the source block.

    #+NAME: apples
    #+BEGIN_SRC emacs-lisp :var apples=10 :results none
    apples
    #+END_SRC
    
    #+NAME: pears
    #+BEGIN_SRC emacs-lisp :var pears=5 :results none
    pears
    #+END_SRC
    
    Total = src_emacs-lisp{(+ apples pears)}