1

I am using the background package to add a background to each page. I want a different background for odd and even pages, which is why I use \AddEverypageHook. However, AddEverypagehook works on all pages, including empty pages. I want it to only add a background if the page is not empty. Therefore, empty pages from using \cleardoublepage should not have a background. I load the emptypage package to remove headers and footers from those pages, but I also want to remove backgrounds. Maybe it should check if the pagestyle is empty? Here is my code so far:

\documentclass{book}

\usepackage{stix2, lipsum, ifthen, emptypage}
\usepackage[tracking]{microtype}
\usepackage[
    paperwidth = 6 in,
    paperheight = 9 in,
    margin = 0.75 in,
    bindingoffset = 0.125 in
]{geometry}
\usepackage[
    contents={},
    opacity=1,
    scale=1.5,
    color=blue!90
]{background}

\AddEverypageHook{
    \ifthenelse{\isodd{\value{page}}}
        {\backgroundsetup{contents = {The background package}}}
        {\backgroundsetup{contents = {Version 2.1}}}
    \BgMaterial
}

\begin{document}

\tableofcontents

\chapter{My chapter}

\lipsum[1-5]

\end{document}

1 Answer 1

0

This adds \ifempty which is set by emptypage (modified). Note commands \oddpagebg and \evenpagebg can be changed at any time.

Note also that I did not have to add the tikz package; the background package does it automatically (which is why I consider the background package to be redundant).

\documentclass{book}

\usepackage{stix2, lipsum, ifthen, emptypage}
\usepackage[tracking]{microtype}
\usepackage[
    paperwidth = 6 in,
    paperheight = 9 in,
    margin = 0.75 in,
    bindingoffset = 0.125 in
]{geometry}
\usepackage[
    contents={},
    opacity=1,
    scale=1.5,
    color=blue!90
]{background}

\newif{\ifempty}

\newcommand{\oddpagebg}{\backgroundsetup{contents = {The background package}}}
\newcommand{\evenpagebg}{\backgroundsetup{contents = {Version 2.1}}}

\AddEverypageHook{
  \ifempty
    \global\emptyfalse
  \else
    \ifthenelse{\isodd{\value{page}}}
        {\oddpagebg}
        {\evenpagebg}
  \fi
    \BgMaterial
}

\usepackage{etoolbox}% edit emptypage
\makeatletter
\patchcmd{\emptypage@emptypage}{\thispagestyle{empty}}%
  {\thispagestyle{empty}\emptytrue}{}{FAILED}
\makeatother


\begin{document}

\tableofcontents

\chapter{My chapter}

\lipsum[1-10]

\newpage

\renewcommand{\oddpagebg}{\backgroundsetup{contents = {The background package}}%
  \tikz[remember picture,overlay]{\node[opacity=0.2] at (current page.center)
    {\includegraphics[height=\paperheight, width=\paperwidth]{example-image-a}};}}
    
\renewcommand{\evenpagebg}{\backgroundsetup{contents = {Version 2.1}}%
  \tikz[remember picture,overlay]{\node[opacity=0.2] at (current page.center)
    {\includegraphics[height=\paperheight, width=\paperwidth]{example-image-b}};}}

\chapter{New chapter}

\lipsum[1-10]

\end{document}
7
  • Thanks. How can I make this work with multiple page hooks? For example, let's say I want to add another watermark with another \AddEverypageHook. The first watermark will be hidden on empty pages but the second one will still show.
    – Amarakon
    Commented Dec 29, 2022 at 3:31
  • You would have to remove the first hook and create a combined hook. IIRC that is possible using an optional argument. I don't use everypage anymore myself (see tex.stackexchange.com/questions/573329/…). Commented Dec 29, 2022 at 15:14
  • Can you please show me a working example of that? Because I don't understand what you mean by combined hook with an optional argument.
    – Amarakon
    Commented Dec 29, 2022 at 18:21
  • Alas, there is no remove page hook command after all. \AddToHook has an optional label which can be used with \RemoveFromHook. OTOH, you could just implement the background using a macro. Commented Dec 29, 2022 at 21:19
  • Sorry, I do not know what code to use to implement the background using a macro. Do you mind updating your answer with an example of putting two watermarks, but making both of them not appear on empty pages? Thanks.
    – Amarakon
    Commented Dec 29, 2022 at 21:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .