仅在非空白页面上显示背景照片

仅在非空白页面上显示背景照片

我正在使用book类,在其中我重新定义了\cleardoublepage当页面为空时显示“此页面故意留空”。

\makeatletter
\renewcommand{\cleardoublepage}{\relax
  \clearpage
  \if@twoside \ifodd\c@page\relax\else
  \thispagestyle{empty}%
  \tikz[remember picture, overlay] \node at (current page.center)
    {\large This page is intetionally left blank};\newpage\fi\fi}
\makeatother

我还使用 设置了背景图片\AddToHook{shipout/background}(这里我定义了一个条件来决定是否显示图片)。现在我希望背景图片不显示在空白页上。我可以通过进一步修改 的定义来实现这一点吗\cleardoublepage

下面是 MWE。

%! TEX program = xelatex
\documentclass{book}

\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\renewcommand{\cleardoublepage}{\relax
  \clearpage
  \if@twoside \ifodd\c@page\relax\else
  \thispagestyle{empty}%
  \tikz[remember picture, overlay] \node at (current page.center)
    {\large This page is intetionally left blank};\newpage\fi\fi}
\makeatother

\newif\ifShowBackground\ShowBackgroundfalse
\AddToHook{shipout/background}
{%
\ifShowBackground%
\begin{tikzpicture}[remember picture,overlay]
    \node at (current page.center)
    {\Huge\color{red} BACKGROUND};
\end{tikzpicture}%
\fi%
}

\usepackage{blindtext}

\begin{document}

\begin{titlepage}
    \begin{tikzpicture}[remember picture,overlay]
        \node at (current page.center)
        {\Huge TITLEPAGE};
    \end{tikzpicture}%
\end{titlepage}

\ShowBackgroundtrue

\frontmatter
Some preface 

\mainmatter
Main text \blindtext[10]

\backmatter

\end{document}

答案1

我的直觉是将背景图像构建到页面样式中,而不是通过 shipout 钩子。这样可以更精确地控制其显示位置。

答案2

根据@DonHosek 的提示,解决方案是在页面样式设置中添加背景图片(这里我使用fancyhdr),而不是在全局中添加\AddToHook{shipout/background}

%! TEX program = xelatex
\documentclass{book}

\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\renewcommand{\cleardoublepage}{\relax
  \clearpage
  \if@twoside \ifodd\c@page\relax\else
  \thispagestyle{empty}%
  \tikz[remember picture, overlay] \node at (current page.center)
    {\large This page is intetionally left blank};\newpage\fi\fi}
\makeatother

%%<=======================
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\rightmark}
\fancyhead[C]{% Background picture added here
    \begin{tikzpicture}[remember picture,overlay]
        \node at (current page.center)
        {\Huge\color{red} BACKGROUND};
    \end{tikzpicture}%
}
\pagestyle{fancy}
%%<=======================

\usepackage{blindtext}

\begin{document}

\begin{titlepage}
    \begin{tikzpicture}[remember picture,overlay]
        \node at (current page.center)
        {\Huge TITLEPAGE};
    \end{tikzpicture}%
\end{titlepage}

\frontmatter
Some preface 

\mainmatter
Main text \blindtext[10]

\backmatter

\end{document}

相关内容