3

I am trying to use @Gonzalo-Medina code (Show current chapter number on each page margin) to display chapter number on each pages from a book document class. I used a counter to overcome the issue with appendix chapters. Unfortunately, I still have issues with my code : The boxes are two big and not displayed correctly, or the last chapter of the main part of the document is not displayed correctt (no number).

The question is how to set the code properly to work with appendices and that could display all chapters.

Here is my MWE that does not fit with appendices

\PassOptionsToPackage{x11names}{xcolor}
\documentclass[]{book}
\usepackage[contents={},opacity=1,scale=1,color=white]{background}
\usepackage[a4paper]{geometry}% just for the example
\usepackage{tikzpagenodes}
\usepackage{totcount}
\usepackage{lipsum}% just to generate text for the example
\usepackage{fancyhdr}
\usepackage{shorttoc}
\usetikzlibrary{calc}

\newif\ifMaterial

\newlength\LabelSize
\setlength\LabelSize{3.5cm}

% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}

\AtBeginDocument{%
\regtotcounter{chapter}
\setlength\LabelSize{\dimexpr\textheight/\totvalue{chapter}\relax}
\ifdim\LabelSize>3.5cm\relax
  \global\setlength\LabelSize{3.5cm}
\fi
}

\newcommand\AddLabels{%
\Materialtrue%
\AddEverypageHook{%
\ifMaterial%
\ifodd\value{page} %
 \backgroundsetup{
  angle=90,
  position={current page.east|-current page text area.north  east},
  vshift=12pt,
  %hshift=-\thechapter*\LabelSize,
  hshift=-\thechapshift*\LabelSize,
  contents={%
  \tikz\node[fill=DodgerBlue4,anchor=west,text width=\LabelSize,
    align=center,text height=15pt,text depth=13pt,font=\large\sffamily] {\thechapter};
  }%
 }
 \else
 \backgroundsetup{
  angle=90,
  position={current page.west|-current page text area.north west},
  vshift=-12pt,
  hshift=-\thechapshift*\LabelSize,
  %hshift=-\thechapter*\LabelSize,
  contents={%
  \tikz\node[fill=DodgerBlue4,anchor=west,text width=\LabelSize,
    align=center,text height=15pt,text depth=13pt,font=\large\sffamily] {\rotatebox{180}{\thechapter}};
  }%
 }
 \fi
 \BgMaterial%
\else\relax\fi}%
\addtocounter{chapshift}{1}%
}

\newcommand\RemoveLabels{\Materialfalse}

\begin{document}

\shorttoc{Sommaire}{0}\pagestyle{fancyplain}


\chapter{Test Chapter One}
\AddLabels
\lipsum[1-2]
\chapter{Test Chapter Two}
\lipsum[1-12]

\appendix
%\RemoveLabels
\chapter{Annexe A}
\lipsum[1-2]


\tableofcontents
\RemoveLabels

\end{document}

the part I am trying to change to make this work with appendices are :

  hshift=-\thechapshift*\LabelSize,
  %hshift=-\thechapter*\LabelSize,

maybe that's not a good way to try... Is this possible? If so, how can I implement it?

JujuW
  • 33
  • 2
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. (Although you seem to be doing pretty well anyway.) – cfr Sep 21 '14 at 21:54
  • Are your labels also supposed to move as the chapters increment? As to the size, what size are they supposed to be? You've set a minimum of 3.5cm. I can't really tell, but that doesn't seem implausible given the output. – cfr Sep 21 '14 at 22:04
  • @cfr Indeed, the labels are supposed to move as the chapter increment. I used 3.5cm as an example only. I only want to find the good way (not to big, not to small) to display the chapter number – JujuW Sep 22 '14 at 18:12
  • So does my solution answer your question or not? I'm not clear whether there are outstanding issues or, if there are, what those issues might be. My answer gets the labels to show up correctly, I think. If the labels are too large, reduce the 3.5cm minimum. Or have I misunderstood? – cfr Sep 22 '14 at 18:29

1 Answers1

4

Perhaps you could use the assoccnt package to manage chaptershift rather than trying to increment it manually. (Otherwise, you need to step it for each chapter - not just when you first execute \AddLabels unless you add that for every chapter.)

\cleardoublepage before \appendix ensures that the chapter number is available when the pages are shipped out. Otherwise, what happens, I think, is that the number is effectively not available when the marks are set.

