In the following setup I tap into LaTeX marks mechanism in order to gauge whether a section starts/ends on a specific page. Based on this, an image is inserted in the background or not with the aid of eso-pic
.
\documentclass[twoside]{article}
\usepackage{graphicx}
\usepackage{eso-pic,titleps}
\usepackage{lipsum}
\newif\ifmysection
\newcommand{\placeimage}[2]{%
\mysectionfalse
\toptitlemarks% See if "my section" is at top of THIS page
\expandafter\ifnum\expandafter\pdfstrcmp\expandafter{\thesection}{#1}=0
\mysectiontrue
\fi
\bottitlemarks% See if "my section" is at bottom of THIS page
\expandafter\ifnum\expandafter\pdfstrcmp\expandafter{\thesection}{#1}=0
\mysectiontrue
\fi
\ifmysection
\AddToShipoutPictureBG*{% Add picture to background of THIS page
\AtPageCenter{% Picture is centred on page
\makebox[0pt]{% Horizontally centred
\raisebox{-.5\height}{% Vertically centred
\includegraphics[width=.7\pdfpagewidth]{#2}}}}}% Actual image
\fi
}
\newpagestyle{main}{%
\sethead[\thepage]% even-left
[\placeimage{2}{example-image-b}]% even-centre
[\thesection\quad\sectiontitle]% even-right
{\thesection\quad\sectiontitle}% odd-left
{\placeimage{2}{example-image-a}}% odd-centre
{\thepage}% odd-right
\setfoot[]% even-left
[\thepage]% even-centre
[]% even-right
{}% odd-left
{\thepage}% odd-centre
{}% odd-right
}
\pagestyle{main}
\begin{document}
\section{First test section}
\lipsum[1-15]
\section{Second test section}
\lipsum[1-15]
\section{Third test section}
\lipsum[1-15]
\end{document}
Marks are made easy using titleps
. It still allows you to use the regular title-like marks, but also to switch between dictionary-style marks (via \toptitlemarks
and/or \bottitlemarks
). Using marks is a good way to detect which sections are visible on a page in a very similar way to which \label
s are better-suited to detect page-related references. All of this stems from the asynchronous shipout procedure which requires some finesse...
In the above example, \placeimage{<num>}{<image>}
will place an <image>
in the background of the page where section <num>
, where <num>
is the expansion of \thesection
. So, if you have a different presentation of \thesection
(say \thechapter.\arabic{section}
), be sure to specify it as such.
The example can be expanded to manually override whether content will be placed or not, perhaps to accommodate end-of-chapter seconds that include images.
The following addition allows for the execution of \excludethispagebg
wherever you want a page to be excluded from the printed background image:
\documentclass[twoside]{article}
\usepackage{graphicx,refcount,multido}
\usepackage{eso-pic,titleps}
\usepackage{lipsum}
\AtEndDocument{%
\addtocounter{excludepgbg}{-1}\refstepcounter{excludepgbg}%
\label{excludepgbg-total}%
}
\newif\ifmysection
\newcounter{excludepgbg}
\newcommand{\excludethispagebg}{\stepcounter{excludepgbg}\label{excludepgbg-\theexcludepgbg}}
\newcommand{\placeimage}[2]{%
\mysectionfalse
\toptitlemarks% See if "my section" is at top of THIS page
\expandafter\ifnum\expandafter\pdfstrcmp\expandafter{\thesection}{#1}=0
\mysectiontrue
\fi
\bottitlemarks% See if "my section" is at bottom of THIS page
\expandafter\ifnum\expandafter\pdfstrcmp\expandafter{\thesection}{#1}=0
\mysectiontrue
\fi%
\ifnum\getrefnumber{excludepgbg-total}>0
\multido{\i=1+1}{\getrefnumber{excludepgbg-total}}{%
\ifnum\value{page}=\getpagerefnumber{excludepgbg-\i}
\global\mysectionfalse
\fi%
}%
\fi%
\ifmysection
\AddToShipoutPictureBG*{% Add picture to background of THIS page
\AtPageCenter{% Picture is centred on page
\makebox[0pt]{% Horizontally centred
\raisebox{-.5\height}{% Vertically centred
\includegraphics[width=.7\pdfpagewidth]{#2}}}}}% Actual image
\fi%
}
\newpagestyle{main}{%
\sethead[\thepage]% even-left
[\placeimage{2}{example-image-b}]% even-centre
[\thesection\quad\sectiontitle]% even-right
{\thesection\quad\sectiontitle}% odd-left
{\placeimage{2}{example-image-a}}% odd-centre
{\thepage}% odd-right
\setfoot[]% even-left
[\thepage]% even-centre
[]% even-right
{}% odd-left
{\thepage}% odd-centre
{}% odd-right
}
\pagestyle{main}
\begin{document}
\section{First test section}
\lipsum[1-15]
\section{Second test section}
\lipsum[1-7]
\begin{center}
\includegraphics[width=.3\linewidth]{example-image}
\excludethispagebg
\end{center}
\lipsum[8-15]
\begin{center}
\includegraphics[width=.3\linewidth]{example-image}
\excludethispagebg
\end{center}
\lipsum[16-21]
\section{Third test section}
\lipsum[1-14]
\end{document}
With each call to \excludethispagebg
a \label
is set. These labels are cycled through at page shipout and used to match with the current page being shipped out. If there is a match, the image is excluded, if not, then the image is added to the background of the page being shipped out.
You can use ifoddpage
package:
\documentclass[12pt]{book}
\usepackage{background}
\usepackage{ifoddpage}
\usepackage{kantlipsum}
\backgroundsetup{
scale=1,
opacity=0.3,
angle=0,
color=black,
contents={%
\checkoddpage
\ifoddpage
\includegraphics[width=\paperwidth]{example-image-a}
\else
\includegraphics[width=\paperwidth]{example-image-b}
\fi
}
}
\begin{document}
\NoBgThispage
\kant[1-50]
\end{document}
If you don't want to use any extra package simply do this:
\backgroundsetup{
scale=1,
opacity=0.3,
angle=0,
color=black,
contents={%
\ifodd\value{page}
\includegraphics[width=\paperwidth]{example-image-a}
\else
\includegraphics[width=\paperwidth]{example-image-b}
\fi
}
}
and remove \usepackage{ifoddpage}
.
Best Answer
You can use
\ULCornerWallPaper{1}{background.pdf}
in the document itself, not in the preamble:Putting it on a specific page (e.g. after
\newpage
or\end{titlepage}
or whatever) will make the background appear on all pages starting from that page.(Similarly, putting
\ClearWallPaper
on some page will remove the background from all pages starting with that one.)