章节标记和附录

章节标记和附录

我正在使用 documentclass 创建文档book。在此文档中,我使用 @TomBombadil 的回答添加了章节标记如何给同一章节的每一页都做标记?

奇怪的是,虽然代码在章节中运行良好,但在添加附录时,由于一个奇怪且不清楚的错误(至少对我来说),我无法生成文档

包 PGF 数学错误:未知函数“A”(在‘-A/20*29.69974’中)。

我的代码是

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

当我删除它时,\appendix一切都正常。你知道为什么会发生这种情况以及如何修复它吗?

答案1

\appendix命令将 设置\value{chapter}0并使其\Alph。因此您会收到错误。不要使用计数器chapter,而要使用另一个计数器。在这里,我定义了counter并在每章之后使用 对其进行了升级\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}

在此处输入图片描述

相关内容