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


#commented code. Remove comments if you have sympy 
#from sympy import *
#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}'

 \int \sqrt{ 1+x } = \pycon{result}

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 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.

1 Answer 1


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.

  • Thanks. How would the MWE I posted be changed then so it works with either of these in order to get the final desired output?
    – Nasser
    May 30, 2020 at 4:15
  • @Nasser You could replace pyconsole with pycode (or pyblock if you want visible code), and replace \pycon with \py. There isn't any way to get console-style code execution with LaTeX output, though.
    – G. Poore
    May 30, 2020 at 4:31
  • Thank you. That replacement worked. Could you please explain a little more when to use pyconsole vs. pycode? I googled this but hard to get short summary of the differences. I'll switch to using pycode from now on in this case, since I mainly use python to get Latex back.
    – Nasser
    May 30, 2020 at 5:03
  • 1
    @Nasser pyconsole is for when you want to run code exactly like the command line, with >>> and extra output. If you just want to run code, the other options are generally better.
    – G. Poore
    May 30, 2020 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .