Source code highlighting in LaTeX

Taking Norman’s advice to heart, I’ve hacked together a solution that used (a patched) Pygments for highlighting and pushed in as many features as possible without bursting 😉

I’ve also created a LateX package, once my Pygments patch was released in version 1.2 …

Presenting minted

minted is a package that uses Pygments to provide top-notch syntax highlighting in LaTeX. For example, it allows the following output.

fancy LaTeX example

Here’s a minimal file to reproduce the above code (notice that including Unicode characters might require XeTeX)!

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{minted}

\setsansfont{Calibri}
\setmonofont{Consolas}

\begin{document}
\renewcommand{\theFancyVerbLine}{
  \sffamily\textcolor[rgb]{0.5,0.5,0.5}{\scriptsize\arabic{FancyVerbLine}}}

\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{csharp}
  string title = "This is a Unicode π in the sky"
  /*
  Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
  of an $n$-sided regular polygon circumscribing a
  circle of diameter $d$.
  */
  const double pi = 3.1415926535
\end{minted}
\end{document}

This can be typeset using the following command:

xelatex -shell-escape test.tex

(But minted also works with latex and pdflatex …)

minted.sty works similar to texments.sty but allows additional features.

How to get it

  • minted is listed on CTAN (package info)

  • documentation is of course included.

  • minted is now maintained by Geoffrey Poore. The development version, including the latest .sty file, is available at github.com/gpoore/minted, and can be cloned from there.

Once again, thanks to Norman for motivating me to produce this package.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)