在每个页边距上显示当前章节号并附加附录

在每个页边距上显示当前章节号并附加附录

我正在尝试使用@Gonzalo-Medina 代码(在每个页边距上显示当前章节编号) 显示书籍文档类中每页的章节编号。我使用计数器来解决附录章节的问题。不幸的是,我的代码仍然存在问题:框太大,无法正确显示,或者文档主要部分的最后一章无法正确显示(没有编号)。

问题是如何正确设置代码以与附录一起工作并显示所有章节。

这是我的 MWE,与附录不符

\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}

为了使这个与附录一起工作,我试图改变的部分是:

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

也许这不是一个好的尝试方法...这可能吗?如果可以,我该如何实现?

答案1

也许你可以使用助理包进行管理,chaptershift而不是尝试手动增加它。(否则,您需要为每个章节添加它 - 而不仅仅是在您第一次执行时,\AddLabels除非您为每个章节添加它。)

\cleardoublepagebefore\appendix确保在页面发送时章节号可用。否则,我认为,在设置标记时,该编号实际上不可用。

\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}

附录的章节标记

相关内容