Typeset Python Programs with LaTeX

Jeroen van Maanen (Jeroen.van.Maanen@cwi.nl)
Fri, 12 Aug 1994 16:03:45 +0200

In the past few weeks I put together a program that translates Python
programs to something that can be typeset with LaTeX. It is available
by FTP from ftp.cwi.nl as pub/python/py2tex.tar.gz

Here is the accompanying BLURB:

The py2tex package allows you to typeset Python programs with LaTeX.
It consists of some Python code to translate Python source to LaTeX
and a LaTeX style file that contains the necessary definitions. The
style file also adds some degree of customizability.

Its primary function is to typeset Python code properly.
* Keywords bold.
* Functions slanted.
* Symbols instead of ASCII kludges for
- assignment (=),
- relations (==, <=, >=, !=, <>, in, not in, is, not is),
- logical operators (not, and, or).
[This is optional because some people (e.g., Guido) find the ASCII
versions more recognizable.]
* Strings in typewriter type. ASCII control characters in strings
are handled properly, i.e., they are typeset as escape sequences
('TAB' is typeset as '\t' and 'ESC' is typeset as '\033'). Long
strings (delimited by triple quotes) are handled properly too
except in comments.
* Numeric literals are typeset sensibly.
- floating point literals in scientific notation
(like $1\cdot 10^6$).
- octal literals in italic digits.
- hexadecimal literals in typewriter type, lower case 'x', upper
case digits.
* Proper indentation (also for continued lines).
* Proper line breaking (following TeX rules for formulas).
* Line numbers.

It also gives you the full power of TeX in the comments to your code.
* Consecutive lines that start with a hash mark (#) right after the
indentation are joined in a single `block comment' and typeset as
a whole rather than line by line.
* Within comments the hash mark switches between TeX and Python mode
like the dollar sign switches between TeX and math mode. So you
can incorporate samples of Python code in the comments to the code.

The Python source for this package is of course documented using the
package itself. The accompanying LaTeX style file is documented
using the doc option by Frank Mittelbach.

Jeroen van Maanen <jeroenm@cwi.nl>