Site icon texblog

Using colors in a LaTeX document

Guest post by Qutub Sajib

While preparing a document in LaTeX, you may want to use colors for different purposes. Those purposes might be to color text or highlight text by changing its background color. You may even want to do both, highlight colored text. In this tutorial, we’re going to discuss how to color and highlight text, and how to define our own colors.

 
Coloring text

To color text in a document, you first need to add the color or xcolor package to the preamble of your LaTeX file. Since the xcolor package has more powerful features than the color package, in this tutorial we will be using xcolor. To do that, we add the following line to the preamble:

\usepackage{xcolor}

Now we are ready to use colors in our text either through \textcolor or through \color. These two commands work as follows:

\textcolor{colorname}{Text to be colored}

{\color{colorname}Text to be colored}

The colorname in the command stands for any “base color” as described in the documentation of xcolor package. Here is an example that shows how the color red can be used in a document:

\textcolor{red}{Text colored with} \textcolor\\
{\color{red}Text colored with} \color

 
Among the nineteen base colors listed, some frequently used colors include green, blue, violet, or purple.

It’s possible to mix multiple colors to a new color. For example, the custom colored text here is a result of 55% green mixed with 45% blue as illustrated in the example:

This is some {\color{green!55!blue}custom colored text}

With xcolor’s different package options, a large number of predefined colors can be used. The option dvipsnames loads 68 cmyk colors, the option svgnames loads 151 rgb colors, and the option x11names loads 317 rgb colors. For example, the color RedViolet is available with the dvipsnames option:

%Preabmle
\usepackage[dvipsnames]{xcolor}

%Document
This is some {\color{RedViolet}RedViolet colored text}

 
Highlighting text

To highlight text, in addition to loading color or xcolor, we also need to load the soul package:

\usepackage{soul}

Now, text can be highlighted by simply using the command \hl{text}.

This is some \hl{highlighted text}

By default, text is highlighted by changing the background color to yellow. It is possible to change the highlighting color to your desired color. The soul package provides the \sethlcolor{colorname} command to change the highlighting color. For example, we can change the color to green using:

%Preamble
\usepackage{xcolor, soul}
\sethlcolor{green}

%Document
This is some \hl{text highlighted in green}

 
Highlighting and coloring text

Text can be colored and highlighted by combining the previously discussed commands.

Some \textcolor{red}{\hl{colored, highlighted text}} (\textcolor)

Some {\color{red}\hl{colored, highlighted text}} (\color)

Note that the \hl command goes inside the \color or \textcolor command to work as expected.

 
Custom colors

Although the range of colors available in xcolor is huge, we might want to define our own custom color. Provided that xcolor package is loaded, new colors can be defined using the \definecolor command. Before defining a new color, let’s see how the color red is defined:

\definecolor{red}{rgb}{1,0,0}

Obviously, red is the name of the color to be defined, rgb is the color model, and 1,0,0 are three corresponding values for red, green, and blue. In other words, for red we use 100% red, 0% green and 0% blue. With this, we can now define a new color OliveGreen through the rgb color model:

\definecolor{OliveGreen}{rgb}{0,0.6,0}

There are numerous online color mixing tools which are useful to get the numbers right. Here is such a tool: color mixer.

 
Similarly, we can define colors through the cmyk color model. We can define OliveGreen by setting corresponding values for cyan, magenta, yellow, and black.

\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}

Exit mobile version