3

I get error "reference y not found in this buffer" when trying to evaluate cubed-plus-cubed:

#+NAME: cubed
#+BEGIN_SRC python :var x=3
return x * x * x
#+END_SRC

#+RESULTS: cubed
: 27

#+NAME: cubed-plus-cubed
#+BEGIN_SRC python :var y=0 :noweb eval
<<cubed(x=y)>> + <<cubed(x=y)>>
#+END_SRC

Why does not it work?

I also imagine my philosophy here could be all wrong and all this is done very differently in practice.

user3496846
  • 378
  • 1
  • 10

1 Answers1

3

Try this

The reason why the error happens is because you haven't defined y in the x=y arguments.

If you update code as follows noweb will work

#+NAME: cubed-plus-cubed
#+BEGIN_SRC python :noweb eval
return <<cubed(x=3)>> + <<cubed(x=3)>>
#+END_SRC

#+RESULTS: cubed-plus-cubed
: 54

However I suspect this is not what you wanted because originally you added :var y=0

Another way to pass the value of variable x is to update your code as follows:

example 1

#+NAME: cubed-ex1
#+BEGIN_SRC python :var x=3 :session
  x * x * x
#+END_SRC

#+RESULTS: cubed-ex1
: 27

#+NAME: cubed-plus-cubed-ex1
#+BEGIN_SRC python :var x=3 :noweb eval :session
  <<cubed-ex1>> + <<cubed-ex1>>
#+END_SRC

#+RESULTS: cubed-plus-cubed-ex1
: 54

example 2

Add named example with value, e.g. x-ex2

#+NAME: x-ex2
: 3 

Pass named value to noweb

#+NAME: cubed-plus-cubed-ex2
#+BEGIN_SRC python :noweb eval
  return <<cubed(x=x-ex2())>> + <<cubed(x=x-ex2())>>
#+END_SRC

#+RESULTS: cubed-plus-cubed-ex2
: 54

Hope that helped!


This code was tested using
emacs version: GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
org-mode version: Org mode version 9.1.2

Melioratus
  • 4,504
  • 1
  • 25
  • 43
  • So, I understand then. :var y is converted to the python's y inside the code block and so there would be a need to convert it back to an s-expression and that's not supported... In principle though, it does not need to be converted at all, the :var y could be passed unchanged in `<>`. But your examples do help! Thank you! – user3496846 Jan 10 '18 at 20:40
  • @user3496846 - I think the reason `<>` didn't work is because `noweb` is evaluated and rendered before `y` was defined inside python which is why needed to move `y` declaration outside of python code block. Unfortunately, I can't find the order of operations in the org manual from which I learned this tidbit. Another way to workaround this issue to explicitly render the `noweb` recursively in multiple steps but it can be painfully convoluted. Another less painful way is to add `#+NAME:` above `#+RESULTS:`. I'll see if I can get a new example of rename solution and update my answer. – Melioratus Jan 10 '18 at 21:13