Colours in LaTeX
The xcolor package colours text, backgrounds and table cells, and lets you define your own colours from hex or RGB values. A little colour helps a report; a lot of it makes it harder to read and to print.
Setup
\usepackage{xcolor} % basic colours
\usepackage[dvipsnames]{xcolor} % adds 68 named colours such as ForestGreen
\usepackage[table]{xcolor} % adds row and cell colours for tablesLoad xcolor once, with all the options you need in one list, for example [dvipsnames, table]. Without options you get these: red, green, blue, cyan, magenta, yellow, black, white, gray, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal and violet.
Coloured text
This is \textcolor{red}{red text}.
{\color{blue} Everything up to the closing brace is blue.}
\textcolor{teal}{\textbf{Bold and teal}}\textcolor{colour}{text} colours its argument. \color{colour} colours everything after it, so keep it inside braces or an environment.
Mixing shades
xcolor mixes colours with !. A colour followed by a percentage is mixed with white, which gives you tints for backgrounds; adding !black gives a darker shade.
red!20 % 20 percent red, 80 percent white: a pale pink
blue!60!black % 60 percent blue, 40 percent black: a dark blue
blue!50!red % half blue, half red: purpleThese work anywhere a colour name does, as in \textcolor{blue!60!black}{...}. Mind that % starts a comment in LaTeX: to print a percent sign in your text, write \%.
Your own colours
\definecolor{brand}{HTML}{1F6FEB} % hex, without the #
\definecolor{accent}{RGB}{230, 126, 34} % 0 to 255
\definecolor{muted}{gray}{0.45} % 0 is black, 1 is white
\colorlet{note}{brand!10} % a tint of another colour
\textcolor{brand}{Text in the brand colour}Define colours once in the preamble and use the names everywhere. If you change your mind later, there is one line to edit.
Backgrounds and boxes
\colorbox{yellow!30}{Highlighted text}
\fcolorbox{red}{white}{Red frame, white background}\colorbox does not break across lines. For a note or a warning that spans a paragraph, use the tcolorbox package:
\usepackage{tcolorbox} % preamble
\begin{tcolorbox}[colback=blue!5, colframe=blue!60!black, title=Note]
The API keys are stored in environment variables, never in the code.
\end{tcolorbox}colback is the background, colframe the border and title bar.
Colour in equations
\[
\textcolor{red}{a}x^2 + \textcolor{blue}{b}x + c = 0
\]Useful in slides and teaching material to point at one term. In a report, prefer explaining the term in the text, since the colour is lost when printed in black and white.
Colour in tables
Load xcolor with the table option, then:
\rowcolors{2}{gray!10}{white} % stripes, starting at row 2
\begin{tabular}{lcc}
\rowcolor{blue!15}
\textbf{Service} & \textbf{Status} & \textbf{Latency} \\
API & \cellcolor{green!25} Up & 120 ms \\
Worker & \cellcolor{red!25} Down & n/a \\
Cache & \cellcolor{green!25} Up & 4 ms \\
\end{tabular}\rowcolor colours a row, \cellcolor one cell and \rowcolors{start}{odd}{even} stripes the rows. For a whole column, use >{\columncolor{blue!10}}l in the column specification.
Coloured headings
\usepackage{titlesec} % preamble
\titleformat{\section}
{\normalfont\Large\bfseries\color{brand}}
{\thesection}{1em}{}Colour models
| Model | Values | Example |
|---|---|---|
HTML | Six hex digits | {HTML}{E61919} |
RGB | Three numbers, 0 to 255 | {RGB}{230, 25, 25} |
rgb | Three numbers, 0 to 1 | {rgb}{0.9, 0.1, 0.1} |
cmyk | Four numbers, 0 to 1 | {cmyk}{0, 0.9, 0.9, 0.1} |
gray | One number, 0 to 1 | {gray}{0.5} |
Copy hex values straight from a design tool or your university's brand guide. Use cmyk only if a print shop asks for it.
Using colour well in a report
- Pick one or two colours and use them consistently, for headings or links.
- Keep body text black. Coloured paragraphs are tiring to read.
- Never rely on colour alone to carry meaning: printed copies are often black and white.
- Use pale tints (10 to 20 percent) for backgrounds so text on top stays legible.
Where to go next
- Tables for the rest of table formatting
- TikZ and PGFPlots, which use the same colour names
- Presenting your PFE to the jury