When working with LaTeX documents, you need a way to modify how the text is displayed. You can achieve this by changing font families, font styles, or size.

You can also modify how the information is displayed by changing the text color. Changing text color allows you to add information such as warnings, hyperlinks, and more.

This tutorial will discuss how to use LaTeX xcolor package to change text colors in LaTeX documents.

Basic LaTeX xcolor Package Usage

Before we can change text color in LaTex, we need to import the xcolor package. Do this by adding the following entry in your LaTeX document preamble.

It is good to note that you can also use the color package. However, this is superseded by the xcolor package as it offers more flexibility and color formats.

How to Change Text Color in LaTeX

To change the text color, use the color{} command followed by the name of the color you want to use.

For example:

The above LaTeX code will set the title color to red and the main text to blue. The following is an illustration of the code above.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-249.png" data-lazy- height="324" src="data:image/svg xml,” width=”814″>

How to Set Text Color by Environment in LaTeX

You can also set text colors for a specific environment. For example, we can display a math block in a particular color:

In the example code above, we set the entire equation block to the cyan color. Therefore, all the text inside the environment takes the color defined.

The following is the result of the code above.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-250.png" data-lazy- height="424" src="data:image/svg xml,” width=”692″>

How to Change a Single word color in LaTeX

If you only want to change a single text to a specific color inside a paragraph, you can do this using the textcolor command.

For example:

Lorem ipsum dolor sit amet, textcolor{orange}{consectetur} adipiscing elit, sed do eiusmod textcolor{magenta}{tempor incididunt} ut labore et dolore magna aliqua. Molestie nunc non blandit massa enim nec dui nunc mattis

In the example above, we set specific colors for a single word by enclosing it with curly braces.

The example code above gives us the following result:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-251.png" data-lazy- height="386" src="data:image/svg xml,” width=”810″>

How to set a Background color in LaTeX

To set a background color for a block of text, we use the command colorbox{}. For example:

In the example above, we set the text block with a background color, Mahogany.

NOTE: We use the package name as (usepackage [dvipsames]{xcolor}). This allows us to use 68 standard colors defined in the package.

Dvips colors are as shown:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-252.png" data-lazy- height="777" src="data:image/svg xml,” width=”419″>

Image source – https://en.wikibooks.org/wiki/LaTeX/Colors

How to Define Custom Colors in LaTeX

LaTeX xcolor package also allows you to define custom colors. The general syntax for color definition is:

The above command takes three arguments, the name, model, and color specification.

The name can be any custom name as long it does include a LaTeX command.

The following are the supported color models you can use:

  • rgb – Red, Green, Blue and has a comma-separated value between 0 and 1.
  • RGB – This is similar to rgb, but values range from 0 to 255.
  • cmyk – Cyan, Magenta, Yellow, and Black. This model is a comma-separated list with four values between 0 and 1. Commonly used in printer models.
  • gray – Grayscale. A single integer value between 0 and 1.

Consider the following code that shows how to define custom colors using the above models.

In the above code, we define four custom colors by using the four types of models. The result is as follows.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-253.png" data-lazy- height="260" src="data:image/svg xml,” width=”829″>

Conclusion

This tutorial showed you how to work with the LaTeX xcolor package, especially to change text colors in LaTeX and define custom colors.

To learn more about how the package works, consider the documentation.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f5adf918acf.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list