How do you change background color of page in LaTeX?

Changing the page color is important to make a page or document more attractive. For this there are different packages in LaTeX and different functionalities defined in them. By which you can make Page Color more functional.

Let’s go to the main points, start from basics.

Use xcolor pacakge

First let’s take the help of xcolor package which we all are familiar with. Because xcolor package provides an extensive range of color options for LaTeX documents.

To change page color using this package, follow these command:

\documentclass{article}
\usepackage{xcolor,lipsum}
\usepackage[margin=1.5cm]{geometry}
\pagecolor{yellow}
\begin{document}
\lipsum[1][1-6]
\section*{This is a demo text}
\lipsum[2-8]
\section*{Next demo text}
\lipsum[8-13]
\end{document}

Output :

In this example, yellow is set instead of default white background by passing color argument to the \pagecolor{yellow} command.

Use Custom Color

You can define custom colors in LaTeX using this package by specifying different color models such as RGB, HTML, and CMYK.

\definecolor{name}{model}{value}

name: name you want to assign to custom color. You can then refer to this name elsewhere in your document to use color.

model: color model you want to use, such as RGB, HTML, CMYK etc.

value: specific values for color in chosen model.

\documentclass{article}
\usepackage{xcolor,lipsum}
\usepackage[margin=1.5cm]{geometry}
% Defining custom colors
\definecolor{bgRGB}{RGB}{255,210,210} % Example color for first page
\definecolor{bgHTML}{HTML}{E0FFE0} % Example color for second page
\definecolor{bgCMYK}{cmyk}{0.1,0.1,0,0} % Example color for third page
\begin{document}
\pagecolor{bgRGB}
\section{First Page content}
\lipsum[1-5]
\vspace*{1cm}
\lipsum[6-8]
\newpage
\pagecolor{bgHTML}
\section{Second page content}
\lipsum[6-9]
\newpage
\pagecolor{bgCMYK}
\section{Third page content}
\lipsum[6-9]
\newpage
\nopagecolor
\section{Fourth page content}
\lipsum[11-15]
\end{document}

Output :

Use Mixed color

You can use mixed colors if you want. That too is possible with this package. In this case, you can use \colorlet command or use direct mixed color combination. Applications of both are shown below

\colorlet{newcolorname}{color1!percentage!color2}
\documentclass{article}
\usepackage{xcolor,lipsum,pagecolor}
\usepackage[margin=1.5cm]{geometry}
\definecolor{htmlcode}{HTML}{F6F8FA}

% Defining a mixed color (60% red and 40% blue in this example)
\colorlet{mixedcolor1}{red!60!blue}

\begin{document}

% Setting mixed color as the background for current page
\pagecolor{mixedcolor1}
\section*{Page1 : Use mixedcolor1}
\lipsum[1-5]
% Content of page with mixed background color
\newpage

% Restoring default background color (optional)
\pagecolor{green!30!htmlcode}
\lipsum[5-10]
% Rest of the document

\end{document}

Output :

mixedcolor1 is name assigned to newly defined color. This color is created by mixing 60% of color red with 40% of color blue. !60! part of command specifies proportion of red color in mix, leaving remaining 40% for blue color.

Use direct like \pagecolor{green!30!htmlcode}, created by mixing 40% of green color with 60% of a previously defined color named htmlcode.

Use Opacity

If you want to set a background color with a certain level of opacity in a LaTeX document. Here’s an example of how to set background color of a page with a specified opacity:

\documentclass{article}
\usepackage{xcolor,lipsum}
\usepackage[margin=1.5cm]{geometry}
\pagecolor{red!7!white} % 7% red and 93% white
\begin{document}
\lipsum[1-5] % Some sample text
\vspace{1.5cm}
\lipsum[1-5] % Some sample text
\end{document}

Output :

This code will set the background color to a very light red, which may create a visually similar effect to a red background with high transparency.

Use pagecolor package

This package allows you to perform many functional tasks related to page colors. There are different commands for this. And with that you can use afterpage package.

\pagecolor{none}: Using none as an argument to \pagecolor command has same effect as using \nopagecolor. It resets page color to default.

\newpagecolor{color}: This command changes background color for current and all subsequent pages to the specified color.

\restorepagecolor: This command restores the page background color that was in effect before last \newpagecolor command. It’s used to revert to previously set page color.

\afterpage{\restorepagecolor}: This command combines afterpage package with \restorepagecolor command from pagecolor package. It restoring previous page color beginning on next page.

