All you need to know about colors in LaTeX

In this tutorial, we are going to explain a series of aspects related to the use of colors in LaTeX documents. The first part of this tutorial is entirely dedicated to the xcolor package, the main tool to use colors in LaTeX, and the capabilities that it offers such as:

After this overview of the xcolor package, we solve concrete situations where one may want to use colors. We start explaining:

Next, we talk about tables:

We also deal with more general topics such as:

Important: To read this tutorial, don’t go directly to the section where the problem you want to solve is explained. First, you will have to read the first section and understand the basics of the xcolor package, since they are constantly used in the subsequent parts, but no longer explained. Once you have grasped the basics of xcolor, feel free to skip to the explanation you are looking for, since the rest of the sections are all independent.

What is xcolor package in LaTeX?

The main tool to work with colors in LaTeX is the xcolor package. This package, written by Dr. Uwe Kern, is built on top of the color package (and thus the x at the beginning, which stands for extended) which provides a stable tool for handling colors within LaTeX in a consistent and driver independent way, supporting several color models.

The xcolor extended version maintains the color package characteristics while providing additional features and flexibility, with easy-to-use interfaces.

How do I change text color in LaTeX?

For now, if we have to pass color to a macro, we will simply insert the name of a common color, which is predefined in the xcolor package; later we will see how to define our own color names.

The simplest command to change the color of elements in the LaTeX document is \color{color}. This command switches the color for the current group in which the command is inserted.

Let’s look at an example to understand how it works:

% LaTeX text color

% Colors package


This shows the basic use of colors in \LaTeX.

    \item Blue
    \item More blue
    \item {\color{red} And red!}


Compiling this code yields:

LaTeX text color
Basic use of colors inside a LaTeX document

Observe how the \color command only affected the group it was inside. Note also that the last item has the number in blue and the text in red. This is due to the fact that the declaration \color{blue} affects the whole enumerate environment (and thus the numbers), but the \color{red} declaration was inside a group that only contained the text.

If we change the content of the enumerate environment to:

% ...
    \item Blue
    \item More blue
    \item And red!

we get the following result:

change text color in LaTeX
How the grouping of the code affects the colors of the output.

which is consistent with the previous explanation.

Another way to produce a similar output is to use the \textcolor{color}{text} command. This command simply prints the contents in text using the color specifed in color, so it is equivalent to {\color{color} text}.

How do you color a page in latex?

We can also change the background color of our LaTeX document using \pagecolor{color}. This declaration will set the color for the current and following pages; it is a global declaration, and it does not respect groups.

The \nopagecolor command is used to remove any currently specified page color, returning thus to the default transparent background.

In the following example, some of the commands produce a page with inverted, or negative, coloring:

% Change page background color in LaTeX

% Colors package


% Change page color to black

% Change text to white color

\section{New section}
\textbf{This is an inverted color page.}

    \item One item.
    \item Another item.
    \item A third item.


This code produces the following output:

page background color latex
A LaTeX document with negative, or inverted, colors.

How do I make a colored box in latex?

To produce a box with a certain background color, the macro \colorbox{color}{text} is provided. This simply typesets the contents of text but colors the background of the box formed by this contents with color.

We can also produce a framed box with \fcolorbox{frame color}{background color}{text}, where:

  • frame color: specifies the color of the frame around the box,
  • background color: the color of the background and text the text to be typeset inside the box.

In the following code, we use boxes to show a bunch of colors:

% Change page background color in LaTeX

% Colors package


A bunch of colors:


which produces:

A bunch of boxes with different colors.

What colors are available in LaTeX?

In the next image, you can see the set of default colors that is always defined once you load the xcolor package. These are the kinds of colors that we have been using so far. To summarize, predefined colors in LaTeX are: red, green, blue, cyan, magenta, yellow, black, gray, white, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal, violet.

Predefined colors in LaTeX
Color names defined by default when loading the xcolor package.

However, this set of colors can be extended loading the package option dvipsnames.


