9

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.

jotsetung
  • 121
  • 1
  • 4

3 Answers3

1

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

fniessen
  • 4,408
  • 19
  • 18
  • 1
    I tried you approach and defined the macro `#+MACRO: image_width 16`. in plain text in the org file this works perfect and as expected `{{{image_width}}}` will be replaced with _16_ in the exported file. However, when I add it to a LaTeX attribute like `#+ATTR_LATEX: :center :placement [H] :width {{{image_width}}}cm` I get `\includegraphics[width={{{image_width}}}cm]{test.png}` and not `\includegraphics[width=16cm]{test.png}`. So, that's not working for me. – jotsetung Aug 27 '13 at 19:20
  • You should report this as a feature at least. I do have the impression that it should well work. – fniessen Sep 04 '13 at 15:17
1

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.

nuaavee
  • 1,336
  • 2
  • 16
  • 31
-2

Define a variable in your .emacs. Just like how you'd define a function, you can bind a variable to certain value. You can do setq or defvar, defconst, depending on what you want. For an instance:

(setq image-width 10)

Make sure to eval-buffer after you set your variable.

jimmu
  • 1,025
  • 1
  • 10
  • 15
  • thanks for pointing me out how to define a variable. How can I get then the value, i.e. the 10 inside the org-file? Ideally the actual value should be included in the org file **before** the export of the org-file. – jotsetung Aug 27 '13 at 18:15