跳到内容

Introduction

This article explains how to use colour in your LaTeX document via the color or xcolor packages. Note that user-level documentation of the color package is contained in The LaTeX standard graphics bundle.

Both packages provide a common set of commands for colour manipulation, but xcolor is more flexible and supports a larger number of colour models, so is the recommended approach.

We’ll start with the following example:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
This example shows some instances of using the \texttt{xcolor} package 
to change the colour of elements in \LaTeX.

\begin{itemize}
\color{blue}
\item First item
\item Second item
\end{itemize}

\noindent
{\color{red} \rule{\linewidth}{0.5mm}}
\end{document}

 Open this xcolor example in Overleaf

This example produces the following output:

Example use of xcolor package in LaTeX

In this example, the package xcolor is imported with

\usepackage{xcolor}

then the command \color{blue} sets the blue colour for the current block of text. In this case for the itemize environment.

The code to typeset the horizontal line created by \rule{\linewidth}{0.5mm} is contained in a group, delimited by { and }, in order to keep the effects of the \color{red} local to that group.

Named colours provided by the xcolor package

As noted in the xcolor package documentation, the following named colours are always available without needing to load any package options:

list of colours in the xcolor package

Accessing additional named colours

Additional named colours can be accessed via the following xcolor package options:

  • dvipsnames: loads 68 named colours (CMYK)
  • svgnames: loads 151 named colours (RGB)
  • x11names: loads 317 named colours (RGB)

For example, if you write

\usepackage[dvipsnames]{xcolor}

you can access the following named colours:

colours loaded by dvipsnames option of xcolor package

Refer to the xcolor package documentation for tabulated lists of colours provided by the svgnames and x11names options.

Example usage

The following example uses named colours loaded via the dvipsnames option.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}
This example shows how to use the \texttt{xcolor} package 
to change the colour of \LaTeX{} page elements.

\begin{itemize}
\color{ForestGreen}
\item First item
\item Second item
\end{itemize}

\noindent
{\color{RubineRed} \rule{\linewidth}{0.5mm}}

The background colour of text can also be \textcolor{red}{easily} set. For 
instance, you can change use an \colorbox{BurntOrange}{orange background} and then continue typing.
\end{document}

 Open this xcolor example in Overleaf

This example produces the following output:

Demonstration of the xcolor package

Two new commands are also used in the example:

  • \textcolor{red}{easily}: Changes the colour of inline text. This command takes two parameters, the colour to use and the text whose colour is changed. In the example the word easily is printed in red
  • \colorbox{BurntOrange}{orange background}: Changes the background colour of the text passed in as the second argument. In the example above, the words orange background are typeset on a background colour of BurntOrange.

Loading and using named colours in the color package

You can also use the color package and load named colours via its usenames and dvipsnames package options:

\usepackage[usenames,dvipsnames]{color}

The following code uses the color package to apply the same named colours used in the previous xcolor package example.

\documentclass{article}
\usepackage[usenames,dvipsnames]{color} %using the color package, not xcolor
\begin{document}
This example shows how to use the \texttt{\bfseries color} package 
to change the colour of \LaTeX{} page elements.

\begin{itemize}
\color{ForestGreen}
\item First item
\item Second item
\end{itemize}

\noindent
{\color{RubineRed} \rule{\linewidth}{0.5mm}}

The background colour of text can also be \textcolor{red}{easily} set. For 
instance, you can change use an \colorbox{BurntOrange}{orange background} and then continue typing.
\end{document}

 Open this color example in Overleaf

This example produces the same output as the previous xcolor version:

Demonstration of the color package

Drivers and colour

Use of colour when typesetting text or math was not part of the original design of TeX; instead, use/application of colour was delegated to external “drivers” which converted TeX’s original output file format (DVI) into PostScript or PDF. In order for colour to work, driver-specific instructions had to be “injected” into TeX’s output by using a built-in TeX command called \special whose job is simply to allow code/data to pass through the typesetting process and become embedded into the output file. When the chosen driver (software) processed the typeset output it would detect the embedded code/data and act upon it to achieve whatever the user intended—such as the use of colour.

