Colored text in LaTeX (textcolor, xcolor)
Load the xcolor package and write \textcolor{red}{text}. \definecolor creates your own colours from RGB or hex values.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{xcolor}Colour a piece of text
\textcolor{colour}{text} colours its second argument. The switch \color{colour} colours everything up to the end of the group. Without options xcolor knows 19 names: black, white, red, green, blue, cyan, magenta, yellow, gray, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal and violet.
\textcolor{red}{Red text}, \textcolor{teal}{teal text}
and {\color{blue} blue up to the closing brace}.Define your own colour
\definecolor{name}{model}{values} creates a colour. The model HTML takes six hex digits, RGB three numbers from 0 to 255 and rgb three numbers from 0 to 1.
\definecolor{brandblue}{HTML}{1F6FEB}
\definecolor{forest}{RGB}{34,139,34}
\textcolor{brandblue}{Blue from a hex code} and
\textcolor{forest}{green from RGB values}.Mix colours
An exclamation mark mixes colours without a definition. red!60!black is 60 percent red and 40 percent black; blue!30 is 30 percent blue and 70 percent white.
\textcolor{red!60!black}{Dark red}, \textcolor{blue!30}{light blue}
and \textcolor{red!50!blue}{a mix of red and blue}.More colour names
The package option dvipsnames adds 68 names such as ForestGreen, RoyalBlue and BrickRed. The options svgnames and x11names add further sets.
Complete document:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}
\textcolor{ForestGreen}{ForestGreen}, \textcolor{RoyalBlue}{RoyalBlue}
and \textcolor{BrickRed}{BrickRed}.
\end{document}Coloured background
\colorbox{colour}{text} puts the text on a coloured background. \fcolorbox{frame}{background}{text} adds a frame. Both set the text in one line without a line break.
\colorbox{yellow}{Text on yellow} and
\fcolorbox{red}{white}{text in a red frame}.Pitfalls
- A name that
xcolordoes not know stops with Undefined color `darkgreen'. The names are case-sensitive:redexists,Redneeds the optiondvipsnames. - Classes and packages such as
beamerandtikzloadxcolorthemselves. Anxcolorolder than version 3 from the end of 2023 then stops at\usepackage[dvipsnames]{xcolor}with Option clash for package geometry. In that case put\PassOptionsToPackage{dvipsnames}{xcolor}in front of\documentclass.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
xcolor - Error Undefined color `darkgreen'
- Error Option clash for package geometry
- How-to Highlight text in LaTeX
- How-to Color table rows, columns and cells in LaTeX