如何不为空白页添加背景

如何不为空白页添加背景

我正在使用 background 包为每个页面添加背景。我希望奇数页和偶数页有不同的背景,这就是我使用的原因\AddEverypageHook。但是,AddEverypagehook适用于所有页面,包括空白页。我希望它仅在页面不为空时添加背景。因此,使用的空白页\cleardoublepage不应该有背景。我加载了 emptypage 包以从这些页面中删除页眉和页脚,但我还想删除背景。也许它应该检查是否为pagestyleempty这是我到目前为止的代码:

\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

这将添加\ifempty由 emptypage (已修改) 设置的注释命令,\oddpagebg并且\evenpagebg可以随时更改。

还要注意,我不必添加 tikz 包;后台包会自动执行此操作(这就是我认为后台包是多余的原因)。

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

相关内容