# Add a vertical bar to specific pages using TikZ

I use a template that creates a document with two pages. The first page has a vertical bar and the second page has a horizontal bar. I would like to extend this template in order to add an arbitrary number of pages with a vertical bar after the first page.

I came up with the following approach using atbegshi:

\documentclass[a4paper]{article}

\usepackage{atbegshi,tikz,lipsum}
\pagestyle{empty}

\AtBeginShipout{\ifnum\value{page}>0%
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\fi}

\begin{document}

% Dummy code for first template page with vertical bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template page with vertical bar}
\end{center}
\vspace*{\fill}
%\clearpage

% Additional pages with sidebar and dummy text
\begin{center}
\end{center}
\lipsum[1-20]
\restoregeometry
%\clearpage

% Dummy code for last template page with horizontal bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(\paperwidth,-6.0cm);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template pages with horizontal bar}
\end{center}
\vspace*{\fill}

\end{document}


The original template also uses TikZ for creating the bars. There are two problems with my approach. Due to the simple condition \ifnum\value{page}>0, the vertical bar is also applied to the page with horizontal bar. Also, an empty page is added to the end of the document.

My preferred solution would be a switch to turn the vertical bar on and off similar to \newgeometry{...} and \restoregeometry. This could be used to define a new environment. A simple boolean does not seem to work.

Is there a way to implement such a switch or a better approach to this problem?

• Why do you say that a Boolean doesn't work? Did you try using one? – user121799 Apr 21 at 22:25
• Yes, I tested a boolean and it did not work for me. I just tested it again. Somehow it seems to work this time. I will test it a bit more and then post the code if there is no other problem... – JotWe Apr 21 at 22:49
• Yes, that's what I thought. ;-) – user121799 Apr 22 at 1:08

Contrary to my original statement, a boolean can be used to solve this problem. Here is the code that I use now.

\documentclass[a4paper]{article}

\usepackage{atbegshi,tikz,lipsum,ifthen}
\pagestyle{empty}

\newboolean{drawSidebar}
\setboolean{drawSidebar}{false}
\AtBeginShipout{\ifthenelse{\boolean{drawSidebar}}{
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
}{}}

\begin{document}

% Dummy code for template page with vertical bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(5.0cm,-\paperheight);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template page with vertical bar}
\end{center}
\vspace*{\fill}
%\clearpage

% Additional pages with sidebar and dummy text
\setboolean{drawSidebar}{true}
\begin{center}
\end{center}
\lipsum[1-20]
\setboolean{drawSidebar}{false}
\restoregeometry
%\clearpage

% Dummy code for template page with horizontal bar
\begin{tikzpicture}[remember picture,overlay]%
\fill[gray] (current page.north west) rectangle %
++(\paperwidth,-6.0cm);%
\end{tikzpicture}%
\vspace*{\fill}
\begin{center}
\LARGE{Template pages with horizontal bar}
\end{center}
\vspace*{\fill}

\end{document}