How to add and customize text colors in LaTeX?

Color plays a crucial role in improving the visual appeal of LaTeX documents. It helps highlight key information, emphasize specific words, and enhance hyperlinks.

This guide explores various methods to apply color effectively in LaTeX.

Using the xcolor Package

The xcolor package is the most versatile way to add color in LaTeX. While the color package is available, xcolor offers greater flexibility and customization. To use it, add the following command to your preamble.

\usepackage{xcolor}

This package provides a wide range of colors by default. However, if you need more options, you can load additional color sets.

1.dvipsnames – Adds 68 named colors

\usepackage[dvipsnames]{xcolor}

2.svgnames – Adds 151 named colors

\usepackage[svgnames]{xcolor}

3.x11names – Adds 317 named colors

\usepackage[x11names]{xcolor}

For a complete list of available colors, refer to the xcolor package documentation.

Applying color to text

To change the color of a sentence or phrase, use the \color{} command.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
 \begin{center}
    Title
 \end{center}
 \color{red}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.\\
 \color{yellow}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.\\
 \color{blue}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
\end{document}

Output :

Change color of text in latex.

Using color in lists

The \color{} command affects only the section it is placed in. In the example below, the first two list items remain red, while the third appears green due to a new color group.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
 \begin{enumerate}
  \color{red}
    \item red
    \item red too
    \item {\color{green}green}
 \end{enumerate}
 \begin{enumerate}
  \color{red}
    \item red
    \item red too
    \color{green}
    \item green
 \end{enumerate}
\end{document}

Output :

Use color in item with \color command.

Changing the color of a single word

To color a specific word or number within a sentence, use the \textcolor{word}{color} command.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
 \begin{center}
   {\color{red}Title}
 \end{center}
 Contrary to popular belief, Lorem Ipsum is not simply \textcolor{blue}{random} text. It has roots in a piece of classical Latin literature from \textcolor{red}{45} BC, making it over \textcolor{red}{2000} years old.
\end{document}

Output :

changing color of a single word

Applying color to entire environments

To change the color of an entire section, equation, or any environment, apply \color{} inside it.

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\begin{document}
\begin{center}
    \color{red}
    Title
\end{center}
\begin{equation}
\color{teal}
M = \begin{pmatrix}
0 & 0 & 1 \\
1 & 1 & 0 \\
i & j & k
\end{pmatrix}
\end{equation}
\begin{equation}
    \color{cyan}
    y = 8x - 9
\end{equation}
\end{document}

Output :

Change text color of environment

Creating colored boxes

Use the \colorbox{color}{text} command to apply a background color to text, with the box size adjusting to the text length.

To change the text color within the box, use \colorbox{bg color}{\textcolor{color}{text}}.

For a bordered color box, use \fcolorbox{frame color}{bg color}{text}, where the frame color defines the box’s outline.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\begin{center}
\color{blue}
    Use \verb|\colorbox{color}{text}| command
\end{center}
\verb|\colorbox{red}{red}|\longrightarrow \colorbox{red}{red}\\[3pt]
\verb|\colorbox{orange}{\textcolor{white}{orange}}|\longrightarrow \colorbox{orange}{\textcolor{white}{orange}}\\[3pt]
\begin{center}
{\color{blue}Use \verb|\fcolorbox{frame}{bg color}{text}| command}
\end{center}
\verb|\fcolorbox{blue}{red}{red}|\longrightarrow \fcolorbox{green}{red}{red}\\[3pt]
\verb|\fcolorbox{black}{orange}{\textcolor{white}{orange}}|\longrightarrow \fcolorbox{black}{orange}{\textcolor{white}{orange}}
\end{document}

Output :

Make colored box

Share tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *