Color is very important to improve the look of your document when creating a document in LaTeX. You can highlight important sentences by color, highlight a word or hyperlink.
So this tutorial will be very helpful for you to use text color in LaTeX. Here we will discuss different methods of using text color in LaTeX.
About xcolor package in LaTeX
The xcolor package is the best option for using colors in latex. You can use color package but xcolor package is much more flexible and better. Use following command to import this package into your LaTeX document preamble.
\usepackage{xcolor}
This package allows you to use the following colors in documents.
If you need more color options, you can access color using these options in the xcolor package.
1. dvipsnames: \usepackage[dvipsnames]{xcolor}
load more 68 named colors.
2. svgnames: \usepackage[svgnames]{xcolor}
load more 151 named colors.
3. x11names: \usepackage[x11names]{xcolor}
load more 317 named colors.
For more information, you can refer to the xcolor package documentation.
How to change some text color in LaTeX
You can use the \color{}
command to highlight a sentence or some text with a color.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\begin{center}
Title
\end{center}
\color{red}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.\\
\color{yellow}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.\\
\color{blue}It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
\end{document}
Output :
Besides, in the example below you can see that the \color{}
command only affected group it was inside.
Here the number of first two items and last item is also shown in red because, it is in same group but text of last item is green because it is in a different group.
So you make the item number the color of the text and keep it entirely in another form.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\begin{enumerate}
\color{red}
\item red
\item red too
\item {\color{green}green}
\end{enumerate}
\begin{enumerate}
\color{red}
\item red
\item red too
\color{green}
\item green
\end{enumerate}
\end{document}
Output :
How to Change a single word color in LaTeX
If you want to change color of any word in a paragraph or sentence, you need to use the \textcolor{color}{text}
command.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\begin{center}
{\color{red}Title}
\end{center}
Contrary to popular belief, Lorem Ipsum is not simply \textcolor{blue}{random} text. It has roots in a piece of classical Latin literature from \textcolor{red}{45} BC, making it over \textcolor{red}{2000} years old.
\end{document}
Output :
Change text color by environment in LaTeX
If you want to change the color of a specific environment, you can do so with the \color{}
command.
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\begin{document}
\begin{center}
\color{red}
Title
\end{center}
\begin{equation}
\color{teal}
M = \begin{pmatrix}
0 & 0 & 1 \\
1 & 1 & 0 \\
i & j & k
\end{pmatrix}
\end{equation}
\begin{equation}
\color{cyan}
y = 8x - 9
\end{equation}
\end{document}
Output :
How to make a colored box in LaTeX
You can use the \colorbox{color}{text}
command to set the background color of the text or colored box. The size of the box is dependent on the size of the text.Use the \colorbox{bg color}{\textcolor{text }}
syntax to change the color of the text inside the box.
You can also create a framed color box with the \fcolorbox{frame color}{bg color}{text}
command. Here you can use the frame color as desired which will be the boundary of the box.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\begin{center}
\color{blue}
Use \verb|\colorbox{color}{text}| command
\end{center}
\verb|\colorbox{red}{red}|\longrightarrow \colorbox{red}{red}\\[3pt]
\verb|\colorbox{orange}{\textcolor{white}{orange}}|\longrightarrow \colorbox{orange}{\textcolor{white}{orange}}\\[3pt]
\begin{center}
{\color{blue}Use \verb|\fcolorbox{frame}{bg color}{text}| command}
\end{center}
\verb|\fcolorbox{blue}{red}{red}|\longrightarrow \fcolorbox{green}{red}{red}\\[3pt]
\verb|\fcolorbox{black}{orange}{\textcolor{white}{orange}}|\longrightarrow \fcolorbox{black}{orange}{\textcolor{white}{orange}}
\end{document}
Output :
Define custom colors in LaTeX
The xcolor package allows you to define custom colors. Which can be defined by the following syntax.
\definecolor{name}{model}{spec}
The above syntax requires three arguments: name, model and color specification. You can use name as you like as long as it is not a LaTeX command. And below are the supported models.
1.rgb: red, green, blue and a comma separated value between 0 and 1
2.RGB: Like rgb, but with values from 0 to 255
3.cmyk: Cyan, Magenta, Yellow and Black. This model is a comma-separated list with four values between 0 and 1
4.gray: Grayscale. Single integer value between 0 and 1
\documentclass{article}
\usepackage{xcolor}
\definecolor{My_color1}{rgb}{0,0.5,1}
\definecolor{My_color2}{RGB}{0,90,250}
\definecolor{My_color3}{cmyk}{1,0.502,1, 0}
\definecolor{My_color4}{gray}{0.3}
\begin{document}
\begin{enumerate}
\item \textcolor{My_color1}{It is My-color1}
\item \textcolor{My_color2}{It is My-color2}
\item \textcolor{My_color3}{It is My-color3}
\item \textcolor{My_color4}{It is My-color4}
\end{enumerate}
\end{document}
Output :