Highlight source code in LaTeX with the lstlisting package.

The listings package offers source code highlighting for various languages. Learn by example how to use it in your LaTeX documents.


The listings package is a powerful way to get nice source code highlighting in LaTeX. It’s fairly easy to use and there’s good documentation available on how to use it. For more code highlight styles, read this post: Create Beautiful Code Listings with Minted.

Example

The output of the listings package will pretty much look like this after some setup:

Image

\documentclass{article}

\usepackage{listings}
\usepackage{color}

\renewcommand\lstlistingname{Quelltext} % Change language of section name

\lstset{ % General setup for the package
	language=Perl,
	basicstyle=\small\sffamily,
	numbers=left,
 	numberstyle=\tiny,
	frame=tb,
	tabsize=4,
	columns=fixed,
	showstringspaces=false,
	showtabs=false,
	keepspaces,
	commentstyle=\color{red},
	keywordstyle=\color{blue}
}

\begin{document}
\begin{lstlisting}
#!/usr/bin/perl
print S(@ARGV);sub S{$r=(@_[0]%4==0&&@_[0]%100!=0)||@_[0]%400=0;}
\end{lstlisting}
\end{document}

I first use the include the color and listings package and then set up the language of the package headings to german using \renewcommand\lstlistingname{Quelltext}. This is not necessary if you’re planning to use it in English.

Afterwards I set up the general layout for the package with the \lstset command. The options I set there should be self-explanatory. Note that it’s required to manually set the colors for keywords and comments, otherwise the output would be only black on white. The desired output must then be embedded within a listings environment.

Assuming we have a Perl script saved in a file script.pl, we could also simply use the following syntax to get the same result:

\lstinputlisting{script.pl}

This will keep your LaTeX source clean and you can still use all features of the package.

Summary

  • After some initial setup, all source code can be embedded in a lstlistings environment
  • A list of all languages and more documentation is available in the manual of the listings package
  • Use the \lstlinputlisting{FILENAME} command to read the content of source files directly into a lstlistings environment.

Next lesson: 14 Circuitikz