LaTeX Guide

LaTeX Color

Load xcolor. Use \textcolor for text and \colorbox for backgrounds. Define custom colors with \definecolor.

The basics

\usepackage{xcolor}

% Colored text
\textcolor{red}{This text is red.}
\textcolor{blue}{This text is blue.}

% Background color (colored box)
\colorbox{yellow}{Highlighted text.}
\colorbox{lightgray}{Code-style background.}

% Custom color from hex
\definecolor{brand}{HTML}{4A90E2}
\textcolor{brand}{Custom blue text.}

% Custom color from RGB (0-255)
\definecolor{warning}{RGB}{255,165,0}
\textcolor{warning}{Warning orange.}

% Mix colors with !
\textcolor{red!50!blue}{Purple from 50/50 mix.}
\textcolor{red!30}{30% red (faded).}

Named colors (xcolor default)

red
green
blue
cyan
magenta
yellow
black
white
gray
darkgray
lightgray
orange
violet
purple
brown
olive
pink
teal
lime

Load \usepackage[svgnames]{xcolor} for 150+ SVG color names (Crimson, RoyalBlue, ForestGreen, etc.).

Color tables

\usepackage[table]{xcolor}    % the [table] option matters

% Color one row
\begin{tabular}{l c c}
  Header A & Header B & Header C \\
  \rowcolor{lightgray}
  Row 1    & data     & data     \\
  Row 2    & data     & data
\end{tabular}

% Zebra striping
\rowcolors{2}{lightgray}{white}    % start at row 2
\begin{tabular}{l c}
  Header & Value \\
  A      & 1     \\
  B      & 2     \\
  C      & 3
\end{tabular}

% Single cell
\begin{tabular}{l c c}
  A & \cellcolor{red!20}highlighted & B
\end{tabular}

Hyperref link colors

\usepackage[
  colorlinks=true,
  linkcolor=blue,    % internal cross-refs
  urlcolor=blue,     % external URLs
  citecolor=blue     % bibliography citations
]{hyperref}

% Or use defined colors
\definecolor{linkcolor}{HTML}{1A73E8}
\hypersetup{linkcolor=linkcolor}

Common mistakes

  • Loading color instead of xcolor. Use xcolor — it's the modern superset.
  • Forgetting [table] option. Table coloring requires \usepackage[table]{xcolor}.
  • Hex codes with the #. xcolor uses {HTML}{4A90E2} with no #.
  • RGB values out of range. {RGB}{...} expects 0-255; {rgb}{...} expects 0.0-1.0.
Typst color is named arguments
#text(fill: red)[red text]
#text(fill: rgb("#4A90E2"))[brand blue]
#highlight(fill: yellow)[highlighted]
#box(fill: yellow)[boxed background]

No package import. RGB hex codes work directly. Try TypeTeX free.

Try TypeTeX free

Frequently Asked Questions

How do I make text colored in LaTeX?

Load \usepackage{xcolor} in your preamble, then use \textcolor{red}{your text} for inline colored text. The first argument is the color (named or custom), the second is the text. xcolor includes 19+ named colors out of the box.

How do I highlight text or set a background color?

\colorbox{yellow}{highlighted text} sets a background color (note: not the same as a true marker highlight — it's a colored box). For a more highlight-like effect with rounded corners, use the soul package: \hl{text} after \usepackage{soul} \usepackage{xcolor}.

How do I use a custom color (hex code) in LaTeX?

Define it once: \definecolor{mycolor}{HTML}{4A90E2}. Then use it: \textcolor{mycolor}{text}. The HTML model accepts 6-character hex codes without the #. For RGB values use {RGB}{74,144,226} (0-255) or {rgb}{0.29,0.56,0.89} (0-1 fractions).

What's the difference between color and xcolor packages?

color is the older basic package; xcolor is the modern superset. xcolor adds more color models (HTML, HSB), color mixing (\textcolor{red!50!blue}), and named color sets (svgnames, x11names). Always use xcolor for new documents — it's a drop-in replacement.

How do I color table rows or columns in LaTeX?

Load \usepackage[table]{xcolor} (the table option enables row/column colors). \rowcolor{lightgray} on a table row colors that whole row. \columncolor{lightgray} (in column spec like >{\columncolor{lightgray}}c) colors a column. \cellcolor{red} colors a single cell.

How do I make alternating-row tables (zebra striping)?

\usepackage[table]{xcolor}, then \rowcolors{2}{lightgray}{white} (after \begin{tabular}). The 2 is the starting row (skip the header), lightgray and white alternate. Unobtrusive zebra striping makes tables easier to read.

How do I mix two colors in LaTeX?

\textcolor{red!50!blue}{text} produces a 50/50 mix of red and blue. \textcolor{red!30}{text} produces 30% red on white background. \textcolor{red!50!black}{text} produces 50% red on black (a darker red). The ! syntax is xcolor's color expression language.

How do I set link colors (hyperref) in LaTeX?

Pass options to hyperref: \usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue,citecolor=blue]{hyperref}. linkcolor for internal references, urlcolor for external URLs, citecolor for citations. Use named colors or \definecolor names.

What named colors are available in xcolor?

Default 19: black, white, red, green, blue, cyan, magenta, yellow, gray, darkgray, lightgray, orange, violet, purple, brown, olive, pink, teal, lime. Load with [svgnames] for ~150 SVG color names (Crimson, RoyalBlue, etc.) or [x11names] for X11 colors.

More LaTeX guides