Wednesday, 26 January 2011

Inserting computer code in LaTeX documents

After my post on lgrind, I searched a bit more for a good syntax highlighting utility I could use for inserting computer code into webpages and LaTeX documents. Good use of time, as I discovered that lgrind is a bit disappointing - it really pales in comparison with pygments and minted.

Pygments is a simple python-based command-line utility that transforms raw computer code into syntax-highlighted code in html, LaTeX and other formats. Much better as I said than lgrind - the output in fact is nearly perfect.

To install pygments on Mac OS 10.6, simply type

sudo easy_install pygments

After that, 'pygmentize' your code, say a C++ code into LaTeX, by typing

pygmentize -o mycode.tex mycode.cpp

With this, you only get the bare highlighted code; to get the full, compilable LaTeX document containing the highlighted code, type

pygmentize -O full -o mycode.tex mycode.cpp

More information, including the list of recognized computer languages, can be found on the pygments website.

With only pygments installed, you still have the job of inserting syntax-highlighted code into your LaTeX notes or presentations. Wouldn't it be simpler if LaTeX did the highlighting itself?

Install minted, a simple LaTeX package, and the job is done. Download the minted directory from ctan and run the makefile by typing

make

inside the minted directory. Then move the style (.sty) file and the documentation (.pdf) somewhere inside your LaTeX tree.

To use minted, use the following template:

\documentclass{article}
\usepackage{minted}
...
\begin{document}
...
\begin{minted}{matlab}
Your code here
end{minted}
...
\end{document}

Instead of Matlab, as above, use any of the programming languages recognised by pygments.

Note that minted is not a stand-alone package: it relies on pygments to generate the syntax-highlighted code and includes it when pygments' job is done. Not a perfect solution (the compilation can be a bit slow), but still quite a very good one.

Tuesday, 18 January 2011

lgrind on Mac

lgrind is a pretty-printer and syntax highlighting utility for computer code. You take some computer code, feed it to lgrind, and you get a publishable code (in html, latex, etc.) with formatted and colored syntax. Very useful for including code in presentations and lecture notes.

I tried to install lgrind from ctan on my Mac but couldn't compile the source code without errors. Fortunately, a bit of Googling led me to a set of Mac binaries courtesy of the JHL Development Blog.

The installation is simple:

1. Download and decompress the JHL's binaries
2. Put the executable file lgrind in your bin directory
3. Put the lgrind.sty file in your latex tree (available from ctan)
4. Download the lgrinddef file from ctan and put it where lgrind.sty is
5. Create a symbolic link in /usr/share/ pointing to your latex tree with

ln -s ~/Library/texmf texmf

Saturday, 15 January 2011

Network drives on Octave

Developing a new course always brings some surprises. Mine last week was to discover (during the very first tutorial session!) that the execution path of Octave doesn't seem to get updated automatically for networked and mounted drives - at least not on Windows machines. This means that you create a new m-file, then try to run it, but receive an error message saying that the m-file doesn't exist. Type 'ls' and you see the m-file is there. Type

exist('filename.m','file')

and Octave tells you the file is there. But try to run it and it's not seen. Quite an annoying bug.

Solution #1: Quit and restart Octave. Not much of a solution - my students weren't impressed.

Solution #2: Remove current directory from path, then add it again. Not more impressive.

Solution #3: Type

path(path);

every time a new m-file is created to refresh the path. This is the only (semi) sensible solution I've found so far.

Is this a Windows bug or an Octave one? I'm inclined to blame Windows of course but who knows? I couldn't find much information about this bug. Seems like we may have to buy Matlab licenses after all...