I am creating a document, using book documentclass. In this document I have added chapter marks using @TomBombadil 's answer on How can one put a marker to every page in a chapter?.
The weird thing is that although the code works fine in chapters, when adding an appendix, I cannot produce a document due to a weird and unclear, at least to me, error
Package PGF Math Error: Unknown function `A' (in '-A/20*29.69974').
My code is
\documentclass[11pt,a4paper]{book}
\usepackage{lipsum}
\let\MakeUppercase\relax
\usepackage{etex}%Makes able the use of many packages
\usepackage[toc,page]{appendix}
% \input{Preamble/tikz}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{xifthen}
\usepackage{totcount}
\regtotcounter{chapter}
\backgroundsetup%
{ contents={%
\begin{tikzpicture}[overlay]
\pgfmathtruncatemacro{\mytotalchapters}{\totvalue{chapter} > 0 ? \totvalue{chapter} : 20}
\pgfmathsetmacro{\mypaperheight}{\paperheight/28.453}
\pgfmathsetmacro{\mytop}{-(\thechapter-1)/\mytotalchapters*\mypaperheight}
\pgfmathsetmacro{\mybottom}{-\thechapter/\mytotalchapters*\mypaperheight}
\ifcase\thechapter
\xdef\mycolor{white}
\or \xdef\mycolor{red}
\or \xdef\mycolor{orange}
\or \xdef\mycolor{yellow}
\or \xdef\mycolor{green}
\or \xdef\mycolor{blue}
\or \xdef\mycolor{violet}
% \or \xdef\mycolor{magenta}
% \or \xdef\mycolor{cyan}
\else \xdef\mycolor{black}
\fi
\ifthenelse{\isodd{\value{page}}}
{\fill[\mycolor] ($(current page.north east)+(0,\mytop)$) rectangle ($(current page.north east)+(-0.5,\mybottom)$);}
{\fill[\mycolor] ($(current page.north west)+(0,\mytop)$) rectangle ($(current page.north west)+(0.5,\mybottom)$);}
\end{tikzpicture}
},
scale=1,
angle=0
}
\begin{document}
\chapter{First chapter}
\lipsum[1]
\appendix
\chapter{Appendix Chapter}
\lipsum[1]
\end{document}
When I remove \appendix everything works fine. Any idea on why is this hapening and how can it be fixed?

\thechapteris having a valueAwhen theappendixstarts. – Sep 21 '14 at 08:33\thechaptergiving back the number. Use\value{chapter}or\number\value{chapter}instead! – Sep 21 '14 at 08:35\thechapterwith either\value{chapter}or\number\value{chapter}there's no error indeed, but I only get chapter marks for the appendix and not the chapters. – Thanos Sep 21 '14 at 08:42TikZstuff. – Sep 21 '14 at 08:46\appendixit resets\value{Chapter}to 1. This Causes\totvalue{chapter}meaningless. Is this what you want? – Symbol 1 Sep 21 '14 at 08:59\appendixhas\value{chapter}evaluated as0. Maybe creating another counter is a better way. – Symbol 1 Sep 21 '14 at 09:04