Consider the followig python code block

#+name: whatever
#+header: :var x="2*np.pi"
#+BEGIN_SRC python
import numpy as np
# .

The code will break, because x is passed to the interpreter as a string and then added together with an integer which is not legal (the trick would work in Maxima for some reason, though). If I remove the quotes ("), then Org complains that it can't find a reference (or, something such). So, how to insert an expression such as 2*np.pi to a code block?


I don't think you can use a language expression as the value to a :var, the doc says:

Arguments can take values as literals, or as references, or even as Emacs Lisp code

A possible way to achieve this is to use another code block to compute the expression, then use it as the value to the :var:

"code block with arguments"

 a 'src' code block name, as assigned by '#+NAME:', followed by
 parentheses and optional arguments passed within the parentheses
 following the `src' code block name using standard function call

You example could then be:

#+name: twotimesnppi
#+BEGIN_SRC python
import numpy as np
return 2*np.pi

#+RESULTS: twotimesnppi
: 6.28318530718

#+name: whatever2
#+header: :var x=twotimesnppi()
#+BEGIN_SRC python
return 1+x

#+RESULTS: whatever2
: 7.28318530718
| improve this answer | |
  • That solution seemed like an overkill so i went for an uglier, but shorter one: eval. Since x ends up being a string, eval(x) is going to give the desired result in case of Python. – Mali Remorker Jul 24 '17 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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