# LaTeX won't let me put two background figures

I'm writing a book in LaTeX. I want to put a page-filling .png figure behind every 'Part_X' frontpage and every 'Chapter_X' frontpage. The page-filling .png picture is different for every Part and Chapter. Here is an example of what it should look like:

Here is some simplified LaTeX code that should put 'testFig_01.png' behind the 'Part_1' frontpage, and 'testFig_02.png' behind the 'Chapter_01' frontpage:

\documentclass[a4paper,11pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{wallpaper}

% Book's title and subtitle
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{
\Huge \textbf{FreeRTOS} \\
\huge The internal mechanisms explained}

% Author
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\author {
\textsc{Kristof Mulier}
\thanks{\url{www.sirris.be}}
}

% I want to put the figure 'testFig_01.png' as
% the background of the Part I frontpage.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Source: http://tex.stackexchange.com/questions/94878/page-background-color-for-part
\usepackage{xpatch}
%
\xpatchcmd{\part}{\thispagestyle{plain}}
{\ThisCenterWallPaper{1}{./testFig_01}\thispagestyle{plain}}{}{}
\xpatchcmd{\@endpart}{\vfil\newpage}{\vfil\newpage
\pagecolor{white}}{}{}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                  %%
%%                  FRONTMATTER                     %%
%%                 -------------                    %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\frontmatter %  Turns off chapter numbering
%  and uses roman numerals for
%  page numbers.

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                  %%
%%                   MAINMATTER                     %%
%%                  ------------                    %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mainmatter  %  Turns on chapter numbering,
%  resets page numbering and
%  uses arabic numerals for page numbers.

%%%%%%%%%%%%%%%%
%% CHAPTER_01 %%
%%%%%%%%%%%%%%%%
\part{My first part}

% I want to put 'testFig_02.png' as the
% background of the Chapter 1 frontpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Documentation overview}
\ThisCenterWallPaper{1}{./testFig_02}

\clearpage

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \\ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \\ Lorem ipsum list:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                  %%
%%                   BACKMATTER                     %%
%%                  ------------                    %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\backmatter   %  Turns off chapter numbering and
%  doesn't fiddle with page numbering.

\chapter{Back}
A word to say goodbye.

\end{document}


Unfortunately, the LaTeX code shown above only puts 'testFig_01' behind the 'Part_1' frontpage, but not 'testFig_02' behind the frontpage of 'Chapter_01'. Strangely, I noticed that commenting out the code for placing 'testFig_01' behind 'Part_01' will allow 'testFig_02' to be drawn behind 'Chapter_01':

% COMMENTED OUT:
% I want to put the figure 'testFig_01.png' as
% the background of the Part I frontpage.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \usepackage{xpatch}
%
% \xpatchcmd{\part}{\thispagestyle{plain}}
% {\ThisCenterWallPaper{1}{./testFig_01}\thispagestyle{plain}}{}{}
% \xpatchcmd{\@endpart}{\vfil\newpage}{\vfil\newpage
%     \pagecolor{white}}{}{}


It looks like LaTeX won't allow me to use the \ThisCenterWallPaper{..} command several times to put background images in my document. What should I do?

Remove the line that patches \@endpart. You do not want an opaque white page color. Then you get the desired result with your code.

Here is a another suggestion. In the following example all part pages get a background image that is set by \setpartbackgroundimage{<image>}. To change the image use this command before \part. To get a part page without image just use an empty argument.

If the chapter page should get a background image use \chapterbackgroundimage{<image>} after \chapter.

\documentclass[a4paper,11pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{wallpaper}

\usepackage{xpatch}
\newcommand\partbackgroundimage{}
\xpatchcmd{\part}{\thispagestyle{plain}}
{\ifdefempty{\partbackgroundimage}{}{\ThisCenterWallPaper{1}{\partbackgroundimage}}%
\thispagestyle{plain}}{}{}

\newcommand\chapterbackgroundimage[1]{\ThisCenterWallPaper{1}{#1}\clearpage}

\title{Title}
\author{Author}

\usepackage{lipsum} % dummy text
\begin{document}
\frontmatter
\maketitle

\mainmatter
\setpartbackgroundimage{example-image-A}
\part{My first part}

\chapter{Documentation overview}
\chapterbackgroundimage{example-image-B}


• But I don't really understand completely how you did it. You define a new command: \partbackgroundimage and another one \setpartbackgroundimage. Why do you define two new commands? – K.Mulier May 31 '16 at 19:02