[Tex/LaTex] How to return latex as string from Python back to Latex document using pythontex package

pythonpythontexstrings

I know how to use basic pythontex with single variables.

But now I am trying to do something more than basic. I'll explain what the problem is, then given MWE below. To run the MWE requires having python installed on your PC.

I need to call python to do some symbolic computation, say integrate a function, using sympy, then return the result in Latex back to Latex document. For this I use the command latex() inside Python which generate the string representation of the latex result. It is this string I want to typeset inside the latex document.

This string comes back to the latex document OK, except it does not render as actual latex when compiled to PDF.

It remains as raw string but in Latex format with all the backslashes there.

So something is wrong somewhere in how I am doing this transfer and this is what I need help on. May be I need to process this result in Latex document more somehow?

To make the MWE very simple, I did not use sympy in this example, since this might require extra installation on your end (it does not always comes with basic Python).

So instead I just put the actual Latex string result to be returned directly in there as if sympy has exectued.

Here is the MWE

\documentclass[11pt]{article}%
\usepackage{pythontex}
\begin{document}
\begin{pyconsole}

#commented code. Remove comments if you have sympy 
#from sympy import *
#x=symbols('x')
#result = latex(integrate("(1+x)**(1/2)",x))
#This below is what result above will be. THis below is what I want
#send back to latex file, but renders as normal latex

result = '\\frac{2 \\left(x + 1\\right)^{\\frac{3}{2}}}{3}'
\end{pyconsole}

\[ 
 \int \sqrt{ 1+x } = \pycon{result}
\]
\end{document}

Compiled and run as follows

 pdflatex python_file_2.tex
 /usr/local/texlive/2020/texmf-dist/scripts/pythontex/pythontex.py  python_file_2.tex
 pdflatex python_file_2.tex

The resulting PDF file looks like this

enter image description here

Then instead of \pycon{result} tried \pycon{print(result)} and recompiled and this is the PDF file now

enter image description here

Still not working. What I want ofcourse is to get the result as if I typed

  \int \sqrt{ 1+x } = \frac{2 \left(x + 1\right)^{\frac{3}{2}}}{3}

Which gives

enter image description here

The problem is in how to return back raw latex string from python back to pdflatex such that it is used as plain latex and not as string. I looked at pythontex.pdf but I am lost what to try as there are no examples.

If you are interested in seeing how the above Python code runs inside Python itself, here it is, on my linux box

>python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> x=symbols('x')
>>> result=latex(integrate("(1+x)**(1/2)",x))

>>> result
'\\frac{2 \\left(x + 1\\right)^{\\frac{3}{2}}}{3}'

Using TL 2020 on Linux.

Best Answer

pyconsole and \pycon do not support returning LaTeX; everything returned always appears verbatim. To get LaTeX output, you have to use pycode and \py or similar environments and commands instead.

Related Question