Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs org-mode: define variable in buffer and access variable

I would like to define a buffer wide variable in an org-file and use the value from this variable later for, e.g., define the width of images for latex export.

Is there a way how this can be done?

Can this be done using #+CONSTANTS:?

Ideally, it should work like this:

I define a variable image_width in a buffer, e.g.

image_width=10

and use this variable in for #+ATTR_LATEX: settings, e.g., #+ATTR_LATEX: :width $image_width.

like image 344
jotsetung Avatar asked Aug 27 '13 12:08

jotsetung


2 Answers

Two ways (untested), if you want your constant to be buffer-specific:

  • "Local Variable(s)" in the last 8 lines (or so) of your Org file;

  • Use Org "#+MACRO:" feature

like image 180
fniessen Avatar answered Nov 09 '22 10:11

fniessen


You can declare a variable using the #+NAME: tag. Then use it by passing it to :var in the src block you need it.

This will look something like this:

#+NAME: instance-id
| abcd |

#+BEGIN_SRC sh :var id=instance-id
echo $id
#+END_SRC

#+RESULTS:
: abcd

#+BEGIN_SRC python :var myvar=instance-id[0, 0]
print myvar
#+END_SRC

#+RESULTS:
: abcd

Note the instance-id[0, 0] in python src block. I've declared the variable as an array because of which I need to de-reference the value like this.

like image 43
nuaavee Avatar answered Nov 09 '22 10:11

nuaavee