[Tex/LaTex] Background image used for a whole section/chapter at once

backgroundsconditionals

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.

enter image description here

\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 \labels 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:

enter image description here

\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.