Typevia
Color and Visual Design

Colored Text and Backgrounds

This page focuses on the commands and techniques for coloring text and backgrounds. It assumes you have read Introduction to color in LaTeX documents. To use custom colors (e.g. brand shades), define them in the preamble as described in Color models and definitions.


Coloring Your Text

There are two primary ways to change the color of your text: a command for a specific snippet, or a "switch" that affects everything from that point until the end of the current scope.

1. The \textcolor Command

The \textcolor command colors only the text inside its second argument. It takes two arguments: the color name and the text.

This is normal text, but \textcolor{red}{this part is red} and this is normal again.

Use \textcolor for single words, phrases, or inline emphasis. It is the safest option because the scope is explicit.

2. The \color Command

The \color command acts as a switch. All text that follows will be in that color until the end of the current group—either a {...} block or an environment (e.g. itemize, section body).

{
\color{blue}
This entire paragraph will be blue because it is inside curly braces.
}
Now we are back to black.

Use \color when you want to color a whole paragraph or block. Always limit its scope with curly braces so the rest of your document does not stay colored.

Practical Example: In Lists and Sections

\documentclass{article}
\usepackage{xcolor}

\begin{document}

\section{Coloring Basics}
You can emphasize \textcolor{blue}{important keywords} easily.

\begin{itemize}
    \item \textcolor{red}{Red Item}
    \item \textcolor{green}{Green Item}
    \item \textcolor{orange}{Orange Item}
\end{itemize}

{\color{purple} This entire block is purple because it is inside a group. The color switch ends at the closing brace.}

\end{document}

Common Mistake: Forgetting Braces

If you use \color{red} without a surrounding group {...}, everything from that point to the end of the document (or environment) will be red.

Wrong:

\color{red} Important!
The rest of my document is now accidentally red.

Right:

{\color{red} Important!}
The rest of my document is black as intended.

Background Colors and Frames

Beyond text color, you can highlight content with background colors or framed boxes.

1. Text Backgrounds (\colorbox)

To give text a colored background, use \colorbox. The first argument is the background color, the second is the content.

\colorbox{yellow}{This text has a yellow background.}
\colorbox{black}{\textcolor{white}{White text on a black background.}}

2. Framed Backgrounds (\fcolorbox)

For a colored border and a colored background, use \fcolorbox. It takes three arguments: frame color, background color, and content.

\fcolorbox{red}{yellow}{Red border, yellow background}

For multi-line content, put a minipage (or similar block) inside the box:

\fcolorbox{red}{blue!10}{
  \begin{minipage}{0.8\textwidth}
    \textbf{Important Note:}\\
    This box has a red frame and a light blue background.
  \end{minipage}
}

3. Page Background Color (\pagecolor)

To change the background of the entire page, use \pagecolor{ColorName}. Place it in the preamble or at the start of the page. To revert to a white background later, use \nopagecolor.

\pagecolor{black}
\color{white} % Ensure text is visible
This page is in "dark mode."
% Later, on another page:
\nopagecolor

Best Practices for Text and Backgrounds

  • Contrast: Ensure high contrast between text and background. Light yellow text on white is unreadable. If you use \pagecolor{black}, set \color{white} (or another light color) so text remains visible.
  • Scoping: Always wrap \color in a group { \color{name} ... } so you do not accidentally color the rest of the document. Prefer \textcolor for short snippets.

For broader advice on consistency, accessibility, and package loading, see Introduction to color in LaTeX documents.


Summary Checklist

  • Use \textcolor{name}{text} for words or short phrases.
  • Use \color{name} inside {...} for larger blocks.
  • Use \colorbox or \fcolorbox to highlight with backgrounds and frames.
  • Use \pagecolor and \nopagecolor for full-page background changes.
  • Ensure high contrast and proper scoping for readability.