Site icon texblog

How to define custom colors in LaTeX

There are different ways to define a specific color in LaTeX. Often, I just choose a predefined color from the xcolor package or define a color using the RGB color model. However, I personally like the red!40!blue notation best. This creates purple with 40% red and 60% blue. In this post, I will introduce these different methods and provide examples to illustrate them.

 
Predefined colors

The xcolor package has an extensive set of predefined colors that are made available through options. These colors are listed in the package documentation.

\documentclass[11pt]{article}
\usepackage{blindtext}

\usepackage[x11names]{xcolor}

\begin{document}

\textcolor{RoyalBlue2}{\blindtext}

\end{document}

 
Mixing colors using a color model

Alternatively, the xcolor package allows defining custom colors, using color models: gray, rgb, HTML, cmyk and others. I would pick the model that I am most familiar with. Here is an example:

\documentclass[11pt]{article}
\usepackage{blindtext}

\usepackage{xcolor}
\definecolor{myOrange}{rgb}{1,0.5,0}

\begin{document}

\textcolor{myOrange}{\blindtext}

\end{document}

 
Mixing two colors

Finally, the xcolor package also implements a command to define a new color by mix two predefined colors. For example, to define a new purple using the command \colorlet, I mix 40% blue with 60% red.

\documentclass[11pt]{article}
\usepackage{blindtext}

\usepackage{xcolor}
\colorlet{myPurple}{blue!40!red}

\begin{document}

\textcolor{myPurple}{\blindtext}

\end{document}

Personally, I like this approach better, because it seems a more natural way to define a color than trying to decompose it into their red, green, and blue components.

 
Example: Shiny colors

I find the basic colors such as red, green, or yellow, rather strong and unpleasant to my eye. To decrease their strength, we can use the approach above and mix them with gray. For example, combining 40% color with 60% gray makes these colors much more pleasant to the eye. See for yourself:

\documentclass[border=10pt,varwidth]{standalone}
\usepackage{pgffor}
\usepackage[x11names]{xcolor}

\newcommand\colorrulemix[1]{\textcolor{#1!40!gray}{\rule{1cm}{1cm}} }
\newcommand\colorrule[1]{\textcolor{#1}{\rule{1cm}{1cm}} }

\begin{document}

\begin{center}
\foreach \name in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {%
	\colorrule{\name}}
	
\rule{\linewidth}{1pt}
\phantom{}\par


\foreach \name in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {%
	\colorrulemix{\name}}
\end{center}
\end{document}
Exit mobile version