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}
mwwalk
I really appreciated this post. I needed to do some highlighting and coloring for my thesis and this was much easier to understand than some of the other stuff I found. For what it’s worth, I made a collection of all of the most helpful tricks I’ve discovered while writing my thesis and you can find it here: http://thewanderingengineer.com/2015/06/23/latex-tips-and-tricks-for-thesis-writing/
tom
Thanks for sharing this link. Best, Tom
Clément
Regarding custom colors, a usefull site, providing with a lot of colors is worth linking to : http://latexcolor.com/
tom
Thanks for the link! That’s an interesting service.
Rustam Abass
Thanks for this beautiful information. God bless u
Hank Wichard
Good info. What about logically controlled text color? For example, we have template documents where values are input by hand. If the values are in the acceptable range, the text would print normally. If a value was added that was outside of an acceptable range, it would print in red.
Since this is all code, I’m thinking this has to be possible in LaTeX, but I’m unsure of how to implement it. Ideas?
Thanks!
tom
Hi there,
Using an if statement would probably work. If you need help with the implementation, please provide a minimal working example.
Best, Tom
Vinod V
Here is a list of useful latex colors: https://latexcolor.blogspot.com/2019/10/list-of-latex-colors.html
Mylo
really helpful