# Python in Latex with Sympy

by on January 30, 2013

The final output for scientific research is typically a paper published in a scientific journal. There may be electronic versions of figures and tables that accompany the paper. However, the links between the input data (e.g. images, spectra, time series), the analysis (e.g. code, databases), and the output paper (LaTeX, EPS figures) is often weak. Everyone has their own work-flow and record-keeping philosophies. In my Latex, I heavily comment with references to my electronic research notebooks and specific python calls used to generate the figures and tables. I still can’t help thinking there must be a better way.

Two intriguing options are

• python.sty — a LaTeX style file that provides a \begin{python} environment (examples). There is an interesting variant called pysnip.
• SympyTex — embed python code to generate latex equations, including calculated results

I haven’t tried these yet, as I can’t quite decide on the exact work flow for incorporating them. There may be some moderate gain with Sympy to writing equations in python format rather than LaTeX (assuming I like Sympy’s formatting). But I am not sure if I want the numbers, figures, and tables to be re-made every time I compile my LaTeX files.

If you use these packages in your papers, let us know when and how.

1 Ben Maughan January 30, 2013 at 8:20 am

It’s not exactly the same thing, but you can achieve much of what you are looking for by using org-mode (http://orgmode.org/) in emacs, specifically using babel (http://orgmode.org/worg/org-contrib/babel/) to embed live code and the resulting output in your org document. Org-mode has powerful export options and in principal you could have a single text file containing your research notes, code, figures and paper, and then just export the subset containing the manuscript to latex and/or pdf to produce the final paper.

2 Erik Tollerud January 30, 2013 at 5:57 pm

An approach I currently use is to keep .tex file and a .py script in the same git repository. The python script includes everything necessary to generate all the plots (and can do so from the command line.) This enforces a moderately strong link between the analysis and the output (although not the data, unless it’s small enough to fit comfortably in a git repo).

In principal, combining that with the pysnip you mentioned here would complete close the loop and make the .tex file generation always include generating the figures (and tables, when necessary). And it’s all also in version control, because it’s in a git repository.

3 Jeremy Sanders January 31, 2013 at 4:03 am

The problem with git and LaTeX is that git works on a line-by-line basis for its patches. If you tend to use emacs fill to reformat your paragraph, this means you can get an enormous diff by just adding or removing a word. You can use one line for each paragraph, but when you replace the word then the whole paragraph needs to be changed. This also makes latex error reporting less helpful. Does anyone know of version control which can do a diff on a word-by-word basis?

4 Erik Tollerud January 31, 2013 at 10:29 am

Good news Jeremy! You can do that already in git: “git diff –word-diff” or more helpfully if you are working at the terminal, “git diff –color-words”

That said, I typically confine my lines to ~80-100 characters when writing .tex (as long as you don’t have an empty line it won’t make a new paragraph), so this doesn’t bother me that much. But not everyone does that.

5 Matt Davis January 31, 2013 at 9:06 am

dexy is another option, though I haven’t actually used it: http://www.dexy.it/

6 G. Poore February 27, 2013 at 9:42 pm

I’m the author of the PythonTeX package for LaTeX (https://github.com/gpoore/pythontex). It was inspired by python.sty and SympyTeX, among others, but I’ve tried to add a lot of additional functionality. PythonTeX will only execute the Python code in your LaTeX document if the code has been modified, so you don’t unnecessarily recreate all the Python-generated content. The Python code can be split into user-defined sessions, which run in parallel, increasing performance. All errors and warnings generated by the Python code are synchronized with the LaTeX document’s line numbers. PythonTeX can track data dependencies as well, so that if your data changes, the Python code that depends on it will be executed the next time you compile your document. And if you want to submit your document to a journal, there’s an option to create a copy of your document in which all Python code is replaced by its output, leaving a pure LaTeX document.

7 Peter Williams March 14, 2013 at 6:25 pm

I use the “make” tool to link the two together — if I want to include the results of some calculations, I write a little Python program that outputs a short LaTeX file that I can then \include{} in the main one. This is great for tables in particular. The standard dependency-tracking of make provides an easily-understood and flexible way for not recalculating things unnecessarily.

I tend not to bother with this with results that are a single number, but if you wanted you could write something that outputs a file like

\newcommand{\signif}{3.2}

and then write “We detect the source at \signif$\sigma$ …”

8 T. Emil Rivera-Thorsen April 13, 2013 at 2:39 pm

I must say that PythonTeX looks very intrigueing. I am definietly going to look into that.