I've seen a few examples with \BgThisPage
being used to specify a background image for 1 single page (or another command to set one for ALL pages at once). Those two extremes are the only options I have found. In total it means that you set the background image only when you have written the document as else it can change where each page is (if you don't want the background image on EVERY page that is).
Is there any way to make it so that the background image is displayed for all pages that belong to a specific section (or chapter) without having to wait for defining it till the document is finished (thus not defining it on a per page base that the specific page has a background image)?
\documentclass{article}
\usepackage{graphicx}
\usepackage[pages=some]{background}
\backgroundsetup{
scale=1,
angle=0,
opacity=0.7,
contents={
\includegraphics[width=\textwidth,height=4cm]{example-image-a}}
}
\begin{document}
\section{mysection}
\BgThispage
Mypage
\end{document}
Best Answer
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
.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: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.