2008-09-22

Python inside LaTeX (and Sage too)

I discovered recently, that it is possible to run Python scripts from LaTeX documents and use the to generate document's content. This can be used to read/convert data, generate tables and figures, do on-the-fly calculations.

How to run Python code from LaTeX:

  • download python.sty. Put it in the same directory as the LaTeX document
  • \usepackage{python}
  • Put Python code inside \begin{python}\end{python} environment. Whatever this code prints, will become part of the LaTeX document.
  • Run LaTeX with -shell-escape option (this permits running external code from LaTeX)
This is an example of the LaTeX document and the PDF produced.

And some more advanced examples are in this LaTeX file (see also the PDF). They cover symbolic calculations from inside LaTeX, plotting and variable persistence between python environments.

P.S. What's more, one can even embed the full-featured symbolic math package Sage into LaTeX. For this purpose use sagetex package. By the way, you do not need to install Sage to start using it, please check out its web-version. It is really powerful.

Links:

This post in Russian: Python внутри LaTeX (и математический пакет Sage тоже)

Visualizing altitude and velocity profiles of GPS tracks

I think that altitude and velocity profiles of GPS tracks is one of the most intereseting forms of their representation. One can use gpsvisualizer.com to plot such profiles. However, I prefer having free tools for such a simple thing.

Here I offer my own python script gpxplot, which extracts profile data from a GPX file and plots a profile. This is a direct link: gpxplot.py.

There are two important features of the script:

1) GPX file may consist of two or more separate tracks. Each track may consist of several disconnected segments. The script preserves this segmentation of the track.

2) GPX files do not contain explicit information about distane travelled. The script calculcates it using haversine formula (as if the Earth were spherical).

The script can either output profile data in a convinient tabular form, or generate a gnuplot script and call gnuplot to do actual plotting.

Usage examples are given on a Google Code page. This is what a result may look like:
example of a time-altitude profile plotted to SVG file with gnuplot

Update: Now there is also online version of the script. Just upload a track and embed the plot in whatever page you want.

Links:

This announcement in Russian: Визуализация профилей высоты и скорости GPS-треков