TransWikia.com

Show background photo only on non-empty page

TeX - LaTeX Asked on June 24, 2021

I’m using book class, in which I redefined the cleardoublepage to show "This page is intetionally left blank" when the page is empty.

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

I also set a background picture using AddToHook{shipout/background} (here I defined a conditional to decider whether to show the picture). Now I wish the background image does not shown on the empty pages. Can I do this by further modifying the definition of cleardoublepage?

Below is a MWE.

%! TEX program = xelatex
documentclass{book}

usepackage{tikz}
usetikzlibrary{calc}

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

newififShowBackgroundShowBackgroundfalse
AddToHook{shipout/background}
{%
ifShowBackground%
begin{tikzpicture}[remember picture,overlay]
    node at (current page.center)
    {Hugecolor{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}

2 Answers

My instinct would be to build the background image into the page style rather than through the shipout hook. This gives more finally-grained control over where it appears.

Correct answer by Don Hosek on June 24, 2021

Based on @DonHosek 's hint, the solution is to add the background picture in the pagestyle setting (here I use fancyhdr) but not globally in AddToHook{shipout/background}.

%! TEX program = xelatex
documentclass{book}

usepackage{tikz}
usetikzlibrary{calc}

makeatletter
renewcommand{cleardoublepage}{relax
  clearpage
  if@twoside ifoddc@pagerelaxelse
  thispagestyle{empty}%
  tikz[remember picture, overlay] node at (current page.center)
    {large This page is intetionally left blank};newpagefifi}
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)
        {Hugecolor{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}

Answered by Jinwen on June 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP