Skip to main content

Format Python code in TeX

Project description

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, is not),
- 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.
* 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.

It also gives you the full power of TeX in the comments to your code
if you want. This not only allows TeX constructs, like formulae, in
comments, it also allows for formatted scraps of Python code in
comments. In (La)TeX mode the hash mark switches between TeX and
Python mode within comments, like the dollar sign switches between
TeX and math mode.

If you don't need these features, or if you want to typeset programs
that were not specifically written to be typeset with py2tex, you
can leave comments uninterpreted either by running the py2tex script
with the -v flag, or by inserting magic comments in the source file.

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.

Project details


Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page