2

I have a piece of python code that outputs latex code. I'm trying to call the python source block from within a latex source block so it will format the output as latex when I export.

Any idea how to do this? I was hoping something similar to the below would work.

#+name: gradient_potential
#+BEGIN_SRC python :exports none :results output
from sympy import symbols, sqrt, diff, latex

x, y = symbols('x y')

U = x * y

print(latex(diff(U, y)))
#+END_SRC


#+begin_export latex
\begin{align}
#+CALL: gradient_potential() :results output
\end{align}
#+end_export

2

Use a Noweb reference <<src block call>> to inject the results of source blocks into other source blocks.

Example:

#+NAME: ltxInput
#+BEGIN_SRC emacs-lisp :exports none :results raw drawer
"\\sqrt{x^2+y^2}"
#+END_SRC

#+RESULTS: ltxInput
:results:
\sqrt{x^2+y^2}
:end:


#+NAME: ltxSrc
#+BEGIN_SRC latex :noweb yes :exports results
\begin{align*}
<<ltxInput()>>
\end{align*}
#+END_SRC

#+RESULTS: ltxSrc
#+begin_export latex
\begin{align*}
\sqrt{x^2+y^2}
\end{align*}
#+end_export

Tested with Emacs 26.3 and Org 9.2.6.

Note that your approach with the :var header argument of a latex source block did not work for me. I got an error "Wrong use of \ in replacement string." for the slash in \sqrt{x^2+y^2}.

| improve this answer | |
1

I found a way to do this, I'll post for anyone who has the same issue in the future. The method I found is using both a python source block and a latex source block.

#+name: gradient_potential_py
#+BEGIN_SRC python :exports none :results output :cache yes
from sympy import symbols, diff, latex

x, y = symbols('x y')

U = x * y

print(latex(diff(U, y)), end='')
#+END_SRC

#+name: gradient_potential
#+begin_src latex :var expr1=gradient_potential_py :exports none
\begin{align}
expr1
\end{align}
#+end_src

#+CALL: gradient_potential()

I found that I need the end='' in the print statement when exporting to latex.

| improve this answer | |

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.