[Tex/LaTex] page background color for Part

backgroundscolor

I want to set the background color specifically for the page created for each "Part" section in my document (e.g. Part I xxx, Part II xxxx). I am using the book document class. Solutions for setting (and resetting) the background colour were not able to specifically color the page with Part I xxxx on it, but also coloured the page after that.

MWE:

\documentclass{book}
\usepackage{xcolor}
\begin{document}

First page

\cleardoublepage
\pagecolor{green}
\part{Example for pagecolor}
\pagecolor{white}

Last page
\end{document}

The "Part" page and the page following it are both colored green.

Best Answer

You can patch the part command to add the page color instead of adding it manually. But since a newpage is issued at the end of \part internally (when openright option is used (default)) the page color is applied to the next page also. This can be avoided by patching the \@endpart command. You can do it the hard way or the smart way as shown in the following code.

\documentclass{book}
\usepackage{xcolor}
\makeatletter
%%-----------------Hard coding----------------------
%\renewcommand\part{%
%  \if@openright
%    \cleardoublepage
%  \else
%    \clearpage
%  \fi
%  \pagecolor{green}   %% Added line here
%  \thispagestyle{plain}%
%  \if@twocolumn
%    \onecolumn
%    \@tempswatrue
%  \else
%    \@tempswafalse
%  \fi
%  \null\vfil
%  \secdef\@part\@spart}

%\renewcommand\@endpart{\vfil\newpage
%                \pagecolor{white}   %%% Added line here
%              \if@twoside
%               \if@openright
%                \null
%                \thispagestyle{empty}%
%                \newpage
%               \fi
%              \fi
%              \if@tempswa
%                \twocolumn
%              \fi}
%%--------- end of hard coding ---------------------

%%---smart coding ----------------------------------
\usepackage{xpatch}
%
\xpatchcmd{\part}{\thispagestyle{plain}}
      {\pagecolor{green}\thispagestyle{plain}}{}{}
\xpatchcmd{\@endpart}{\vfil\newpage}{\vfil\newpage
              \pagecolor{white}}{}{}
%%--------- end of smart coding ---------------------
\makeatother


\begin{document}
\chapter{One}
First page

\cleardoublepage
\part{Example for pagecolor}

Last page
\end{document}

enter image description here

Related Question