How to render latex produced by embedded python code.

If have the code :

```{python}

from sympy import *
from IPython.display import display
init_printing(use_latex='mathjax')
x=symbols('x')
d=diff(sin(x)/cos(x), x)
print(latex(d))
#```

This prints correctly:

\frac{\sin^{2}{\left(x \right)}}{\cos^{2}{\left(x \right)}} + 1

But offcourse I need this latex rendered , not the text, in my document.
I tried init_printing(use_latex='mathjax') , but no ...

From this page:

If your python code is generating raw HTML or LaTeX then the results='asis' option will ensure that it’s passed straight into the document’s output stream

So you don't need init_printing, just a few modifications to your code:

```{python, results='asis'}
from sympy import *
x=symbols('x')
d=diff(sin(x)/cos(x), x)
print('$' + latex(d) + '$') # need to add '$' so it is recognized as MathJax
```
2 Likes

Thanks, this did the trick.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.