Times have changed, and PDF is now by far the most common output format used by TeX engines, long supplanting the use of legacy DVI drivers and creation of PostScript. Users also have a greater choice of TeX engine to use for typesetting their LaTeX documents, most notably pdfTeX, XeTeX or LuaTeX/LuaHBTeX, so it is still important to take account of differences in those TeX engines—ensuring the correct mechanisms are used to insert colour data (PDF instructions) into their PDF files. To support and accommodate the legacy DVI output format and the wider environment of TeX engines and workflows in use, you can, if required, configure the color or xcolor packages to use a particular “driver” so that LaTeX will generate colour data using using the appropriate method.

Drivers for color and xcolor

The color package provides out-of-the-box support for the following driver options:

  • dvipdfmx, dvips, dvisvgm, luatex, pdftex and xetex

There are other options as described in the color package documentation.

The xcolor package provides the following driver options:

  • dvips, xdvi, dvipdf, dvipdfm, dvipdfmx, luatex, pdftex, dvipsone, dviwindo, emtex, dviwin, oztex, textures, pctexps, pctexwin, pctexhp, pctex32, truetex, tcidvi, vtex and xetex.

Automatic colour driver detection

When typesetting your document LaTeX reads a configuration file called color.cfg which contains code that can determine if you are running pdfTeX, XeTeX or LuaTeX and automatically loads the appropriate driver (.def file) for you, so you don’t need to specify the driver package option for those engines.

More advanced example using the dvisvgm driver to create an SVG file

The following example uses the color package with options that load named colours and the dvisvgm driver to output colour definitions/data using SVG code:

\usepackage[dvisvgm, usenames, dvipsnames]{color}

Specifically, the output file typeset by Overleaf is converted to SVG using a program called dvisvgm, which is part of TeX Live and available on our servers. To run dvisvgm, after the page is typeset, we use a latexmkrc file which is created when you open the following code in Overleaf:

\documentclass{article}
\usepackage[dvisvgm, usenames, dvipsnames]{color}
\title{Creating SVG graphics}
\author{Overleaf}
\begin{document}
\maketitle
Hello, {\color{Apricot}in Apricot} and now in {\color{DarkOrchid} DarkOrchid} but perhaps it might look nicer if we use {\color{JungleGreen}JungleGreen}---or may not?
\end{document}

 Open this example to create an SVG file

Once Overleaf has finished compiling you can access the SVG graphic, called mygraphic.svg, by selecting Logs and outputs and choosing the Other logs and files dropdown list:

Accessing a generated file on Overleaf

Creating your own colours

It is possible to define your own colours, the manner in which the colour is defined depends on the preferred colour model. The following example uses 4 colour models.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}

\definecolor{mypink1}{rgb}{0.858, 0.188, 0.478}
\definecolor{mypink2}{RGB}{219, 48, 122}
\definecolor{mypink3}{cmyk}{0, 0.7808, 0.4429, 0.1412}
\definecolor{mygray}{gray}{0.6}

\begin{document}
User-defined colours with different colour models:

\begin{enumerate}
\item \textcolor{mypink1}{Pink with rgb}
\item \textcolor{mypink2}{Pink with RGB}
\item \textcolor{mypink3}{Pink with cmyk}
\item \textcolor{mygray}{Gray with gray}
\end{enumerate}
\end{document}

 Open this example in Overleaf

This example produces the following output:

LaeX example creating user-defined colours

