Introduction to Color in LaTeX
By default, LaTeX documents are black and white, reflecting their roots in academic publishing. However, adding color can significantly improve the readability and visual appeal of your documents. Whether you want to highlight key terms, color-code sections of a technical manual, or match a university’s branding, LaTeX provides a powerful and flexible system for managing color.
In this guide, we will focus on the xcolor package, which is the gold standard for color manipulation in LaTeX. It extends the basic color package with more features, easier syntax, and support for various color models.
Setting Up the xcolor Package
To begin using colors, you must include the xcolor package in your document's preamble (the area before \begin{document}).
\documentclass{article}
\usepackage[dvipsnames]{xcolor} % The 'dvipsnames' option provides 68 additional colors
\begin{document}
Hello, world!
\end{document}Why dvipsnames?
By default, xcolor only provides a few basic colors (black, white, red, green, blue, cyan, magenta, yellow). By adding the [dvipsnames] option, you gain access to a wide palette of professional names like RoyalBlue, ForestGreen, WildStrawberry, and Sepia.
Note: There are other option sets available, such as
svgnames(providing around 150 colors) andx11names(over 300 colors). For most beginners,dvipsnamesis the perfect middle ground.
Basic Text Color Commands
There are two primary ways to apply color to your text: the \textcolor command and the \color switch.
1. The \textcolor Command
This command is used for coloring a specific snippet of text. It takes two arguments: the color name and the text itself.
This is normal text, but \textcolor{red}{this text is red} and
\textcolor{RoyalBlue}{this text is Royal Blue}.2. The \color Switch
The \color command changes the color of all following text within the current scope (like a group or an environment).
{
\color{ForestGreen}
This entire paragraph will be green because it is inside curly braces.
}
This text will be back to the default black.Warning: Be careful when using
\colorwithout braces. If you place\color{red}in the middle of a document without closing the scope, the rest of your entire document will turn red!
Defining Your Own Colors
Sometimes the predefined colors aren't exactly what you need. LaTeX allows you to define custom colors using different color models like RGB, HTML, or CMYK.
You define colors in the preamble using the \definecolor command:
\definecolor{Name}{Model}{Values}
Examples of Custom Definitions:
\usepackage{xcolor}
% Define using RGB (values from 0 to 255)
\definecolor{MyDarkBlue}{RGB}{0, 51, 102}
% Define using HTML hex codes (perfect for matching web branding)
\definecolor{CustomOrange}{HTML}{FF5733}
% Define using rgb (decimal values from 0 to 1)
\definecolor{LightGrey}{rgb}{0.8, 0.8, 0.8}
\begin{document}
\textcolor{MyDarkBlue}{This is my custom dark blue text.}
\end{document}Color Mixing
xcolor also allows you to mix colors on the fly without defining a new name. You use the ! symbol to indicate percentages.
blue!50: 50% blue mixed with 50% white.red!70!black: 70% red mixed with 30% black.
Background Colors and Frames
Beyond just changing the text color, you can highlight blocks of text by changing the background color or adding colored frames.
Colored Boxes (\colorbox)
To create a colored background for text, use the \colorbox command.
\colorbox{yellow}{This text has a yellow background.}
\colorbox{black}{\textcolor{white}{White text on a black background.}}Framed Color Boxes (\fcolorbox)
If you want a frame around your box, use \fcolorbox. It requires three arguments: the frame color, the background color, and the text.
\fcolorbox{red}{yellow}{Red frame, yellow background.}Page Background Color
To change the color of the entire page, use \pagecolor{ColorName}. This is usually placed in the preamble or at the very start of a page. To revert to a white background later, use \nopagecolor.
Coloring Tables
Adding color to tables is a great way to make data more readable. To do this, you need to load xcolor with the table option:
\usepackage[table]{xcolor}Once enabled, you can use \rowcolors to alternate row colors automatically, or \cellcolor to color specific cells.
\begin{tabular}{|l|l|}
\hline
\rowcolor{lightgray} Item & Price \\
\hline
Apples & \$1.00 \\
\cellcolor{orange!25}Oranges & \$2.00 \\
\hline
\end{tabular}Best Practices and Common Pitfalls
To create professional documents, keep these tips in mind:
- Consistency is Key: Choose a small palette of 2–3 colors and stick to them throughout the document. Using too many colors can look amateurish and distract the reader.
- Consider Accessibility: Roughly 8% of men and 0.5% of women have some form of color blindness. Avoid using red and green as the only way to distinguish information. Ensure there is high contrast between your text color and the background.
- Check Print Compatibility: If your document will be printed, remember that colors on a screen (RGB) often look different on paper (CMYK). Use the
cmykmodel for high-end professional printing. - Package Conflicts: Some packages (like
tikzorgraphicx) also handle colors. Generally, it is safest to load\usepackage{xcolor}early in your preamble to avoid "Option Clash" errors. - Use Meaningful Names: When defining custom colors, name them by their function rather than their appearance (e.g.,
\definecolor{LinkColor}{HTML}{0000EE}is better than\definecolor{DarkBlue}{HTML}{0000EE}). This makes it easier to change the entire document's theme later.
Tip: If you are creating a digital document with many links, the
hyperrefpackage works beautifully withxcolorto create colored, clickable table of contents and URLs.
For more on defining and mixing colors, see Color models and definitions; for more on text and background coloring, see Colored text and backgrounds.