\PassOptionsToPackage{x11names}{xcolor}
\documentclass[]{book}
\usepackage[contents={},opacity=1,scale=1,color=white]{background}
\usepackage[a4paper]{geometry}% just for the example
\usepackage{tikzpagenodes}
\usepackage{totcount}
\usepackage{lipsum}% just to generate text for the example
\usepackage{fancyhdr}
\usepackage{shorttoc}
\usetikzlibrary{calc}
\usepackage{assoccnt}

\newif\ifMaterial

\newlength\LabelSize
\setlength\LabelSize{3.5cm}

% auxiliary counter
\newcounter{chapshift}
\DeclareAssociatedCounters{chapter}{chapshift}

\AtBeginDocument{%
\regtotcounter{chapter}
\setlength\LabelSize{\dimexpr\textheight/\totvalue{chapter}\relax}
\ifdim\LabelSize>3.5cm\relax
  \global\setlength\LabelSize{3.5cm}
\fi
}

\newcommand\AddLabels{%
\Materialtrue%
\AddEverypageHook{%
\ifMaterial%
\ifodd\value{page} %
 \backgroundsetup{
  angle=90,
  position={current page.east|-current page text area.north  east},
  vshift=12pt,
  %hshift=-\thechapter*\LabelSize,
  hshift=-\thechapshift*\LabelSize,
  contents={%
  \tikz\node[fill=DodgerBlue4,anchor=west,text width=\LabelSize,
    align=center,text height=15pt,text depth=13pt,font=\large\sffamily] {\thechapter};
  }%
 }
 \else
 \backgroundsetup{
  angle=90,
  position={current page.west|-current page text area.north west},
  vshift=-12pt,
  hshift=-\thechapshift*\LabelSize,
  %hshift=-\thechapter*\LabelSize,
  contents={%
  \tikz\node[fill=DodgerBlue4,anchor=west,text width=\LabelSize,
    align=center,text height=15pt,text depth=13pt,font=\large\sffamily] {\rotatebox{180}{\thechapter}};
  }%
 }
 \fi
 \BgMaterial%
\else\relax\fi}%
}

\newcommand\RemoveLabels{\Materialfalse}

\begin{document}

\shorttoc{Sommaire}{0}\pagestyle{fancyplain}


\chapter{Test Chapter One}
\AddLabels
\lipsum[1-2]
\chapter{Test Chapter Two}
\lipsum[1-12]
\cleardoublepage
\appendix
%\RemoveLabels
\chapter{Annexe A}
\lipsum[1-2]


\tableofcontents
\RemoveLabels

\end{document}

Chapter marks for appendices

cfr
  • 198,882
  • As I know somehow the author of assoccnt, I must emphasize that that package is still in an experimental state ;-) –  Sep 21 '14 at 22:26
  • @ChristianHupfer It seems to work, though. Should I delete this? – cfr Sep 21 '14 at 22:26
  • No, keep it -- it works -- there is one caveat with xifthen package and assoccnt, but this is of no importance here! I just got some idea. I should use my powers from the dark side to force the author of assoccnt to provide version 0.5 ;-) –  Sep 21 '14 at 22:28
  • @ChristianHupfer OK. Thanks for the package! I have to admit I've never used it before. I just found it on CTAN. So if you told me it would eat grasshoppers for breakfast, I would be inclined to believe you. – cfr Sep 21 '14 at 22:30
  • I just looked on the code of your solution, I think, there is no issue why it should fail with associated counters! Actually the package was a step - aside -project. –  Sep 21 '14 at 22:34
  • @ChristianHupfer Thank you very much. No grasshoppers need be harmed in the making, then. Good ;). – cfr Sep 21 '14 at 22:54
  • 1
    I don't get the grasshoppers joke -- some special humour? ;-) assoccnt 0.5 was just uploaded to CTAN, it will be available tomorrow, but there are only minor changes in it. –  Sep 21 '14 at 23:03
  • 1
    @ChristianHupfer The introduction of grasshoppers was unmotivated. The follow-up about not harming them may be culturally-specific. That is, it may be a specifically British association. Thinking about it, this is possibly true. My apologies. I have a feeling it comes from a time when every other TV and radio programme assured you at the end that 'no X were harmed in the making of this programme' where X was an animal of one sort or another. In some cases, this seemed to make sense but it got to the point where they'd say it even when there didn't seem to be any Xs involved at all. – cfr Sep 22 '14 at 00:04
  • 1
    No TeX users were harmed by this discussion ;-) –  Sep 22 '14 at 12:49
  • @cfr thanks for the tip with this specific package. Your code is doing the job and my counter is incrementing correcly. – JujuW Sep 22 '14 at 18:30
  • 1
    @cfr: assoccnt 0.5a is available on TeXLive 2014 –  Sep 23 '14 at 10:15