\documentclass{article}
\usepackage{pagecolor,xcolor}
\usepackage{afterpage,lipsum}
\usepackage[margin=1.5cm]{geometry} % for margin of page

\begin{document}

\pagecolor{red!10!white} % Set color to red transparent 
\section*{Red page here}
\lipsum[1-3]
\newpage

\nopagecolor % Reset to default color
\section*{Default page color here}
\lipsum[3-6]
\newpage

\pagecolor{yellow} % Set color to yellow
\section*{Yellow page here}
\lipsum[6-9]
\newpage

\newpagecolor{green!10!white} % Set color to green transparent 
\section*{Green page here}
\lipsum[10-13]
\newpage

\restorepagecolor % Restore color to yellow (previous color)
\section*{Yellow page here}
\lipsum[15-18]
\newpage

\afterpage{\restorepagecolor} % Restore previous yellow on  next page
\section*{Yellow page here}
\lipsum[18-21]
\newpage

\section*{Still yellow page here}
\lipsum[15-18]
\end{document}

Output :

Use gradient background with tikz package

You can create a gradient background color for a page in LaTeX by using the tikz package.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum} % for generating dummy text

\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-10] % some text

\end{document}

Output :

This example will create a gradient on a single page. If you want to apply the gradient to every page of your document, you could use the background package in conjunction with TikZ:

\documentclass{article}
\usepackage{tikz}
\usepackage{background}
\usepackage{lipsum} % for generating dummy text

\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!20, bottom color=green!20] (current page.south west) rectangle (current page.north east);
    \end{tikzpicture}
  }
}

\begin{document}
\lipsum[1-30] % some text
\end{document}

Output :

Even odd page color

You can create a document where odd and even pages have different colors for their background. You can do this by using the afterpage and xcolor packages.

\documentclass{article}
\usepackage{xcolor,afterpage}
\usepackage{lipsum} % for generating dummy text
\usepackage[margin=1.5cm]{geometry} % for page margin
\newcommand{\togglePageColor}{
  \ifodd\value{page}
    \pagecolor{red!10}
    \afterpage{\togglePageColor}
  \else
    \pagecolor{green!10}
    \afterpage{\togglePageColor}
  \fi
}

\begin{document}
\togglePageColor

\lipsum[1-25] % Add some text to create multiple pages

\end{document}

Output :

This code uses \afterpage command to insert \togglePageColor command after each page. This ensures that command is executed at the right time, even on first page, changing color as needed.

It checks whether page number is odd or even and sets the color accordingly. It then uses \afterpage command again to continue process on subsequent pages.

Another solution could make use of eso-pic package to achieve a similar result. This method combines ifthen package to differentiate between odd and even pages.

\documentclass{article}
\usepackage{lipsum} % for generating dummy text
\usepackage[margin=1.5cm]{geometry} % for page margin
\usepackage{xcolor,eso-pic,ifthen}

\AddToShipoutPictureBG{
  \ifthenelse{\isodd{\value{page}}}
    {\AtPageLowerLeft{\color{blue!10}\rule{\paperwidth}{\paperheight}}}
    {\AtPageLowerLeft{\color{red!10}\rule{\paperwidth}{\paperheight}}}
}

\begin{document}

\lipsum[1-30] % Add some text to create multiple pages

\end{document}

Output :

Use reset page

Resetting page color in LaTeX to default (typically white) can be achieved through a couple of methods:

1. Using \nopagecolor: This command resets page color to default for current and subsequent pages.

2. Using \pagecolor{none}: This command is equivalent to \nopagecolor and also resets page color to default.

3. Third, you can use white background.

\documentclass{article}
\usepackage{pagecolor}
\usepackage{xcolor}

\begin{document}

\pagecolor{blue} 
 Blue page here
\newpage

\nopagecolor 
 White page here 
\newpage

\pagecolor{red} 
 Red page here
\newpage

\pagecolor{none} 
 White page here
\newpage

\newpagecolor{green} 
 Green page here
\newpage

\newpagecolor{white} 
 White page here

\end{document}

Output :

Conclusion

All types of page color related queries are solved by this latex code along with beautiful examples to illustrate you. From defining colors to color gradients, every problem is covered in this tutorial.

Md Jidan Mondal

LaTeX expert with over 10 years of experience in document preparation and typesetting. Specializes in creating professional documents, reports, and presentations using LaTeX.

Leave a Comment

Your email address will not be published. Required fields are marked *