This loads 68 more colors as defined in the dvips driver; you can see these colors in the next image:

dvips colors in latex
The colors predefined when the package option dvipsnames is used

Even further, the x11names package option loads the 317 colors shown below:

The list of colors loaded with the x11names package option (Part 1).
The list of colors loaded with the x11names package option (Part 2).

How to define custom colors in LaTeX

To define a custom color in LaTeX, we can use the macro:


where the new color is called name using the model model and the specifications inside the model spec.

The most common models and their specification syntax are the following:

  • rgb: Is specified as three comma-separated values between 0 and 1 that represent the amount of red, green and blue (in this order) that the color has.
  • RGB: The same as before but with the values going between 0 and 255.
  • cmyk: Is specified as four comma-separated values between 0 and 1 that determine the amount of cyan, magenta, yellow and black (in this order) to be added using the additive model in most printers.
  • gray: A value in the grey scale between 0 and 1.
  • HTML: Consists of 6 hexadecimal digits that represent the color in HTML code.
  • wave: This is a fun one that may be useful when writing documents related to the field of Optics. It is a single number between 363 and 814 that specifies the wave length of the color in nanometres.

With this command, if the color name passed is an already existing one, it will be overwritten. To prevent this we can use the \providecolor command, which takes the same arguments but only makes the definition valid if the color doesn’t exist already.

\colorlet, a more flexible command for defining new colors

This is the most general and flexible way to create a new color, but maybe it is not the easiest. Probably, a more practical and intuitive is using the command:


where name is the name of the new color and combination is a combination of preexisting colors. The percentage of every color in the combination is written between ! signs. For instance:


will produce a kind of ochre color, consisting on a 20% of blue and a 80% of yellow. The percentage remaining until 100 that is not specified is considered to be white. This means that a declaration like


produces a light blue color.

Illustrative example

Let’s see how to use these commands in practice:

% Define new colors in LaTeX

% Colors package

% Define custom colors


\textcolor{cyanish}{\textbf{This is some cyan text}}
\textcolor{lightgreen}{\textbf{This is some lightgreen text}}
\textcolor{orangish}{\textbf{This is some orangish text}}
\textcolor{ochre}{\textbf{This is some ochre text}}


You can see the output produced by this code below.

Define custom colors in LaTeX
Using user-defined colors inside a LaTeX document

The most used tool when dealing with links and references inside LaTeX documents is the hyperref package, written by Sebastian Rahtz and Heiko Oberdiek. This package automatically creates hyperlinks for the table of contents, for the cites of the bibliography, and for the references to floating objects. It also provides a series of tools to create your own anchors and hyperlinks.

By default, this package prints colored frames around the hyperlinks. However, one common package option is colorlinks=true, which instead of printing frames simply changes the typesetting color; this means that a hyperlink will be written using a different color than the ordinary text.

Furthermore, every kind of link has its option to change the color used:

  • linkcolor: Color used for normal internal links (mainly the links in the table of contents and the references to figures and tables). The default value is red.
  • citecolor: Color for bibliographical citations in text. The default value is green.
  • filecolor: Color for URLs that open local files. The default value is cyan
  • menucolor: Color for Acrobat menu items. The default value is red.
  • runcolor: Color for run links. The default value is the same as filecolor.
  • urlcolor: Color for linked URLs. The default value is magenta.
  • anchorcolor: Color for anchor text. The default value is black, since it is ignored by most drivers.
  • allcolors: Color for all the options previously described.

These options are all passed a valid color. Check this detailed tutorialOpens in a new tab. for illustrative examples!

In case you set the default option colorlinks=false and stick to the framed boxes, there are also different color options that let you modify the appearance of the frame: citebordercolor, filebordercolor, linkbordercolor, menubordercolor, urlbordercolor, runbordercolor and allbordercolors. The color they change is the same as described above.

How to add colors in tables

This part is explained with much detail in “Tables in LaTeX- coloring cellsOpens in a new tab.” and here we will provide the easiest way to add colors to table cells using the colortbl packageOpens in a new tab., written by David Carlisle.

