[Tex/LaTex] How to clear the background of just the current page

backgroundseso-pic

eso-pic provides \AddToShipoutPictureBG to add content to the background at every page and \AddToShipoutPictureBG* to add it only to the current page. \ClearShipoutPicture clears all the content. But there is no \ClearShipoutPicture* to clear the content only for the current page.

I would like to show some content in every page but chapter pages. Using \AddToShipoutPictureBG{} when the chapter is created doesn't work, since the previous content is not cleared, it is just added. If I use \ClearShipoutPicture I need to know when the page is changed to add the content again. How should I do this?

I also tried background, but \NoBgThispage is not working. I could use

\backgroundsetup{contents={
 \begin{tikzpicture}
  ...
 \end{tikzpicture}
,angle=0
}}

instead of \AddToShipoutPictureBG{...} and \backgroundsetup{contents={}} instead of \ClearShipoutPicture. The results is pretty much the same.


MWE:

\documentclass[a4paper,twoside,openright]{report}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
\lipsum[1-3]\section{First section ch1}\lipsum[4-10]
\end{filecontents}
\begin{filecontents}{ch2.tex}
\lipsum[11-14]\section{First section ch2}\lipsum[15-25]
\end{filecontents}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{eso-pic}
\usepackage{tikz,bm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,positioning}

\regtotcounter{sidecnt} \newcounter{sidecnt}

\colorlet{sidecolor}{red!75}
\colorlet{sidelabelcolor}{blue}

\def\vsidemargin{3.5cm}

\AddToShipoutPictureBG{%
\begin{tikzpicture}[overlay]
 \pgfmathsetmacro{\pheight}{(\paperheight-2*\vsidemargin)/28.453}
 \pgfmathsetmacro{\myt}{-(\vsidemargin/28.453)-(\thesidecnt-1)/\totvalue{sidecnt}*\pheight}
 \pgfmathsetmacro{\myb}{-(\vsidemargin/28.453)-\thesidecnt/\totvalue{sidecnt}*\pheight}
 \ifthenelse{\isodd{\value{page}}}{\def\corner{east}\def\sign{-}}{\def\corner{west}\def\sign{+}}
 \fill[sidecolor] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$);
 \node[font=\normalfont\sffamily\bfseries] at ($(current page.north \corner)+(\sign0.375,\myt-0.5)$) {\color{sidelabelcolor}\thechapter};
\end{tikzpicture}
}

\newcommand\importchapter[2]{
 \chapter{#1}
 \stepcounter{sidecnt}
 %\ClearShipoutPicture* alike
 \input{#2}
}

\begin{document}

\importchapter{First chapter}{ch1.tex}
\importchapter{Second chapter}{ch2.tex}

\end{document}

I don't want the background to be shown in pages 1 and 5.

Best Answer

You can cheat by adding a white rectangle the size of the page to the background on chapter pages to cover over the background picture rather than actually clearing it. Obviously, if you are printing on any colour of paper other than white, you'd need to match the colour. But hopefully that is not so and this will work:

\documentclass[a4paper,twoside,openright]{report}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
  \lipsum[1-3]\section{First section ch1}\lipsum[4-10]
\end{filecontents}
\begin{filecontents}{ch2.tex}
  \lipsum[11-14]\section{First section ch2}\lipsum[15-25]
\end{filecontents}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{eso-pic}
\usepackage{tikz,bm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,positioning}

\regtotcounter{sidecnt} \newcounter{sidecnt}

\colorlet{sidecolor}{red!75}
\colorlet{sidelabelcolor}{blue}

\def\vsidemargin{3.5cm}

\AddToShipoutPictureBG{%
  \begin{tikzpicture}[overlay]
    \pgfmathsetmacro{\pheight}{(\paperheight-2*\vsidemargin)/28.453}
    \pgfmathsetmacro{\myt}{-(\vsidemargin/28.453)-(\thesidecnt-1)/\totvalue{sidecnt}*\pheight}
    \pgfmathsetmacro{\myb}{-(\vsidemargin/28.453)-\thesidecnt/\totvalue{sidecnt}*\pheight}
    \ifthenelse{\isodd{\value{page}}}{\def\corner{east}\def\sign{-}}{\def\corner{west}\def\sign{+}}
    \fill[sidecolor] ($(current page.north \corner)+(0,\myt)$) rectangle ($(current page.north \corner)+(\sign0.75,\myb)$);
    \node[font=\normalfont\sffamily\bfseries] at ($(current page.north \corner)+(\sign0.375,\myt-0.5)$) {\color{sidelabelcolor}\thechapter};
  \end{tikzpicture}}

\newcommand\importchapter[2]{
  \chapter{#1}
  \stepcounter{sidecnt}
  \AddToShipoutPictureBG*{%
    \begin{tikzpicture}[overlay,color=white]
      \fill (current page.north east) rectangle (current page.south west);
    \end{tikzpicture}}
  \input{#2}}

\begin{document}

  \importchapter{First chapter}{ch1.tex}
  \importchapter{Second chapter}{ch2.tex}

\end{document}

Cheat by covering over the background on some pages