LaTeX minted

The minted package is a possible alternative to listing. It provides syntax highlighting for sourc code in LaTeX documents by calling the Python pygmentize tool.

In comparison to listing it has a better syntax support; the underlying pygments library includes many lexers and can distinguish more syntax elements than mere keywords.

Usage is just as simple; example:

 * Gets single server parameter for specified key. ...
public function getServerParameter($key, $default = '')
    return (isset($this->server[$key]))
        ? $this->server[$key] : $default;

A disadvantage is a limited functionality, it has notably fewer options to control the output’s appearance (for example one cannot as easily emphasize additional words). It also adds an additional external dependency to the TeX compiler setup and the program that is your document. (At first I was afraid I could not keep my Unix desktop and Windows netbook in sync, but the installation was straightforward.)

If the external dependency and long term archiving is an issue then it is also possible to create a LaTeX-only version of the document: Replace the minted-environments with the output of pygmentize -f latex into the TeX document. The resulting code is is no longer readable/editable (at least not easily), but the resulting document will no longer require Python to compile.

The pygmentize step yields this LaTeX code:

[mschuett@dagny] /tmp% pygmentize -O startinline -f latex example.php
\PY{l+s+sd}{ * Gets single server parameter for specified key. ...}
\PY{l+s+sd}{ */}
\PY{k}{public} \PY{k}{function} \PY{n+nf}{getServerParameter}\PY{p}{(}\PY{n+nv}{\PYZdl{}key}\PY{p}{,} \PY{n+nv}{\PYZdl{}default} \PY{o}{=} \PY{l+s+s1}{''}\PY{p}{)}
    \PY{k}{return} \PY{p}{(}\PY{n+nb}{isset}\PY{p}{(}\PY{n+nv}{\PYZdl{}this}\PY{o}{->}\PY{n+na}{server}\PY{p}{[}\PY{n+nv}{\PYZdl{}key}\PY{p}{]))}
        \PY{o}{?} \PY{n+nv}{\PYZdl{}this}\PY{o}{->}\PY{n+na}{server}\PY{p}{[}\PY{n+nv}{\PYZdl{}key}\PY{p}{]} \PY{o}{:} \PY{n+nv}{\PYZdl{}default}\PY{p}{;}

One other hint: some tweaking is required to get correct syntax highlighting in the Kate/Kile editor.

Finally as an advanced example here is a pygmentize lexer. I chose this one for Apache config files because it is neither trivial nor too complicated; it uses two states and shows how regular expressions are used to classify matches):

class ApacheConfLexer(RegexLexer):
    Lexer for configuration files following the Apache config file format.
    name = 'ApacheConf'
    aliases = ['apacheconf', 'aconf', 'apache']
    filenames = ['.htaccess', 'apache.conf', 'apache2.conf']
    mimetypes = ['text/x-apacheconf']
    flags = re.MULTILINE | re.IGNORECASE
    tokens = {
        'root': [
            (r'\s+', Text),
            (r'(#.*?)$', Comment),
             bygroups(Name.Tag, Text, String, Name.Tag)),
             bygroups(Name.Builtin, Text), 'value'),
            (r'\.+', Text),
        'value': [
            (r'$', Text, '#pop'),
            (r'[^\S\n]+', Text),
            (r'\d+\.\d+\.\d+\.\d+(?:/\d+)?', Number),
            (r'\d+', Number),
            (r'/([a-zA-Z0-9][a-zA-Z0-9_./-]+)', String.Other),
             r'user|group)\b', Keyword),
            (r'"([^"\\]*(?:\\.[^"\\]*)*)"', String.Double),
            (r'[^\s"]+', Text)

Comments are closed.