Octree LogoOctree

Using Colors in LaTeX

Colors can improve readability, highlight important information, and create visual hierarchy in your LaTeX documents. Whether you need colored text, backgrounds, or tables, LaTeX's xcolor package provides everything you need.

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, andMaroon. The svgnames and x11names options add even more named colors based on SVG and X11 color standards.

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:

\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.

Background Colors

You can add background colors to text using \colorbox and\fcolorbox:

% Colored background
\colorbox{yellow}{Highlighted text}

% Colored background with a border
\fcolorbox{red}{white}{Text with red border on white background}

% Nested: colored text on colored background
\colorbox{black}{\textcolor{white}{White text on black}}

% Light highlight effect
\colorbox{yellow!30}{Softly highlighted text}

% Multi-line colored box (use minipage)
\colorbox{blue!10}{%
  \begin{minipage}{0.8\textwidth}
    This is a longer block of text with a
    light blue background that spans
    multiple lines.
  \end{minipage}
}

\colorbox{bg-color}{text} sets the background color.\fcolorbox{border-color}{bg-color}{text} adds a colored frame (border) around the box as well.

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:

% 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}

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.

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.

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}

% Reset page color back to white
\nopagecolor

Using \pagecolor sets the background color for all pages. Use very light tints (like yellow!5) to avoid readability issues. 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}{gray!15}{white}  % 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.

Next Steps

Free LaTeX Tools

Write LaTeX with AI assistance

Octree's AI can help you write LaTeX with beautiful colors, formatting, and more. Just describe what you want!

Try Octree Free →