logo

How to Define and Use Your Colors in LaTeX 📂Writing

How to Define and Use Your Colors in LaTeX

Description

The xcolor package allows users to employ predefined colors, but users can also define and use their own colors. By including \definecolor{color name}{color space}{values} in the preamble, you can define custom colors. The color spaces include rgb, cmyk, HTML, gray, among others.

  • \definecolor{color name}{gray}{gray}: Definition of grayscale color $[0, 1]$
  • \definecolor{color name}{rgb}{r,g,b}: Definition of RGB color $[0, 1]^{3}$
  • \definecolor{color name}{cmyk}{c,m,y,k}: Definition of CMYK color $[0, 1]^{4}$
  • \definecolor{color name}{RGB}{R, G, B}: Definition of RGB color $[0, 255]^{3}$
  • \definecolor{color name}{HTML}{RRGGBB}: Definition of HTML color $\mathbb{Z}_{16}\times\mathbb{Z}_{16}\times\mathbb{Z}_{16}$

Here, gray, r, g, b, c, y, m, and k are values ranging between $0$ to $1$. R, G, and B are values ranging from $0$ to $255$, and RRGGBB refers to the RGB code expressed in $16$ hexadecimal format.

An example is shown below.

\documentclass{article}
\usepackage{xcolor}

\definecolor{my_gray}{gray}{0.8}
\definecolor{my_rgb}{rgb}{0.8,0.2,0.2}
\definecolor{my_cmyk}{cmyk}{0.8,0.5,0.3,0.4}
\definecolor{my_RGB}{RGB}{155, 15, 200}
\definecolor{my_HTML}{HTML}{BCE55C}
\begin{document}

This is {\color{my_gray}gray} text.
This is {\color{my_rgb}rgb} text.
This is {\color{my_cmyk}cmyk} text.
This is {\color{my_RGB}RGB} text.
This is {\color{my_HTML}HTML} text.

\end{document}