Using Colours in LaTeX

A brief departure from the bookdesign series, as some of my future posts within and outside the series will be dealing with the use of colours, so I might as well get it out of the way first. Summarily, we’ll take a look at how to use colours in LaTeX with the xcolor package. This will work with all outputs, i.e. .dvi, .ps and .pdf.

The most useful commands for applying colours made available by xcolor are:

  • \color{color} (applies color to the text in the currrent group)
  • \textcolor{color}{text} (applies color to the specified text only)
  • \pagecolor{color} (colors the entire page to be color)
  • \colorbox{color}{text} (creates a box containing the specified text, with color as the background)
  • \fcolorbox{frame color}{bg color}{text} (same as above, but with a coloured border around the box)

See Section 2.6 of the xcolor manual for full list and more details of commands for colour application. Alternatively, if you’re using emacs, you can look it up with M-x list-colors-display.

The xcolor package has quite a collection of pre-defined colours; they are listed in Section 4 Colors by Name in the manual. Apart from the 19 “base” colours that are always available, you can also access more pre-defined colours via the dvipsnames, svgnames and x11names options. Here’s a quick example:

\documentclass[a6paper,12pt]{article}
\usepackage[x11names]{xcolor}

\begin{document}

% ‘LemonChiffon1’ from x11names
\pagecolor{LemonChiffon1}

% ‘magenta’ is a base colour
\textcolor{magenta}{Hello World!}

% ‘CadeBlue1’, ‘Firebrick2’ and ‘Goldenrod1’
% are available via x11names
What a \colorbox{CadetBlue1}{wonderful}
\fcolorbox{Firebrick2}{Goldenrod1}{world}.
\end{document}

You can mix up your own colours, too. \color{LemonChiffon1!80} means 80% LemonChiffon1 and 20% white, while \color{lime!30!yellow!60!Mahogany} would be 30% lime, 60% yellow, and 10% Mahogany. You can also give names to the colours you concoct, so that you can reuse them at various points in your document. For example:

  • \definecolor[named]{CoolBlack}{cmyk}{.3,0,0,1} defines the CMYK ‘Cool Black’ (useful if your printer asks for CMYK black instead of ‘rich black’)
  • \definecolor[named]{AquaBlue2}{rgb}{.553,.769,.98} defines ‘AquaBlue2’ with decimal RGB values (range [0,1])
  • \definecolor[named]{Hazelnut}{HTML}{BDA59B} defines ‘Hazelnut’ with HTML RGB values. Useful if you’re re-using colour definitions from CSS styesheets.

If you’re like me i.e. you just can’t hit the right figures to get a colour just right, head over to COLOURlovers for lotsa inspirations and colour schemes. I got almost all my colours for the Grid Computing Cluster report from this website.

Have fun and happy LaTeXing!

Leave a Reply

Your email address will not be published. Required fields are marked *