# How to change the background color only for the current page and next page?

I found that I can change current pagecolor with

\newpagecolor{color}\afterpage{\restorepagecolor}

so I tried to use

\newpagecolor{color}\newpagecolor{color}\afterpage{\restorepagecolor},

that unfortunately changes the color for all pages after this command. Any ideas how to limit this command only to the current and the next page?

• The second \newpagecolor{color} after \newpagecolor is wrong since the restore color is overwritten with that value, so if the previous colour was white, and \newpagecolor{red} is used, then \restorepagecolor would restore to white, since this is the previous color information. \newpagecolor{red}\newpagecolor{red} would first save white, but then then 2nd usage would save red, so \restorepagecolor would restore to red for the rest of the document (or until another \newpagecolor is used)
– user31729
Oct 15 '17 at 7:18

Set the page colour, and then \restore it using a nested \afterpage:

\documentclass{article}

\usepackage{pagecolor,afterpage}
\usepackage{lipsum}

\begin{document}

\lipsum[1-20]
\newpagecolor{red}
\afterpage{\afterpage{\restorepagecolor}}
\lipsum[1-40]

\end{document}


Exactly the same approach can be followed using xcolor's \pagecolor command.