Tango LaTeX Syntax Highlighting
As I have previously mentioned, I am quite a fan of the Tango color palette. In this installment, I want to show how to highlight code listings in LaTeX using the Tango colors like in the following Smalltalk example:
The screenshot above is actually from a full blown example which I have created for XeLaTeX. If you wish to typeset the example in pdfLaTeX or similar, you can just comment out the lines it complains about.
- Syntax highlighting sample (PDF)
- The original source (TEX)
Let’s go through the source. First of all, you will need the xcolor package to define the custom colors:
\usepackage{xcolor}
Then we can define the colors. I have simply converted the hex color definitions into rgb values acceptable to xcolor. For more information, see the xcolor documentation.
\definecolor{Butter}{rgb}{0.93,0.86,0.25}
\definecolor{Orange}{rgb}{0.96,0.47,0.00}
\definecolor{Chocolate}{rgb}{0.75,0.49,0.07}
\definecolor{Chameleon}{rgb}{0.45,0.82,0.09}
\definecolor{SkyBlue}{rgb}{0.20,0.39,0.64}
\definecolor{Plum}{rgb}{0.46,0.31,0.48}
\definecolor{ScarletRed}{rgb}{0.80,0.00,0.00}
Note that this is just a part of the color definition. See the source for all available colors.
Now that we have defined the Tango colors, we can use them to highlight our source. Depending on the language that you are wishing to highlight, not all numbered keywords will be applicable. I use these different keywordstyles to highlight the above Smalltalk source.
\lstset{
keywordstyle=[1]{\color{DarkSkyBlue}},
keywordstyle=[2]{\color{DarkScarletRed}},
keywordstyle=[3]{\bfseries},
keywordstyle=[4]{\color{DarkPlum}},
keywordstyle=[5]{\color{SkyBlue}},
commentstyle={\color{Aluminium4}},
stringstyle={\color{Chocolate}},
basicstyle={\ttfamily},
}
And that’s all you need to use Tango colors for you syntax highlighting in LaTeX. Again, take a look at the source code to see the above code in full.
Tags: code, color, highlight, latex, listing, smalltalk, source, syntax, tango, tex, typeset, xelatex, xetex

December 31st, 2008 at 19:40
Hmm… your example doesn’t have a keyword message with multiple parameters in it though. In particular, the method you are defining doesn’t take multiple parameters. I can’t see any way to safely highlight a method definition like “on: anEvent do: aBlock” in LaTex… and it would be nice to have that method declaration in bold.
January 1st, 2009 at 14:40
@Julian: Yes, you are certainly right about that but unfortunately that’s an inherit problem of the listings package. I wouldn’t know how to tell it to highlight multiple keyword messages nor of a LaTeX package that would support such syntax highlighting. Maybe one could use pygments or some other syntax highlighting library instead. The post was more about getting nice colors in the syntax highlighting.
January 10th, 2009 at 12:59
hi philip. i love your tango syntax highlighting.
how to use it with LyX? i’ve tried to put it in LaTeX Preamble but still got some errors.
January 25th, 2009 at 12:15
@kholis: I’m not to familiar with LyX, I am afraid. I assume you are including xcolor and lstlistings? Are you using pdfLaTeX? You might need to upload the errors so that I can say more.
March 11th, 2009 at 13:05
Can you make one for TextMate, please?
June 22nd, 2009 at 09:35
Hello,
If you use this with LaTeX then watch out that Acrobat Reader on Windows might not open the file, but instead give a warning about a bad PDF file. If that happens, remove this part of the LaTeX code:
% HYPERREF
\usepackage[dvipdfm, bookmarks, colorlinks, breaklinks, pdftitle={Glare},
pdfauthor={Philipp Bunge}]{hyperref}
\hypersetup{
linkcolor=DarkSkyBlue,
citecolor= DarkSkyBlue,
filecolor= DarkSkyBlue,
urlcolor= DarkSkyBlue
}
November 10th, 2009 at 18:41
Thank you very much for publishing this.
June 28th, 2010 at 15:02
[...] been looking at Philip Bunge’s post on how to create a “Tango” style with LaTeX listings, and trying to adapt this to make the default text style white and the background black (this is [...]