4

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?

Thanos
  • 12,446
  • 1
    That is because the \thechapter is having a value A when the appendix starts. –  Sep 21 '14 at 08:33
  • Harish Kumar's comment seems to be the answer. It's sometimes not good to rely on \thechapter giving back the number. Use \value{chapter} or \number\value{chapter} instead! –  Sep 21 '14 at 08:35
  • @ChristianHupfer : Thank you very much for your comment! The thing is that if I replace \thechapter with 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:42
  • @Thanos: I'll take a look, although I have no idea abut TikZ stuff. –  Sep 21 '14 at 08:46
  • @ChristianHupfer : Thank you very much in advance! – Thanos Sep 21 '14 at 08:52
  • Once LaTeX enters \appendix it resets \value{Chapter} to 1. This Causes \totvalue{chapter} meaningless. Is this what you want? – Symbol 1 Sep 21 '14 at 08:59
  • @Symbol1: I just figured that out too ;-) –  Sep 21 '14 at 09:00
  • And somehow the page just before \appendix has \value{chapter} evaluated as 0. Maybe creating another counter is a better way. – Symbol 1 Sep 21 '14 at 09:04
  • I will try using my assoccnt package –  Sep 21 '14 at 09:07

1 Answers1

4

The \appendix command sets the \value{chapter} to 0 and makes it \Alph. Hence you get the error. Instead of using the chapter counter, use another counter. Here I have defined a counter and stepped it up after each chapter using \xpatchcmd.

\documentclass[11pt,a4paper]{book}
\usepackage{lipsum}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\chapter}{\thispagestyle{plain}}
                    {\thispagestyle{plain}\stepcounter{counter}}
                    {}{}
\makeatother


\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{counter}
\newcounter{counter}

\backgroundsetup%
{   contents={%
        \begin{tikzpicture}[overlay]
            \pgfmathtruncatemacro{\mytotalcounter}{\totvalue{counter} > 0 ? \totvalue{counter} : 20}
            \pgfmathsetmacro{\mypaperheight}{\paperheight/28.453}
            \pgfmathsetmacro{\mytop}{-(\thecounter-1)/\mytotalcounter*\mypaperheight}
            \pgfmathsetmacro{\mybottom}{-\thecounter/\mytotalcounter*\mypaperheight}
            \ifcase\thecounter
                \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]
 \chapter{second chapter}
 \lipsum[1]
 \appendix
 \chapter{Appendix Chapter}
 \lipsum[1]
 %\showthe\value{counter}
\end{document}

enter image description here