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?

9
  • 1
    That is because the \thechapter is having a value A when the appendix starts.
    – user11232
    Sep 21, 2014 at 8: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!
    – user31729
    Sep 21, 2014 at 8: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, 2014 at 8:42
  • @Thanos: I'll take a look, although I have no idea abut TikZ stuff.
    – user31729
    Sep 21, 2014 at 8:46
  • @ChristianHupfer : Thank you very much in advance!
    – Thanos
    Sep 21, 2014 at 8:52

1 Answer 1

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

1
  • Thank you very much for your answer! It is really working! Thank's again!
    – Thanos
    Sep 21, 2014 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.