Fonts and Text Formatting

How to Change Text Color in LaTeX with xcolor

Change text color in LaTeX with the xcolor package, covering textcolor, color blocks, named and hex colors, custom colors, mixing, and boxes.

On this page
  1. Short answer: load xcolor and use \textcolor
  2. Load xcolor and choose a color set
  3. Color one word or phrase
  4. Color a block of text
  5. Custom colors and hex codes
  6. Mixing shades
  7. Highlights and colored boxes
  8. Printing and contrast
  9. Frequently asked questions

Short answer: load xcolor and use \textcolor

Load the xcolor package in the preamble, then use \textcolor with a color name and the text to color:

\documentclass{article}
\usepackage{xcolor}

\begin{document}
This sentence has \textcolor{red}{red text} in the middle.
\end{document}

Use xcolor rather than the older color package. Most current guides recommend it, because it includes what color offers and adds color mixing and hex codes.

Load xcolor and choose a color set

By default, xcolor provides 19 basic colors, including red, blue, green, cyan, magenta, yellow, black, white, gray, orange, and purple.

For more names, pass an option when you load the package:

\usepackage[dvipsnames]{xcolor}

The dvipsnames option adds dozens of names, such as ForestGreen and RoyalBlue. The svgnames and x11names options add many more. Color names are case-sensitive, so use the spelling from the color list, such as ForestGreen and not forestgreen.

Color one word or phrase

Use \textcolor with the color name first and the text second:

This is \textcolor{teal}{a teal phrase} in a sentence.

It also works inside math:

$E = \textcolor{teal}{mc^2}$

Color a block of text

For a longer passage, put \color inside a group, so the color ends at the closing brace:

{\color{blue}
  This paragraph and the next one are blue.

  Both stay blue until the closing brace.}

Inside an environment such as itemize, a \color command colors everything until that environment ends.

Custom colors and hex codes

To name your own color, define it in the preamble. The HTML model takes a hex code, and the rgb model takes values from 0 to 1:

\definecolor{brandblue}{HTML}{1A73E8}
\definecolor{mygreen}{rgb}{0.2, 0.6, 0.3}

Then use the name like any other color:

\textcolor{brandblue}{text in the brand color}

For a one-off color, give the hex code directly:

\textcolor[HTML]{1A73E8}{text in a hex color}

Mixing shades

xcolor mixes colors with an exclamation mark. red!70!black is 70% red mixed with black, and blue!40 is 40% blue mixed with white:

\textcolor{red!70!black}{dark red} and \textcolor{blue!40}{light blue}.

Highlights and colored boxes

The \colorbox command gives text a background color, and \fcolorbox adds a border. Both come from xcolor:

\colorbox{yellow}{highlighted phrase} and \fcolorbox{red}{yellow}{boxed phrase}.

For colored table cells, load the colortbl package and use \cellcolor or \rowcolor. For boxes with titles and several paragraphs, see how to make a text box in LaTeX.

Printing and contrast

Color does not always survive the trip to paper or to every reader:

  • Black-and-white printing turns colors into shades of gray, so two colors can look the same.
  • Light text on a light background is hard to read on screen and in print.
  • Color alone can hide meaning from some readers, for example when red and green are the only cues. Add a label or a symbol as well.

Preview the document in grayscale before you submit it.

Frequently asked questions

Should I use the color package or xcolor?

Use xcolor for new documents. Older documents may load color directly. Switching a document to xcolor is usually a small change to the preamble.

Why is my color not showing?

Check three things: that xcolor is loaded, that the color name is spelled with the right capitals, and that a \color command sits inside the group you intended.

How do I color text in a table?

Load colortbl, then use \cellcolor for one cell and \rowcolor for a whole row.

The complete guide to fonts and text formatting

LaTeX Fonts and Font Families Explained
Read the guide

Keep exploring

Guides from other topics

All topics →