First, let’s see an example of this package in action, and then I will explain in more detail the main commands it provides:

% Add colors to columns and rows in LaTeX

% Colors package

% Colors for tables package

% use the colortbl package to define a new column


\hline \rowcolor{lime}
Variables & \multicolumn{4}{c}{Data} \\
var 1 & 1 & 2 & 3 & 4 \\ \hline
var 2 & 1 & 2 & 3 & 4 \\ \hline
var 3 & 1 & 2 & 3 & 4 \\ \hline


This code produces the following:

add colors to tables in LaTeX
A table with colored cells.

Let’s jump into the details of the code.

  • First of all, we loaded xcolor and colortbl packages.
  • Then, we defined a new kind of column, that is, a new kind of column specifier for the tabular environment. This definition, in spite of its complex notation, is very simple: we just specify the color of the column inside \columncolor and then we select its alignment with the common l, r and c specifiers. Then this specifier is used for the columns that we want to color.
  • To color rows is even simpler: we just have to use the command \rowcolor inside the row we want to color. As you can see, the color of the row has preference over the column color.

How do you color part of a formula in LaTeX?

The commands \color and \textcolor can also be used inside math mode, although the name may suggest the opposite. This enables us to write colored equations, as in the following examples:

% Add colors to Equations in LaTeX

% Colors package


\textcolor {blue}{\int_{\textcolor{red}{-\infty}}^{ \textcolor{red}{\infty}}}
\textcolor {magenta}{e^{-x^2}}\textcolor{cyan}{dx}

\frac{1}{\textcolor{red}{n}^2} =\frac{\pi^2}{6}


which produces the following result:

Add colors to equations in LaTeX
Colored equations inside math mode

Observe that in the first equation we made indiscriminate use of the colors, to illustrate its usage; but in the second, we made a more realistic use by highlighting concrete elements of the equation. The most interesting use of colors inside equations is to guide the attention of the reader through the equation, highlighting the variables, constants, unknowns or, as in this case, indexes.

How write text with a colored underline

Here we are going to describe how to underline text, as well as how to color the line used. For underlining text, LaTeX has the default macro \underline, which works well in most cases, but doesn’t have much flexibility.

To produce text with a colored underline, we will have to make use of the external soul packageOpens in a new tab., written by Melchior Franz. This package provides hyphenatable letterspacing, underlining, overstriking and highlighting, with a set of options to customize them.

When using this package, underlined text is produced with the \ul macro. The predefined underline depth and thickness of the line work well with most fonts, but they can be changed with the declaration:

\setul{underline depth}{underline thickness}

Though one may think of passing this command any pair of valid TeX dimensions, it is highly recommended to use only the ex unit (which corresponds to the width of an `x’ in the current font). This lets the values grow and shrink with the respective fonts.

And now the main setting that concerns us. By default, the underlines are black. This can be changed using the \setulcolor command at any point in the text. Of course, this command gets passed a valid xcolor color.

The cool part is that now that you know this, you get three for the price of one since the striking \st and highlighting \hl macros accept analogous configuration options for their color. Therefore, we can also customize their appearance, as it is done in the following example:

% Change underline, highlight and stricke colors

% Colors package

% For underline text
\usepackage {soul}



\setulcolor {Peach}
\ul{This is underlined text.}\\

\setstcolor {Goldenrod}
\st{This text is striked out.} \\

\sethlcolor {Aquamarine}
\hl{This text is high lighted.}\\

\sethlcolor {Black}
\color{white}\hl{\textbf{Contrasted text.}}


Compiling this code yields the following:

Change highlight underline color latex
Underlined, striked out and highlighted text using different colors


In this tutorial, we have seen a lot of options regarding the use of colors in LaTeX. Although in every case we had to use the xcolor package to define the color names and the basic coloring tools, several external packages have been appearing during the tutorial to produce more advanced and fancy outputs.

Recent Posts