Specify Page Background Color in LaTeX

Jun. 01, 2024

Introduction

Jidan’s blog, How do you change background color of page in LaTeX1, gives some examples showing how to specify page background color when typesetting in LaTeX system. In this post, I will reproduce and take some notes about some of them which I think are fundamental and commonly used.


LaTeX xcolor package

\pagecolor{$COLOR_NAME} command is provided by LaTeX xcolor package2, and its function is to “specify the background color for the current, and all following, pages.”, and “it is a global declaration which does not respect $\mathrm{\TeX}$​ groups.” By setting $COLOR_NAME as some built-in color names preset by xcolor package, it is easy to change page background color:

NOTE: More information about built-in color names of xcolor package can be found in my previous blog.3

1
2
3
4
5
6
7
8
9
10
11
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{lipsum}

\usepackage{xcolor}
\pagecolor{yellow}

\begin{document}
	\lipsum[1-10]
\end{document}

png-1

png-2

As mentioned above, \pagecolor command will specify background color for all following pages. As in the above case I use it in the preamble, it will dictate all pages in the documentation. If we want to cancel page color settings, we can use \newpage command to create a new page, and \nopagecolor, which is also provided by xcolor package, to clear background color for subsequent pages, like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{lipsum}

\usepackage{xcolor}
\pagecolor{yellow}

\begin{document}
	\lipsum[1-6]
	
	\newpage
	\nopagecolor
	\lipsum[1-10]
\end{document}

png-1

png-2

png-3

Besides, by combining \pagecolor and \newpage commands in the main document environment, we can specify page background color for different parts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage[x11names]{xcolor}

\usepackage{lipsum}

\begin{document}
	\pagecolor{AntiqueWhite1}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{AntiqueWhite2}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{AntiqueWhite3}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{AntiqueWhite4}
	\lipsum[1-6]
\end{document}

NOTE: To use color name AntiqueWhite1 and etc., we should specify option x11names when importing xcolor package3.

png-1

png-2

png-3

png-4

In addition to those built-in color names, we can customize our own colors in the preamble by \definecolor command or \colorlet, where both commands are also provided by xcolor package2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage[x11names]{xcolor}
\definecolor{bgRGB}{RGB}{255,210,210}
\definecolor{bgHTML}{HTML}{E0FFE0}
\definecolor{bgCMYK}{cmyk}{0.1,0.1,0,0}
\colorlet{mixedcolor}{red!60!blue} % Mixed color
\colorlet{transparentRed}{red!7!white} % Transparent red

\usepackage{lipsum}

\begin{document}
	\pagecolor{bgRGB}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{bgHTML}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{bgCMYK}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{mixedcolor}
	\lipsum[1-6]
	
	\newpage
	\pagecolor{transparentRed}
	\lipsum[1-6]
\end{document}

png-1

png-2

png-3

png-4

png-5

What’s more, we can globally specify background color for those even and odd pages respectively. To this end, the \afterpage command of afterpage package4 is needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{xcolor,afterpage}
\newcommand{\togglePageColor}{
	\ifodd\value{page}
	\pagecolor{red!20}
	\afterpage{\togglePageColor}
	\else
	\pagecolor{green!20}
	\afterpage{\togglePageColor}
	\fi
}

\usepackage{lipsum}

\begin{document}
	\togglePageColor
	
	\lipsum[1-20]
\end{document}

png-1

png-2

png-3

png-4


LaTeX pagecolor package

Actually, as mentioned in Jidan’s blog1, there is a more professional package called pagecolor5, providing several other commands to control page background color in a more detailed way. However, at this moment, \pagecolor command of xcolor package is enough to meet my ends, so I wouldn’t introduce more about pagecolor package here.


LaTeX tikZ package

Besides using commands of xcolor package to set page background color, another way is through tikz package. It is realized by creating a colorful shade covering the whole page by tikzpicture environment. More importantly, this way allows us to make a page background with gradient colors.

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{tikz}
\usepackage{lipsum}

\begin{document}
	\begin{tikzpicture}[remember picture, overlay]
		\shade[top color=red!30, bottom color=yellow] (current page.south west) rectangle (current page.north east);
	\end{tikzpicture}
	
	\lipsum[1-11]
\end{document}

png-1

png-2

However, as can be seen, this method can merely change background color of one page. If we want to specify page background color for all pages in the documentation, we can put aforementioned tikzpicture environment into contents option of \backgroundsetup command. \backgroundsetup is from background package6, and actually background package provides functions to control many other aspects of page background.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
\documentclass[a4paper,12pt]{article}
\usepackage[margin=1in]{geometry}

\usepackage{tikz}
\usepackage{background}

\backgroundsetup{
	scale=1,
	color=black,
	opacity=1,
	angle=0,
	position=current page.south,
	vshift=0cm,
	hshift=0cm,
	contents={
		\begin{tikzpicture}[remember picture, overlay]
			\shade[top color=red!30, bottom color=yellow] (current page.south west) rectangle (current page.north east);
		\end{tikzpicture}
	}
}

\usepackage{lipsum}

\begin{document}
	\lipsum[1-11]
\end{document}

png-1

png-2


References