Alternating Background Image for different chapters in Memoir

I have used a background image throughout my book. The code I used evolved from this question and answer (thanks to @GonzaloMedina): "Margin vertical rule on outside margin of twosided memoir"

My question is I would like to figure out how to shift the colored background box to a new location down the edge of the page for each chapter (7-10 chapters). In addition I want to place the chapter # within the colored box. Is this possible?

    \documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90,hshift=-2.5cm]{background}
\usepackage{lipsum}

\newcommand*\VerBar[2]{%
{\color{#1}\rule{#2}{65pt}}}

\newif\ifBgUse

\ifBgUse%
\ifodd\value{page}
\backgroundsetup{position={current page.north east},vshift=32.5pt,%
contents={\VerBar{blue}{5cm}}}%
\else
\backgroundsetup{position={current page.north west},vshift=-32.5pt,%
contents={\VerBar{blue}{5cm}}}%
\fi%
\BgMaterial%
\fi}

\begin{document}

\chapter{Test chapter one}
\lipsum[1-9]\clearpage

\BgUsetrue% activate background material
\chapter{Test chapter two}
\lipsum[1-9]\clearpage

\BgUsefalse% deactivate background material
\chapter{Test chapter two}
\lipsum[1-9]

\end{document}

• But what's the point of using the code of my answer below in your update? The code does what you required (at least on my system), once book is changed to memoir. Is there something not working for you? – Gonzalo Medina Sep 9 '13 at 19:02
• Still one problem remains, I have found. It seems that the issue I was having before is there is a conflict when using the \appendix command in memoir. I want the appendix to be numbered by letters, but your answer creates a conflict with this. I have 4 appendix chapters. The errors seem to be PGF errors. SAMPLE ERROR: Package PGF Math Error: Unknown function A' (in '-A*\LabelSize '). [\chapter] – Calhistorian Sep 9 '13 at 20:35
• Ah, yes. Sorry; that was my faul for using \thechapter and not \value{chapter} in some parts of the code. Please see my updated answer for the easy fix: notice also that I in order to cope with the fact that \appendix resets the chapter counter, I defined an auxiliary counter to maintain the appropriate shifting in appendices. – Gonzalo Medina Sep 9 '13 at 22:37

You can simply introduce an appropriate horizontal shifting (since the material has previously been rotated), based, for example, on the chapter counter:

\documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90]{background}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{etoolbox}

\newcounter{mychap}
\pretocmd{\chapter}{\stepcounter{mychap}}{}{}

\newcommand*\VerBar[2]{%
{\color{#1}\rule{#2}{65pt}}%
\llap{\rotatebox[origin=c]{-90}{%
\hspace*{-2.5cm}\raisebox{-2.5cm}[0pt][0pt]{%
\Huge\color{white}\bfseries\thechapter}%
}%
}%
}

\newif\ifBgUse

\newlength\LabelSize
\setlength\LabelSize{2.5cm}

\ifBgUse%
\ifodd\value{page}
\backgroundsetup{
position={current page.north west},
vshift=-32.5pt,
hshift=-\value{mychap}*\LabelSize,
contents={\VerBar{blue}{5cm}}%
}%
\else
\backgroundsetup{
position={current page.north east},
vshift=32.5pt,
hshift=-\value{mychap}*\LabelSize,
contents={\VerBar{blue}{5cm}}%
}%
\fi%
\BgMaterial%
\fi}

\begin{document}

\chapter{Test chapter one}
\lipsum[1-9]\clearpage

\BgUsetrue% activate background material
\chapter{Test chapter two}
\lipsum[1-9]

\chapter{Test chapter three}
\lipsum[1-9]

\chapter{Test chapter four}
\lipsum[1-9]\clearpage

\appendix

\chapter{Test appendix one}
\lipsum[1-9]\clearpage

\BgUsefalse% deactivate background material
\chapter{Test appendix two}
\lipsum[1-9]

\end{document}


Here is how I would do:

\documentclass[a4paper,openany]{book}
\usepackage[contents={},opacity=0.5,scale=1,angle=90]{background}
\usepackage{lipsum}

\usepackage{tikz}
\usepackage{ifthen}

\newcommand*\VerBar[2]{%
\begin{tikzpicture}
\fill[#1] (0,0)--(-5,0)--(-5,2)--(0,2)--cycle;
\draw[white] (-2.5,1)node{\rotatebox{-90}{\Huge \thechapter}};
\end{tikzpicture}
}

\newlength{\shift}
\setlength{\shift}{2.5cm}

\newif\ifBgUse

\ifBgUse%
\ifodd\value{page}
\backgroundsetup{position={current page.north west},vshift=-1cm,%
contents={\VerBar{blue}{5cm}}}%
\else
\backgroundsetup{position={current page.north east},vshift=1cm,%
contents={\VerBar{blue}{5cm}}}%
\fi%
\BgMaterial%
\fi}

\BgUsefalse

\newcommand{\mychapter}[1]{\ifthenelse{\equal{#1}{*}}%
{\mycommandStar}%
{\mycommandNoStar{#1}}%
}
\newcommand{\mycommandStar}[1]{\BgUsetrue
\backgroundsetup{hshift=\shift}
\chapter{#1}
\newcommand{\mycommandNoStar}[1]{\BgUsefalse%
\chapter{#1}}

\begin{document}

\mychapter{Test chapter one}
\lipsum[1-9]\clearpage

\mychapter*{Test chapter two}
\lipsum[1-9]\clearpage

\mychapter*{Test chapter three}
\lipsum[1-9]

\end{document}


For having the number of the chapter in the box, I use tikz instead of \rule.

For having a sweep of the box according to the chapter I defined a new length (shift) which is decreased (to go down) by 5 cm each time a new chapter is started and the BgUse is true. For that I just defined a new command \mychapter working exactly as \chapter (and setting BgUse to false) and the starred version of this command \mychapter* which will turn BgUse` true and add the sweeping boxes on the side of the page. It also call the chapter command.

Here is the output for the chapters 2 and 3 (first page):