HOME
PDF (letter size)
PDF (legal size)

A very simple introduction to using Python in Latex

Nasser M. Abbasi

June 19, 2020   Compiled on June 19, 2020 at 10:07pm

Contents

1 Example using pyconsole
2 Example using pycode
3 Calling python passing it argument to process and getting the Latex back
4 Calling python from latex to solve differential equations and show its solution

This note describes all the steps to use Python inside Latex. The directions are based on using Linux, since this is the system I tried this on.

Using texlive 2020, this package (pythontex) is already there. There is not additional installation needed. Once you install texlive itself, then you have this package.

1 Example using pyconsole

Here is the most simple tex file that uses python


Now comes the tricky part. To compile it, we have to do the following commands

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

Which prints on the screen this

>/usr/local/texlive/2020/texmf-dist/scripts/pythontex/pythontex.py  python_file.tex
This is PythonTeX 0.17

--------------------------------------------------
PythonTeX:  python_file - 0 error(s), 0 warning(s)

Now need to compile the latex file on more time

pdflatex foo.tex

We have to run the above 3 commands each time. On my installation of texlive 2014, the script pythontex.py was not already in the path, so instead of keep typing the above long command each time, I added an alias to my bashrc

vi $HOME/.bashrc #add this line below to the file
alias PYX=/usr/local/texlive/2020/texmf-dist/scripts/pythontex/pythontex.py

#after exiting vi, typed
source $HOME/.bashrc

So now I can write (less typing) the following

pdflatex foo.tex
PYX  foo.tex
pdflatex foo.tex

Or you can add it to the path. You must have python itself installed. Version 2.7 or better, else none of the above will work.

In the above example, we did not do anything interesting. Let us now make a python variable, raise it to the power 2, and show the result in Latex


Which produces the following PDF file when compiled

PDF

Here is screen shot of the above

pict
Figure 1:PDF file showing Python computation result inside it

2 Example using pycode

In this example we will call Python to do symbolic computation, which is integrating a function, then obtain the latex back of the result. For this sympy is used inside Python.

Create Latex file like this


Now compile using the same commands as first example above

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

Which produces the following PDF file when compiled

PDF

Here is screen shot of the above

pict
Figure 2:PDF file showing Python computation result inside it

3 Calling python passing it argument to process and getting the Latex back

In this example, we make a small function inside pycode which takes one argument which is string that represents the integrand to integrate and second argument which is the integration variable.

Create Latex file like this


Compiling again using the same commands as the above examples

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

Produces the following PDF file

PDF

Here is screen shot of the above

pict
Figure 3:PDF file showing Python computation result inside it

4 Calling python from latex to solve differential equations and show its solution

In this example, we make a small function inside pycode which takes and ode to solve, using different initial conditions.

Create Latex file like this


Compiling again using the same commands as the above examples produces the following PDF file

PDF

Here is screen shot of the above

pict
Figure 4:PDF file showing Python computation result inside it

References:

  1. Pythontex main documenation http://www.ctan.org/pkg/pythontex
  2. pythontex quickstart PDF
  3. Pythontex GitHub
  4. A Gentle Introduction to PythonTeX