Basic Formatting
How to Use Colors in LaTeX
The xcolor package gives you access to named colors, custom color definitions, and commands for coloring text and backgrounds. It's one of the most widely used LaTeX packages.
Load the xcolor Package
Add xcolor to your preamble. The dvipsnames option provides 68 extra named colors (e.g., ForestGreen, RoyalBlue).
\usepackage[dvipsnames]{xcolor}
% Now you can use named colors like:
% red, blue, green, cyan, magenta, yellow, black, white,
% ForestGreen, RoyalBlue, Maroon, Orchid, etc.Output: The package is loaded and all named colors are available.
Color Text
Use \textcolor{color}{text} to change the color of specific text:
This word is \textcolor{red}{red} and this is \textcolor{blue}{blue}.
\textcolor{ForestGreen}{This entire sentence is forest green.}Output: The words appear in their specified colors in the PDF.
Background Colors
Use \colorbox{color}{text} to add a background color behind text:
\colorbox{yellow}{Highlighted text}
\colorbox{cyan!20}{Light cyan background}Output: Text appears with a colored rectangle behind it.
Define Custom Colors
Create your own colors using RGB, HTML hex, or CMYK values:
% RGB (0-255 values scaled to 0-1)
\definecolor{myblue}{RGB}{41, 128, 185}
% HTML hex
\definecolor{darkgold}{HTML}{B8860B}
% Mix existing colors
\definecolor{lightred}{red!30!white}
\textcolor{myblue}{Custom blue text}
\textcolor{darkgold}{Dark gold text}Output: Text appears in your custom-defined colors.
Colored Boxes with Frames
Use \fcolorbox for bordered colored boxes, or combine with tcolorbox for more advanced layouts:
% Simple framed color box
\fcolorbox{red}{yellow}{Warning: important text}
% For advanced boxes, use tcolorbox:
\usepackage{tcolorbox}
\begin{tcolorbox}[colback=blue!5, colframe=blue!50]
This is a styled box with a blue theme.
\end{tcolorbox}Output: A neatly framed box with colored fill and border appears in the PDF.
💡 Tips
- •Use color sparingly in academic papers—many journals prefer black and white
- •The ! operator mixes colors: red!30 means 30% red + 70% white
- •Use \color{red} (without text argument) to switch color for all following text until the group ends
- •For accessibility, avoid relying on color alone to convey meaning
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free