Saturday, September 13

Python syntax highlighting in LaTeX

LaTeX provides a listings package to typeset code. The standard output is great for black-and-white printouts, but not for beamer presentations, where a more dynamic color scheme would be suitable. Having recently worked on slides with Python code, I have prepared a file that you can include to get nice colourful code that looks good on a presentation:

My code is based on a snippet found on Ubuntu forums. I added some more keywords and tweaked the color scheme to correspond to the one that IDLE uses (that color scheme is not ideal, but my audience uses IDLE for coding and I wanted some consistency).

To use the package, copy the file to the same folder as your document and include it in the header:

\include{pythonlisting}

Then, to render code, use python sections:

\begin{python}
def foo():
    print "bar"
\end{python}

Note that if you want to use this in a beamer frame, you will have to mark the frame that contains Python code as fragile, like this:

\begin{frame}[fragile]{Some Python code}

\begin{python}
def foo():
    print "bar"
\end{python}

\end{frame}

Better Russian-English dictionaries

A reader called Kuno notes that there are now some good Russian-English dict-format dictionaries available online. They are very likely better than the ones I extracted a couple years ago. The dictionary files suggested by Kuno can be downloaded directly, fetched with rsync (rsync rsync://dictd.xdsl.by/dicts/) or the server can be queried with a dict client (try "dict -h dictd.xdsl.by -D" for a list of available dictionaries).