How to import/expand noweb refs in evaluated (exported) source blocks in org-babel?
Asked Answered
D

2

5

I'm trying to do something like this:

* Define some functions
#+begin_src python :noweb_ref defs
   def f1(a,b,c):
     return True
   def f2(d,e,f):
     return False
#+end_src

* Use them in a results-exported block later
#+begin_src python :results output :exports both :tangle yes
<<defs>>
print "test results:"
print f1(1,2,3)
#end_src

What I want to happen is for <<defs>> to be expanded tangle-style when the block is evaluated to produce the export output. What actually happens is that <<defs>> gets evaluated literally and causes a syntax error.

When tangling blocks like this to an output file, everything works perfectly, but I can't figure out how to do the same thing when I'm exporting the buffer.

Suggestions?

Dispersal answered 18/3, 2013 at 18:22 Comment(0)
P
7

I'm not sure to really understand your point... but

1) you miss a noweb:yes header argument

2) you can use <<func()>> to insert the results of evaluating func (instead of the code of func) -- that's here that I'm not sure about what you really want.

Petrinapetrine answered 18/3, 2013 at 19:55 Comment(3)
The :noweb yes header was exactly my problem. I'd tried :noweb tangle and :tangle yes. Now that you point it out, the documentation at orgmode.org/manual/noweb.html#noweb is perfectly clear.Dispersal
Excellent! BTW, on the 2nd point, dunno why this is badly written, but you should read <<func()>>, that is your function followed by open and closing parens, all between doubled angular bracketsPetrinapetrine
S.O. tried to turn into HTML, I believe. I edited your answer so it would show up, but the edit is waiting in some sort of review queue, I think,Dispersal
A
3

You can also use :noweb no-export. That shows the noweb-syntax in exported files but expands the code blocks when evaluating or tangling the files.

:noweb strip-export is great if you just want to show an algorithm:

<<prep>>
result = A + B
<<plot>>

The exported file then shows this:

result = A + B
Anatol answered 28/5, 2013 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.