The command \definecolor takes three parameters: the name of the new colour, the model, and the colour definition. Roughly speaking, each number represent how much of each colour you add to the mix that makes up the final colour.

  • rgb: Red, Green, Blue. Three comma-separated values between 0 and 1 define the components of the colour.
  • RGB: The same as rgb, but the numbers are integers between 0 and 255.
  • cmyk: Cyan, Magenta, Yellow and blacK. Comma-separated list of four numbers between 0 and 1 that determine the colour according to the subtractive CMYK model used by commercial printers.
  • gray: Grey scale. A single number between 0 and 1.

In the example, mypink1, mypink2 and mypink3 define the same colour but for different models. You can actually see that the one defined by cmyk is slightly different.

Colours defined by either model can later be used within your document not only to set the colour of the text, but for any other element that takes a colour as parameter, for instance tables (you must add the table option to xcolor), graphic elements created with TikZ, plots, vertical rulers in multicolumn documents and code listings.

xcolor-only colour models

The xcolor package provides additional commands which provide support for more colour models and friendly colour mixing, as demonstrated in the following example:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\colorlet{LightRubineRed}{RubineRed!70!}
\colorlet{Mycolor1}{green!10!orange!90!}
\definecolor{Mycolor2}{HTML}{00F9DE}
\begin{document}
This document presents several examples showing how to use the \texttt{xcolor} package 
to change the colour of \LaTeX{} page elements.

\begin{itemize}
\item \textcolor{Mycolor1}{First item}
\item \textcolor{Mycolor2}{Second item}
\end{itemize}

\noindent
{\color{LightRubineRed} \rule{\linewidth}{1mm}}

\noindent
{\color{RubineRed} \rule{\linewidth}{1mm}}
\end{document}

 Open this example in Overleaf

This example produces the following output:

Custom colours produced by the xcolor package

Three new colours are defined in this example, each one in a different manner.

  • \colorlet{LightRubineRed}{RubineRed!70!}: A new colour named LightRubineRed is created, this colour has 70% the intensity of the original RubineRed colour. You can think of it as a mixture of 70% RubineRed and 30% white. Defining colours like this lets you derive different tints of a main colour—common practice in corporate branding. In the example, you can see the original RubineRed and the new LightRubineRed used in two consecutive horizontal rules.
  • \colorlet{Mycolor1}{green!10!orange!90!}: A colour named Mycolor1 is created with 10% green and 90%orange.
  • \definecolor{Mycolor2}{HTML}{00F9DE}: The colour Mycolor2 is created using the HTML model. Colours in this model must be created with 6 hexadecimal digits, the characters A, B,C, D, E and F must be in upper-case.

The colour models that only xcolor supports are:

  • cmy: cyan, magenta, yellow
  • hsb: hue, saturation, brightness
  • HTML: RRGGBB
  • Gray: Greyscale, a number between 1 and 15.
  • wave: Wavelength, a number between 363 and 814. The number represents the wavelength of light, in nanometres (nm)

Setting the page background colour

The background colour of the entire page can be easily changed with \pagecolor. The following code demonstrates this, using the text of an earlier example::

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\colorlet{LightRubineRed}{RubineRed!70!}
\colorlet{Mycolor1}{green!10!orange!90!}
\definecolor{Mycolor2}{HTML}{00F9DE}
\begin{document}
\pagecolor{black}
\color{white}% set the default colour to white
This document presents several examples showing how to use the \texttt{xcolor} package 
to change the colour of \LaTeX{} page elements.

\begin{itemize}
\item \textcolor{Mycolor1}{First item}
\item \textcolor{Mycolor2}{Second item}
\end{itemize}

\noindent
{\color{LightRubineRed} \rule{\linewidth}{1mm}}

\noindent
{\color{RubineRed} \rule{\linewidth}{1mm}}
\end{document}

 Open this example in Overleaf

This example produces the following output:

Setting the page background colour in LaTex

The command \pagecolor{black} set the page colour to black. This is a switch command, meaning it will take effect in the entire document unless another switch command is used to revert it. \nopagecolor will change the background back to normal.

Further reading

For more information see:

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX