highlight LaTeX code with lstlistings


Günter Milde



$Revision: 5534 $


$Date: 2005-06-28$


© 2007, 2009 G. Milde, Released without warranties or conditions of any kind under the terms of the Apache License, Version 2.0


For syntax highlight of literal blocks with the listings LaTeX package, call with the options --stylesheet=listings-tex-options.sty --literal-block-env=lstlisting.

Adapt the settings to your needs by either

  • modifying a copy of this file under a different name, or

  • copy parts of this file into your custom style sheet.

See listings.pdf for configuration and usage of the listings package.



[2009/03/02 v0.1 highlight LaTeX with lstlistings]

Of course, we need to load the listings package itself:


In a document with lots of lists and listings, separating paragraphs by vertical seems appropriate:


load hyperref with blue citation colour:


In the References/Bibliography section, avoid holes in the title due to block alignment:


Omit the auto-inserted “References” heading, to be compatible with HTML output also if use latex references is set:


Font settings

Times, Helvetica, Courier:

%\RequirePackage[scaled=.90]{helvet}  % scaled to fit Times

Palatino, Helvetica, Courier:

\RequirePackage[sc]{mathpazo}         % with small caps
% \RequirePackage[sc,osf]{mathpazo}   % with small caps and old style figures
\RequirePackage[scaled=.95]{helvet}   % scaled to fit Palatino

(The TX typewriter font comes with bold, slanted, and small-caps variants and without the problems of txfonts’ math fonts). However, txfonts is a contributed package, not necessarily available:


Default language

Highlight literal blocks as TeX:


pre-load the language:

\lstloadlanguages{[LaTeX]TeX} %  comma separated list of languages


Typeface settings:

  basicstyle=\ttfamily,           % print whole listing in tt
  % basicstyle=\ttfamily\small,
  keywordstyle={},                % do not highlight keywords
  % identifierstyle=\slshape,     % macros that are not keywords, args
  commentstyle=\rmfamily\itshape, % italic comments
  % stringstyle=\ttfamily,        % typewriter type for strings
  % texcsstyle=\slshape,

Visible whitespace:

\lstset{showstringspaces=false}  % no visible spaces in strings
%        showtabs=true,
%        tab=\rightarrowfill}

Line numbers:

%\lstset{numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt}

Frames around listings:


Extended characters in listings:


Column alignment

(for typesetting with variable width fonts)

columns=[c|l|r] <alignment>

<alignment> in [fixed, flexible, spaceflexible, or fullflexible]

The optional c, l, or r controls the horizontal orientation of smallest output units (keywords, identifiers, etc.).

default: [c]fixed

basewidth={fixed, flexible}

sets the width of a single character box for fixed and flexible column mode (both to the same value or individually).

default: {0.6em,0.45em}

Free spacing for comments:

\lstset{columns=fullflexible, basewidth={0.5em,0.4em}}