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:
Now we are ready to use colors in our text either through \textcolor or through\color. These two commands work as follows:
The colorname in the command stands for any “base color” as described in thedocumentation of xcolor package. Here is an example that shows how the color redcan be used in a document:


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:

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:

Highlighting text
To highlight text, in addition to loading color or xcolor, we also need to load the soulpackage:
[size=1em]
[size=1em]1
| [size=1em][size=1em]\usepackage{soul}
|
Now, text can be highlighted by simply using the command \hl{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:

Highlighting and coloring text
Text can be colored and highlighted by combining the previously discussed commands.
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:
Obviously, red is the name of the color to be defined, rgb is the color model, and1,0,0 are three corresponding values for red, green, and blue. In other words, for redwe use 100% red, 0% green and 0% blue. With this, we can now define a new colorOliveGreen through the rgb color model:

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.
