Colored text in LaTeX (textcolor, xcolor)

Load the xcolor package and write \textcolor{red}{text}. \definecolor creates your own colours from RGB or hex values.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{xcolor}

Colour a piece of text

\textcolor{colour}{text} colours its second argument. The switch \color{colour} colours everything up to the end of the group. Without options xcolor knows 19 names: black, white, red, green, blue, cyan, magenta, yellow, gray, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal and violet.

\textcolor{red}{Red text}, \textcolor{teal}{teal text}
and {\color{blue} blue up to the closing brace}.
Result
The compiled example

Open in the editor

Define your own colour

\definecolor{name}{model}{values} creates a colour. The model HTML takes six hex digits, RGB three numbers from 0 to 255 and rgb three numbers from 0 to 1.

\definecolor{brandblue}{HTML}{1F6FEB}
\definecolor{forest}{RGB}{34,139,34}
\textcolor{brandblue}{Blue from a hex code} and
\textcolor{forest}{green from RGB values}.
Result
The compiled example

Open in the editor

Mix colours

An exclamation mark mixes colours without a definition. red!60!black is 60 percent red and 40 percent black; blue!30 is 30 percent blue and 70 percent white.

\textcolor{red!60!black}{Dark red}, \textcolor{blue!30}{light blue}
and \textcolor{red!50!blue}{a mix of red and blue}.
Result
The compiled example

Open in the editor

More colour names

The package option dvipsnames adds 68 names such as ForestGreen, RoyalBlue and BrickRed. The options svgnames and x11names add further sets.

Complete document:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}
\textcolor{ForestGreen}{ForestGreen}, \textcolor{RoyalBlue}{RoyalBlue}
and \textcolor{BrickRed}{BrickRed}.
\end{document}
Result
The compiled example

Open in the editor

Coloured background

\colorbox{colour}{text} puts the text on a coloured background. \fcolorbox{frame}{background}{text} adds a frame. Both set the text in one line without a line break.

\colorbox{yellow}{Text on yellow} and
\fcolorbox{red}{white}{text in a red frame}.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages