Colors in LaTeX: \colorbox, \textcolor and xcolor

Quick answer

  • Load: \usepackage{xcolor}; add [dvipsnames,svgnames,table] for more names and table colours.
  • Coloured background: \colorbox{yellow}{text}; with a frame, \fcolorbox{red}{white}{text}.
  • Text colour: \textcolor{blue}{text} or {\color{blue} ...}.
  • Custom colour: \definecolor{brand}{HTML}{1A73E8} (also RGB, rgb, gray, cmyk); mix with red!30 or blue!50!black.
  • Page and tables: \pagecolor{yellow!5}, \rowcolor{gray!20}, \cellcolor{green!30}.

Colours in LaTeX come from the xcolor package: \textcolor colours text, \colorbox and \fcolorbox put a coloured background (and frame) behind it, \definecolor creates your own colours from RGB, HTML hex or gray values, and \pagecolor colours the whole page. This guide covers all of them, the dvipsnames and svgnames colour names, colour mixing, coloured tables with \rowcolor and \cellcolor, and the errors (“Undefined color”, “Option clash for package xcolor”) that stop a compile.

Colorbox in LaTeX

\colorbox{color}{text} sets text on a box filled with color. \fcolorbox{frame}{background}{text} does the same with a frame drawn in a second colour. Both come with xcolor and accept any colour name, mix or value:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}

\colorbox{yellow}{Highlighted text}

\colorbox{yellow!30}{Softer highlight: 30\% yellow, 70\% white}

\colorbox{ForestGreen!20}{A dvipsnames colour, tinted}

% Colour values without defining a name first
\colorbox[HTML]{FFE08A}{Hex background}
\colorbox[rgb]{0.85,0.93,1}{rgb background}

% Frame colour, then background colour
\fcolorbox{red}{white}{Red frame on white}

% White text on a dark box
\colorbox{black}{\textcolor{white}{White on black}}

\end{document}

Padding and Frame Width

The space between the text and the edge of the box is \fboxsep (3pt by default) and the frame thickness of \fcolorbox is \fboxrule (0.4pt). Change them inside a group so other boxes keep the defaults:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}

{\setlength{\fboxsep}{6pt}%
 \setlength{\fboxrule}{1.5pt}%
 \fcolorbox{RoyalBlue}{RoyalBlue!10}{More padding, thicker frame}}

\end{document}

Colorbox Across Several Lines

Like \mbox, a colorbox never breaks across lines: long text runs into the margin. For a coloured paragraph, put a \parbox or minipage inside the box. To highlight text that should wrap normally, use \hl from the soul package instead:

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\begin{document}

% A full-width grey paragraph
\noindent\colorbox{gray!15}{%
  \parbox{\dimexpr\linewidth-2\fboxsep}{%
    This paragraph has a light grey background and wraps across
    lines like normal text, because the parbox does the line breaking.}}

% Highlighting that breaks across lines
\sethlcolor{yellow}
\hl{This highlighted sentence wraps onto the next line when it
reaches the margin, which a colorbox cannot do.}

\end{document}

For boxes with titles, rounded corners, shadows or page breaks, use tcolorbox; see the tcolorbox example below and the full tcolorbox guide.

Colorbox in Math Mode

\colorbox switches to text mode, so put the formula back in $...$ inside it, adding \displaystyle in display math to keep fractions full size:

\documentclass{article}
\usepackage{xcolor}
\begin{document}

The term $\colorbox{yellow}{$x^2$}$ is highlighted.

\[
  a + \colorbox{yellow}{$\displaystyle\frac{b}{c}$} = d
\]

\end{document}

The xcolor Package

The xcolor package is the standard way to use colors in LaTeX. It extends the older color package with additional features like color mixing, named color palettes, and multiple color models.

Basic setup in your preamble:

\usepackage{xcolor}

You can load additional named color palettes by passing options:

% Load the dvipsnames color palette (68 named colors)
\usepackage[dvipsnames]{xcolor}

% Load the svgnames palette (150+ named colors)
\usepackage[svgnames]{xcolor}

% Load the x11names palette (300+ named colors)
\usepackage[x11names]{xcolor}

% Combine multiple palettes
\usepackage[dvipsnames, svgnames, x11names]{xcolor}

The dvipsnames option is the most commonly used and provides popular colors like ForestGreen, RoyalBlue, and Maroon. The svgnames option adds the 151 web (CSS/SVG) colour names, such as SteelBlue, Crimson, DarkOrange and Teal, and x11names adds X11 variants such as Firebrick3. Colour names are case-sensitive: forestgreen is an undefined colour.

\documentclass{article}
\usepackage[dvipsnames,svgnames]{xcolor}
\begin{document}

\textcolor{ForestGreen}{dvipsnames} and \textcolor{SteelBlue}{svgnames}
\colorbox{Crimson!15}{a tinted svgnames background}

\end{document}

Predefined Colors

The xcolor package provides a set of base colors available without any additional options:

Color NameDescription
redPure red
greenPure green
bluePure blue
cyanCyan (light blue-green)
magentaMagenta (pinkish-purple)
yellowPure yellow
blackPure black
whitePure white
orangeOrange
purplePurple
brownBrown
limeLime green
oliveOlive green
pinkPink
tealTeal (dark cyan)
violetViolet
darkgrayDark gray
grayMedium gray
lightgrayLight gray

Using Colors for Text

The most common use of color in LaTeX is to change text color. There are several ways to do this:

\documentclass{article}
\usepackage{xcolor}

\begin{document}

% Method 1: \textcolor{color}{text}
This is \textcolor{red}{red text} in a sentence.

% Method 2: \color{color} — changes all following text
{\color{blue} This entire block is blue.}

% Method 3: Scoped color change
Normal text, {\color{green} green text here}, back to normal.

% Using with bold and italic
\textcolor{purple}{\textbf{Bold purple text}}
\textcolor{orange}{\textit{Italic orange text}}

% Colored text with custom colors
\textcolor{red!70!black}{Dark red text}

\end{document}

The \textcolor command is the most straightforward approach. It takes two arguments: the color name and the text to color. The \color command changes the color for all subsequent text, so it's best used inside braces to limit its scope.

Defining Custom Colors

You can define your own colors using \definecolor with various color models:

% RGB model (values 0 to 1)
\definecolor{myblue}{rgb}{0.1, 0.3, 0.8}

% RGB model (values 0 to 255)
\definecolor{mygreen}{RGB}{34, 139, 34}

% HTML/hex model
\definecolor{mypurple}{HTML}{6A0DAD}

% CMYK model (values 0 to 1)
\definecolor{myorange}{cmyk}{0, 0.5, 1, 0}

% Gray model (0 = black, 1 = white)
\definecolor{mygray}{gray}{0.6}

% Using custom colors
\textcolor{myblue}{This text is my custom blue.}
\colorbox{mygreen!20}{Highlighted with custom green}

% Redefining an existing color
\colorlet{alertcolor}{red!80!black}
\textcolor{alertcolor}{This is an alert!}

The \definecolor command takes three arguments: the name you want to give your color, the color model, and the color values. The\colorlet command is useful for creating aliases or derived colors from existing ones.

Color Mixing

One of xcolor's most powerful features is color mixing using the ! operator:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
% Mix two colors: color1!percentage!color2
\textcolor{blue!50!red}{50\% blue + 50\% red = purple}
\textcolor{red!70!blue}{70\% red + 30\% blue}
\textcolor{green!30!yellow}{30\% green + 70\% yellow}

% Tinting: color!percentage (mixes with white)
\textcolor{red!30}{30\% red + 70\% white = light red}
\textcolor{blue!80}{80\% blue + 20\% white = light blue}
\textcolor{red!10}{Very light red / pink}

% Shading: color!percentage!black
\textcolor{blue!50!black}{Dark blue (50\% blue + 50\% black)}
\textcolor{red!70!black}{Dark red}

% Chain mixing
\textcolor{red!50!blue!50!green}{Mixed from three colors}

% Using in colorbox
\colorbox{yellow!20}{Soft yellow highlight}
\colorbox{red!10}{Very light red background}
\end{document}

The expression red!30 means "30% red mixed with 70% white" — when only one color is specified with a percentage, it's mixed with white by default. The expression blue!50!red means "50% blue mixed with 50% red." You can chain multiple mixes together to create complex colors.

Colors in Math Mode

You can colorize individual terms in equations to draw attention to specific parts:

% Color individual terms in an equation
\[
  \textcolor{red}{a}x^2 + \textcolor{blue}{b}x + \textcolor{green}{c} = 0
\]

% Color larger expressions
\[
  E = \textcolor{red}{m} \textcolor{blue}{c^2}
\]

% Color with alignment environments
\begin{align}
  f(x) &= \textcolor{red}{3x^2} + \textcolor{blue}{2x} + \textcolor{green}{1} \\
  f'(x) &= \textcolor{red}{6x} + \textcolor{blue}{2}
\end{align}

% Define colors for readability
\definecolor{termA}{HTML}{E74C3C}
\definecolor{termB}{HTML}{3498DB}
\[
  \textcolor{termA}{\underbrace{\int_0^1 f(x)\,dx}_{\text{integral}}} =
  \textcolor{termB}{\underbrace{F(1) - F(0)}_{\text{antiderivative}}}
\]

Using colors in math mode is especially useful for teaching materials, presentations, and anywhere you need to visually distinguish different parts of a formula. Load amsmath for align and \textin this snippet. When coloring a relation, preserve its operator spacing witha \mathrel{\textcolor{red}{=}} b.

Colored Boxes and Frames

For more advanced colored boxes, the tcolorbox package provides beautiful, customizable boxes:

\usepackage{tcolorbox}

% Simple colored box
\begin{tcolorbox}
  This is a default tcolorbox.
\end{tcolorbox}

% Customized box
\begin{tcolorbox}[
  colback=blue!5!white,
  colframe=blue!75!black,
  title=My Title
]
  Content inside a styled box.
\end{tcolorbox}

% Warning box
\begin{tcolorbox}[
  colback=red!5!white,
  colframe=red!75!black,
  title=Warning
]
  This is important information!
\end{tcolorbox}

% Info box with rounded corners
\begin{tcolorbox}[
  colback=green!5!white,
  colframe=green!50!black,
  arc=4mm,
  title=Tip
]
  Here is a helpful tip.
\end{tcolorbox}

% Box without title
\begin{tcolorbox}[
  colback=yellow!10!white,
  colframe=yellow!50!black
]
  A simple note box.
\end{tcolorbox}

The tcolorbox package is extremely versatile. Thecolback option sets the background color, colframe sets the border color, and you can add titles, rounded corners, shadows, and more. The tcolorbox guide covers styles, theorem boxes and breakable boxes in detail.

Page and Section Colors

You can also apply colors to section headings and even the page background:

\usepackage{xcolor}
\usepackage{titlesec}

% Colored section headings
\titleformat{\section}
  {\normalfont\Large\bfseries\color{blue!80!black}}
  {\thesection}{1em}{}

\titleformat{\subsection}
  {\normalfont\large\bfseries\color{blue!60!black}}
  {\thesubsection}{1em}{}

% Set page background color
\pagecolor{yellow!5}  % Very light yellow background

% Set default text color for the entire document
\color{darkgray}

% Remove the page background
\nopagecolor

\pagecolor sets the background used when a page is shipped out; it is not restricted by ordinary braces. Use \clearpage before changing it when the change must start on a new page. \nopagecolor removes the background rather than painting it white. The snippet shows alternatives: do not immediately reset the background if you want the tint to appear in the PDF. The titlesec package lets you customize how section headings are formatted, including their color.

Color in Tables

The colortbl package (automatically loaded by xcolor with the table option) lets you color rows, columns, and cells:

\usepackage[table]{xcolor}

% Alternating row colors
\begin{table}[h]
  \centering
  \rowcolors{2}{white}{gray!15}  % Start at row 2, alternate gray and white
  \begin{tabular}{lcc}
    \hline
    \rowcolor{blue!30} \textbf{Name} & \textbf{Score} & \textbf{Grade} \\
    \hline
    Alice   & 95 & A \\
    Bob     & 87 & B \\
    Charlie & 92 & A \\
    Diana   & 78 & C \\
    Eve     & 88 & B \\
    \hline
  \end{tabular}
\end{table}

% Color individual cells
\begin{tabular}{|l|c|c|}
  \hline
  Item & Status & Priority \\
  \hline
  Task 1 & \cellcolor{green!30} Done & Low \\
  Task 2 & \cellcolor{yellow!30} In Progress & \cellcolor{red!30} High \\
  Task 3 & \cellcolor{red!30} Blocked & Medium \\
  \hline
\end{tabular}

% Color an entire column
\begin{tabular}{|>{\columncolor{blue!10}}l|c|c|}
  \hline
  Name & Value & Unit \\
  \hline
  Length & 5.2 & m \\
  Width  & 3.1 & m \\
  Height & 2.0 & m \\
  \hline
\end{tabular}

Key commands: \rowcolor{color} colors an entire row,\cellcolor{color} colors a single cell,\columncolor{color} colors an entire column (used in the column specification), and \rowcolors{start}{odd}{even} sets up alternating row colors automatically.

dvipsnames Color Table

When you load \usepackage[dvipsnames]{xcolor}, you get access to 68 named colors. Here are some of the most commonly used:

Color NameDescription
ForestGreenDeep green, great for nature themes
RoyalBlueRich medium blue
MaroonDark brownish-red
NavyBlueDark blue
RubineRedVivid pinkish-red
CeruleanSky blue
AquamarineBlue-green
EmeraldBright green
GoldenrodWarm golden yellow
OrchidLight purple
SalmonPinkish-orange
SepiaWarm brown
PeachLight pinkish-orange
PlumDark purple
PeriwinkleSoft blue-violet
TanLight brown
VioletBlue-purple
BrickRedEarthy red
ThistlePale purple
MidnightBlueVery dark blue

Color Models Reference

The xcolor package supports several color models for defining custom colors:

ModelValuesExample
rgb3 values, each 0–1\definecolor{myred}{rgb}{0.9, 0.1, 0.1}
RGB3 values, each 0–255\definecolor{myred}{RGB}{230, 25, 25}
HTML6-digit hex (no #)\definecolor{myred}{HTML}{E61919}
cmyk4 values, each 0–1\definecolor{myred}{cmyk}{0, 0.9, 0.9, 0.1}
gray1 value, 0–1\definecolor{mygray}{gray}{0.5}

Use rgb or HTML for screen-oriented documents andcmyk for print. The RGB model is convenient when working with color values from web design tools, as it uses the familiar 0–255 range.

Readable colors and clickable links

Choose dark text on a light background and check the PDF at normal reading size. A pale tint that works behind a table row is usually too faint for the text itself. Do not distinguish results only by red versus green: add a word such as “failed” or “passed”, or use different line styles in a plot. Print a grayscale sample when readers may photocopy the document.

Define a small palette with descriptive names such as accent andwarning, then reuse those names. That lets you change the whole document without searching for dozens of numerical color values. Load xcolor before hyperref and pass the same named color to its link settings:

\documentclass{article}
\usepackage{xcolor}
\definecolor{accent}{HTML}{174A7E}
\usepackage[colorlinks=true,linkcolor=accent,urlcolor=accent]{hyperref}
\begin{document}
\section{Results}\label{sec:results}
\textcolor{accent}{A readable accent color.}
See Section~\ref{sec:results} or
\href{https://www.useoctree.com/learn}{the learning hub}.
\end{document}

Compile twice to resolve the section reference, then click both links in the PDF. For a publisher that wants uncolored text, use hyperref's hidelinksoption instead. It hides link decoration without removing the links.

Common Errors

ErrorCauseFix
Undefined control sequence on \textcolor or \colorboxxcolor is not loaded\usepackage{xcolor}
Package xcolor Error: Undefined color `ForestGreen'The name comes from an option you did not load, or its case is wrong\usepackage[dvipsnames]{xcolor}; check spelling and capitals
LaTeX Error: Option clash for package xcolorAnother package (tikz, pgfplots, a template) loaded xcolor before you passed optionsPut \PassOptionsToPackage{dvipsnames,table}{xcolor} before \documentclass; in Beamer use \documentclass[xcolor={dvipsnames,table}]{beamer}
Undefined control sequence on \rowcolor or \cellcolorTable colours need colortbl\usepackage[table]{xcolor}
Illegal parameter number in definition after \definecolorA # in the hex code\definecolor{brand}{HTML}{1A73E8}, no #
Coloured box runs into the margin\colorbox does not break linesA \parbox inside it, \hl from soul, or tcolorbox
% Option clash: tikz has already loaded xcolor without options
\documentclass{article}
\usepackage{tikz}
\usepackage[dvipsnames]{xcolor}   % ! Option clash for package xcolor

% Fix: pass the options before anything loads xcolor
\PassOptionsToPackage{dvipsnames,table}{xcolor}
\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}

Frequently Asked Questions

How do I use \colorbox in LaTeX?

Load \usepackage{xcolor} and write \colorbox{yellow}{text} for a coloured background. \fcolorbox{red}{white}{text} adds a red frame around a white box. The colour can be a name, a mix such as yellow!30, or a value: \colorbox[HTML]{FFE08A}{text}. A colorbox does not break across lines, so wrap long text in a \parbox or use \hl from soul.

How do I change the text colour in LaTeX?

Load xcolor and write \textcolor{red}{text} for a few words, or {\color{blue} ...} to colour everything up to the closing brace. Both take any colour name, a mix such as red!70!black, or a colour you defined with \definecolor.

What colours are available in LaTeX?

xcolor always defines 19 base colours: red, green, blue, cyan, magenta, yellow, black, white, gray, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal and violet. The dvipsnames option adds 68 more (ForestGreen, RoyalBlue, Maroon), svgnames adds 151 (SteelBlue, Crimson, DarkOrange) and x11names adds 317. Names are case-sensitive.

How do I use a hex colour code in LaTeX?

Define it once in the preamble with the HTML model and no # sign: \definecolor{brand}{HTML}{1A73E8}. Then use the name anywhere, as in \textcolor{brand}{text} or \colorbox{brand!15}{text}. For a one-off colour, \textcolor[HTML]{1A73E8}{text} also works.

How do I colour a table row or cell in LaTeX?

Load \usepackage[table]{xcolor}, which loads colortbl. Then start a row with \rowcolor{gray!20}, colour one cell with \cellcolor{green!30}, colour a column with >{\columncolor{blue!10}}l in the column spec, or alternate rows with \rowcolors{2}{white}{gray!15} before the tabular.

Next Steps

Free LaTeX Tools

Write LaTeX with AI assistance

Open a color example in Octree and ask AI chat about package options or a color definition. Compile the result and check contrast, table shading and PDF links.

Try Octree Free →