Tikz draw on specific pages (not all pages)

diagramspage-numberingtikz-pgf

I am writing a 10 page report in single column IEEE format with the IEEEtran class. Its a continuous report without any headers & footers or chapters. I am drawing a sidebar on the left side of the every page like this.

\usepackage[all]{background}
\usepackage{tikz}

\newcommand{\MyTikzLogo}{
    \begin{tikzpicture}[remember picture,overlay,draw=black,ultra thick]
        \draw  [fill=red] (0,-30) rectangle (1cm,10cm);
    \end{tikzpicture}
}

\SetBgContents{\MyTikzLogo}
\SetBgPosition{current page.north west}
\SetBgOpacity{1.0}
\SetBgAngle{0.0}
\SetBgScale{1.0}

This works. However, I am inserting two pages in the beginning of the document and two pages at the end of the document from another pdf like this:

\begin{document}
\includepdf[pages=-]{beginning.pdf}
⋮
\includepdf[pages=-]{ending.pdf}
\end{document}

I don't want the sidebar or page numbers to appear on the inserted pages. Also, after inserting the pages, the page numbers appear on them too and the page number on third page is 3 after inserting two pages at the beginning.

How to:

  • Prevent tikz from drawing on the inserted pages? meaning exclude first two and last two inserted pages.
  • How to prevent pages numbers to appear on the inserted pages? (after inserting 2 pages at the beginning, 3rd page number should start from 1 not 3, and the inserted pages both at the start and end should not show page numbers)

Any help will be highly appreciated.

Best Answer

This uses \AddToHook{shipout/background}{...} (standard LaTeX) instead of the background package. It also replaces [remember picture] and (current page.north west) with \put(0,0) {...} so that it works on the first run.

You will need two page counters. One to be printed as \thepage and another to actually keep track. Hyperref provides its own page count (\Hy@pagecounter), which would have made this easier. You might want to check using \pageref.

\documentclass{IEEEtran}
\usepackage{tikz}
\usepackage{lipsum}% MWE only

\newcounter{mypage}
\setcounter{mypage}{1}
\renewcommand{\thepage}{\arabic{mypage}}

\newcommand{\MyTikzLogo}{%
      \begin{tikzpicture}[baseline={(0,0)},draw=black,ultra thick]
            \draw  [fill=red] (0,-\paperheight) rectangle (1cm,0);
      \end{tikzpicture}%
}

\AddToHook{shipout/background}{\ifnum \value{page}>2
  \ifnum\value{page}<5
    \put(0,0){\MyTikzLogo}% north west corner
  \fi
\fi}

\AddToHook{shipout/after}{\ifnum \value{page}>2
  \ifnum\value{page}<5
    \stepcounter{mypage}% hopefully after \protected@write expands \thepage
  \fi
\fi}
  
\begin{document}
\lipsum[1-60]
\end{document}