How to set background and text color in LaTeX?
In this tutorial, I will show you how to set the background color for your document, and change the text, list, and table color in LaTeX. Also, learn text highlights.
In order to use different colors for your document you can use the color
or xcolor
package. But I recommend the xcolor
package.
Set page color in LaTeX
First, load the xcolor
package with \usepackage{xcolor}
, then you can use the \pagecolor{<color>}
command just after \begin{document}
. Take a look.
\documentclass{article} \usepackage{xcolor} \usepackage{lipsum} \begin{document} \pagecolor{blue!20} % Set page color here \section{This is a heading} \lipsum[1] \begin{table}[h] \centering \begin{tabular}{|c|c|c|c|} \hline \textbf{Head-1} & \textbf{Head-2} & \textbf{Head-3} & \textbf{Head-4}\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline \end{tabular} \caption{This is a table with random data} \end{table} \section{This is a heading} \lipsum[1][1-9] \end{document}
Output:
Use RGB or HEX color in LaTeX
You can’t directly put RGB or HEX color on \pagecolor{<color>}
. In this case, you have to define the RGB or HEX color with a name, then you can use the name in the \pagecolor
command. Like,
For HEX: Use \definecolor{<set-name>}{HTML}{<HEX code without #>}
.
For RGB: Use \definecolor{<set-name>}{rgb}{<x, y, z>}
.
Also, you can use our color code generator to pick any color and it will generate the \definecolor
code for that color.
\documentclass{article} \usepackage{xcolor} \usepackage{lipsum} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.639, 0.675, 0.125} % \definecolor{myColor}{HTML}{a3ac20} \begin{document} \pagecolor{myColor} % Set page color here \section{This is a heading} \lipsum[1] \begin{table}[h] \centering \begin{tabular}{|c|c|c|c|} \hline \textbf{Head-1} & \textbf{Head-2} & \textbf{Head-3} & \textbf{Head-4}\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline \end{tabular} \caption{This is a table with random data} \end{table} \section{This is a heading} \lipsum[1][1-9] \end{document}
Output:
Set color for text in LaTeX
To set the whole text color for your document you can use the \color{<color-name>}
command just after the \begin{document}
. But in this case, the color of the table will not change, for this, you have to mention the color in the table.
\documentclass{article} \usepackage{xcolor} \usepackage{lipsum} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.027, 0.212, 0.192} \begin{document} \pagecolor{myColor} % Set page color here \color{white} % Set text color here \section{This is a heading} \lipsum[1] \begin{table}[h] \color{white} % For table set color here \centering \begin{tabular}{|c|c|c|c|} \hline \textbf{Head-1} & \textbf{Head-2} & \textbf{Head-3} & \textbf{Head-4}\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline \end{tabular} \caption{This is a table with random data} \end{table} \section{This is a heading} \lipsum[1][1-9] \vspace{1cm} \noindent This is a List: \begin{enumerate} \item Item \item Item \item Item \end{enumerate} \end{document}
Output:
In the above output, you can see that the text color is changing but the page number color is not changing. In this case, you need to take help of fancyhdr
package. Use this code in the preamble without changing the rest of the code to change the page number color.
\usepackage{fancyhdr} % Load the package % Define your page style \fancypagestyle{myfancy}{ \fancyhf{} % Clear header and footer \renewcommand{\headrulewidth}{0pt} % Remove header rule \renewcommand{\footrulewidth}{0pt} % Remove footer rule \fancyfoot[C]{\textcolor{white}{\thepage}} % Set color for page number } % Apply the page style \pagestyle{myfancy}
Output:
Set color for list and table separately
If you want to set a specific color for the list then you have to use the \color{<color-name>}
command inside the list environment. And for table use \color{<color-name>}
inside the table environment.
\documentclass{article} \usepackage{xcolor} \usepackage{lipsum} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.027, 0.212, 0.192} \usepackage{fancyhdr} % Load the package % Define your page style \fancypagestyle{myfancy}{ \fancyhf{} % Clear header and footer \renewcommand{\headrulewidth}{0pt} % Remove header rule \renewcommand{\footrulewidth}{0pt} % Remove footer rule \fancyfoot[C]{\textcolor{white}{\thepage}} % Set color for page number } % Apply the page style \pagestyle{myfancy} \begin{document} \pagecolor{myColor} % Set page color here \color{white} % Set text color here \section{This is a heading} \lipsum[1] \begin{table}[h] \color{yellow} % For table set color here \centering \begin{tabular}{|c|c|c|c|} \hline \textbf{Head-1} & \textbf{Head-2} & \textbf{Head-3} & \textbf{Head-4}\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline Text & Text & Text & Text\\ \hline \end{tabular} \caption{This is a table with random data} \end{table} \section{This is a heading} \lipsum[1][1-9] \vspace{1cm} \noindent This is a List: \begin{enumerate} \color{lime} % Set color for list \item Item \item Item \item Item \end{enumerate} \end{document}
Output:
Set color for a specific word or part of a text
If you want to change the color of a specific word or part of a text, you can use the \textcolor{<color-name>}{<text>}
or {\color{color-name}<text>}
command.
\documentclass{article} \usepackage{xcolor} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.027, 0.212, 0.192} \begin{document} \pagecolor{myColor} % Set page color here \color{white} % Set text color here \section{This is a heading} \textcolor{red}{Lorem Ipsum} is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \section{This is a heading} \textcolor{red}{Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,} when an unknown printer took a galley of type and scrambled it to make a type specimen book. \vspace{1cm} \noindent This is a List: \begin{enumerate} \item \textcolor{lime}{Item} \item \textcolor{red}{Item} \item Item \end{enumerate} \end{document}
Output:
Text highlighting and color box
In order to highlight a text with a colored box you can use the \colorbox{<color-name>}{<text>}. But with this command line break is not possible. So you can use this for a few texts.
\documentclass{article} \usepackage{xcolor} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.027, 0.212, 0.192} \begin{document} \pagecolor{myColor} % Set page color here \color{white} % Set text color here \section{This is a heading} \colorbox{red}{Lorem Ipsum} is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \section{You cant use for the whole} \colorbox{red}{Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.} \end{document}
Output:
To overcome this problem you have to use the \highLight[<color-name>]{<text>}
command, for this you have to load the luacolor
and lua-ul
package and use the LuaLaTeX compiler.
\documentclass{article} \usepackage{xcolor} \usepackage{lipsum} \usepackage{luacolor} \usepackage{lua-ul} % Define HEX and RGB color \definecolor{myColor}{rgb}{0.027, 0.212, 0.192} \definecolor{hl}{rgb}{0.855, 0.580, 0.106} \begin{document} \pagecolor{myColor} % Set page color here \color{white} % Set text color here \section{This is a heading} \highLight[red]{Lorem Ipsum} is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the \highLight[red]{1500s}, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \section{This is a heading} \highLight[hl]{\lipsum[1]} \end{document}
Output:
Leave a Reply