One of the attraction of LaTeX is that it is extensible. Using Martin Ehmsen’s python.sty, it is possible to embed python into LaTeX code to prepare dynamic, living documents.
Embedding python in LaTeX is made possible by use of python.sty written by Martin Ehmsen. Simply place this in the same directory as your LaTeX file, and add \usepackage{python} in the preamble of your manuscript (pylatex.tex in this case):


Typeset the document now to make sure it is compiling. You will need to have python running on your machine (doh!), and make sure that LaTeX is typesetting with the -shell-escape option. This is (I think?) the default option in TeXShop, and can be set in Preferences (A, under engine tab B) if it isn’t:

I am writing this on an OSX Snow Leopard machine, running TeXShop (2.33) and the Enthought Distribution (5.1.1) of Python (2.5), with python.sty at version 0.21. The Enthought distribution includes matplotlib and EPDLab, the environment where python code screenshots are taken.
First Steps with LaTeX Embedded Python
Let’s try our first step with embedding python in LaTeX with some simple “hello world” code:


This generate the following output:
A shows the elemental case where python fed back into LaTeX through stdout.
B shows that LaTeX syntax from stdout is interpreted. To avoid complications with escape characters, we will be using raw strings (the r preceding the “”)
C shows our first example of “programmatically” controlling LaTeX from python. So cool 🙂
D shows the same thing as C… with an extra space inserted before print. This fails because python is sensitive to white-space. The error is sent to a log: filename.py.err. Test your python code separately, and preferably wrap them into a module. For example, if we want to write a piece of code that print the Fibonacci numbers, we could do:

In doing so, you’ll likely be chasing after improper indents for a little while. Whereas by wrapping it into a module and debugged first, this becomes less painful:



From left to right we have the directory structure, the python code, as well as the LaTeX document. For editing in external python editor, it is helpful to set the tab to spaces. LaTeX had given me a few indentation errors when it was set to tab.
Second Steps and Beyond
Now that you can link python to LaTeX, the sky’s the limit! Kjell Fauske has shown examples of using PyX to generate plots that is placed in the LaTeX output, and kib has examples of preparing customized assignments for a math class.
Addendum: As I’m running OSX, I have two installations of python floating around (OSX and Enthought). All my paths point to the Enthought distribution, but for some reason, python.sty seems to call the OSX python. I know this because import os and import random (i.e., generic functions) work, but import pylab or import matplotlib fails with an import error (no such module). This is strange – Enthought python is on every $PATH. If you have any ideas of how to solve this, I would love to know. At the moment I’m stalled out…
Addendum 2 (24 Mar 10): Solved it! (sorta) Even though I had all the right paths for the interactive shell in .bash_profile (so python at Terminal will give Enthought python), the non-interactive shell doesn’t read that on start up. Actually, I still have no idea what shell opens up or what it reads… I tried adding the paths from .bash_profile to etc/bashrc, ~/.profile, ~/.bashrc, and even etc/csh.cshrc, and I still couldn’t get it to work. Absent the best solution, I settled for a good one. If you open up python.sty (you can do that in your TeX-editor), there should be the following line:
\immediate\write18{cat \@pythoninclude\space\jobname.py | python > \jobname.py.out 2> \jobname.py.err}
All you need to do for the good (but not best) solution is to substitute the path of your MacPython/Enthought Python/whatever distribution into where python is . In my case, it looks like:
\immediate\write18{cat \@pythoninclude\space\jobname.py | /Library/Frameworks/Python.framework/Versions/5.1.1/bin/python > \jobname.py.out 2> \jobname.py.err}
It’s really awesome to have access to the full matplotlib in LaTeX! (That said, if you know which configuration file to get the paths in properly, let me know.)
Addendum 3: Feb 2011 – this is silly. If all you need is symbolic math operations, sageTeX is a much cleaner way of doing this. This post is only useful if you want to leverage your full python installation.
Leave a Reply