# Background image used for a whole section/chapter at once

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}

• There is a minor difference between a per-section and per-chapter image. The former (section) has no page break between them, while there is typically a clear separation (like a page break) between the latter/chapters. Therefore, adding a background image on a per-chapter basis would be easier than doing it on a per-section basis. – Werner Jul 28 '14 at 18:35
• you mean between 2 sections correct? (as a secvtioni itself can be longer than 1 page if I'm not mistaken?) – Thomas Jul 28 '14 at 18:38
• Yes. One section could finish with another starting on the same page. In such cases, would you place a background image or not? So, for example, would you place a background image on ALL pages where you have section X? This will never happen with a chapter (typically). – Werner Jul 28 '14 at 18:41
• my structure in the current file is so that I have chapters and sections within and at the end of a chapter a blank page with a picture. In that case I want it for the whole chapter except where the picture is. in other cases where 2 sections are on a page: yes background image shown. if a specific section does not want a background image it shouldn't start on a page where a section with one is. thus in effect if a section has a background image the whole section has it. – Thomas Jul 28 '14 at 18:45
• Are you using fancyhdr at all? One may be able to tap into LaTeX's marks to gauge whether placement of a background image should be done or not. – Werner Jul 28 '14 at 19:20

It depends on you actual intent, but you can add the background for all the pages in a sectional unit using a conditional on its number, for example to have the material only on the second section one could use:

\documentclass{article}
\usepackage{graphicx}
\usepackage[contents={}]{background}
\usepackage{lipsum}

\ifnum\value{section}=2\relax
\backgroundsetup{
scale=1,
angle=0,
opacity=0.7,
contents={
\includegraphics[width=\textwidth,height=4cm]{example-image-a}}
}
\fi
\BgMaterial
}
\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}


And to have the material only on sections one and three:

\documentclass{article}
\usepackage{graphicx}
\usepackage[contents={}]{background}
\usepackage{lipsum}

\ifnum\value{section}=1\relax
\backgroundsetup{
scale=1,
angle=0,
opacity=0.7,
contents={
\includegraphics[width=\textwidth,height=4cm]{example-image-a}}
}
\else
\ifnum\value{section}=3\relax
\backgroundsetup{
scale=1,
angle=0,
opacity=0.7,
contents={
\includegraphics[width=\textwidth,height=4cm]{example-image-a}}
}
\fi\fi
\BgMaterial
}
\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}


The ideal situation, however, is to do this for sectional units that start on a page of their own (chapters) to guarantee proper placement or not placement of the background material in the first and last pages; otherwise, some manual adjustments might be necessary for the first and last pages of the group having the background material.

For a range of pages, the same idea applies, but noy using a conditional test on the value of the page counter so, for example, to have the material appear from page 3 to page 6 one could use

\documentclass{article}
\usepackage{graphicx}
\usepackage[contents={}]{background}
\usepackage{lipsum}

\ifnum\value{page}<7\relax
\ifnum\value{page}>2\relax
\backgroundsetup{
scale=1,
angle=0,
opacity=0.7,
contents={
\includegraphics[width=\textwidth,height=4cm]{example-image-a}}
}
\fi
\fi
\BgMaterial
}
\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}


• Conditioning on \value{section} doesn't seem accurate here. I may be wrong though... – Werner Jul 28 '14 at 18:42
• @Werner why not? – Gonzalo Medina Jul 28 '14 at 18:43
• Could you test it with a case where the section is started on page X, but it pushed to the top of page X+1 and see whether the conditioning still works? In your pictured example, progressively add some text before the start of Section 2 and see what happens... – Werner Jul 28 '14 at 18:44
• Ah, I see what you mean now. The ideal situation is to add/suppress the background material aftera \clearpage (for chapters, for example): in other cases, some manual adjustments might be needed. – Gonzalo Medina Jul 28 '14 at 18:47
• That's exactly my point that this is easier for \chapters, but may require some tweaking for sections. – Werner Jul 28 '14 at 18:48

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
\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}{%
[\placeimage{2}{example-image-b}]% even-centre
{\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:

\documentclass[twoside]{article}
\usepackage{graphicx,refcount,multido}
\usepackage{eso-pic,titleps}
\usepackage{lipsum}
\AtEndDocument{%
\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
\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}{%
[\placeimage{2}{example-image-b}]% even-centre
{\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.

• With the if it would also be possible to eclude pages where specific other tags are located on? like for example an includegraphics is on? – Thomas Jul 29 '14 at 4:44
• @ThomasE.: Perhaps the addition to my answer will address your new question. – Werner Jul 29 '14 at 5:56