2

I have a variable defined in my heading that I would like to use in one text source block that I tangle to a file.

Example:

* Test
:PROPERTIES:
:header-args:  :var foo=222
:END:

... use foo in some shell scripts ...

#+BEGIN_SRC text :tangle config-file
I want to include value of FOO here
#+END_SRC
  • Org header variables are intended for use in the evaluation of a code block, not when a block is tangled to a file. However, you might be able to write an export function for the tangling that does what you want. A related problem and example code is at emacs.stackexchange.com/questions/44958/… – Win Dec 21 '18 at 18:27
1

Use built-in :noweb functionality

  1. Add named elisp src block to return the variable value.

    #+NAME: foo-value
    #+BEGIN_SRC elisp :exports none 
      foo
    #+END_SRC
    
    #+RESULTS: foo-value
    : 222
    

    Note: I choose elisp for this example because elisp code is enabled by default. This trick will also work with other languages, e.g.ruby and python.

  2. Add :noweb yes header to the src block that will be tangled.

    #+BEGIN_SRC text :noweb yes :tangle config-file 
      I want to include value of <<foo-value()>> here.
    #+END_SRC
    

    When the file is tangled, the <<foo-value()>> will be replaced with 222.


Below is the updated version of your example code:

* Test
:PROPERTIES:
:header-args:  :var foo=222
:END:

#+NAME: foo-value
#+BEGIN_SRC elisp :exports none 
  foo
#+END_SRC

#+RESULTS: foo-value
: 222

#+BEGIN_SRC text :noweb yes :tangle config-file 
  I want to include value of <<foo-value()>> here.
#+END_SRC

The code in this answer was tested using
emacs version: GNU Emacs 26.1
org-mode version: 9.1.14


Thanks for asking a great question! Hope that helped!

0

If I understand your question, the following does what you want:

* Test  
:PROPERTIES:
:header-args:  :var foo=222
:END:

#+BEGIN_SRC bash :tangle tangle.txt
echo $foo
#+END_SRC

I can tangle this with C-c C-v t, which produces the file tangle.txt:

foo='222'
echo $foo

You have to specify what kind of code your code block is. In my example, a bash script. If you just specify text as you did, org won't know what syntax to use to define your variable.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.