如何给同一章节的每一页都做标记?

如何给同一章节的每一页都做标记?

我正在尝试为正在设计的模板创建某种制表符。我希望标题页具有章节的颜色,然后章节内的所有页面都具有如下所示的标记。标题页应使用页面高度并根据章节总数进行拆分。显示的矩形应遵循奇偶页标准,以便显示正确。这是一种 MWE:

\documentclass[12pt,twoside,letterpaper]{report}

\usepackage[text={4.95in,7.5in},centering,bottom=1.5in,showframe]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usetikzlibrary{calc}

%-------------------------------------------------------------------------
%                        Chapter Page Header/Footer 
%-------------------------------------------------------------------------
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyhead{} % clear all header fields
\fancyfoot{} % clear all footer fields
\fancyhead{
\ifodd\thepage
    \tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\else
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north west) rectangle ($(current page.west)+(0.5,9)$);}
\fi
}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
%-------------------------------------------------------------------------
%                       Header/Footer Page Styles
%-------------------------------------------------------------------------
\fancyhead{} % clear all header fields
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\fancyhead{
\ifodd\thepage
    \tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\else
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north west) rectangle ($(current page.west)+(0.5,9)$);}
\fi
}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\pagestyle{fancy}

\begin{document}

\title{Your Paper
\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\tikz[remember picture, overlay]{\draw[fill=magenta] ($(current page.north east)+(0,-5)$) rectangle ($(current page.east)+(-0.5,4)$);}}
\author{You}

\maketitle
\newpage
\chapter{Test 1}
%\tikz[remember picture, overlay]{\draw[fill=cyan] (current page.north east) rectangle ($(current page.east)+(-0.5,9)$);}
\newpage
%\tikz[remember picture, overlay]{\draw[fill=magenta] ($(current page.north east)+(0,-5)$) rectangle ($(current page.east)+(-0.5,4)$);}
\chapter{Test 2}
\lipsum
\chapter{Test 3}
\lipsum
\end{document}

在此处输入图片描述 在此处输入图片描述

请注意,第 2 章应使用洋红色,并将标记向下移动适当的量。我曾考虑过使用一个foreach语句,但我不知道如何测试您是仍在第 1 章还是第 2 章等等。我认为棘手的部分将是根据章节数量设置颜色。因此,对于章节标记,最好使用默认的 20 种颜色,并按出现的顺序使用(从而允许用户决定使用哪种颜色以及用于哪一章)。

答案1

这是第一个版本:

代码

\documentclass{book}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{background}
\usepackage{xifthen}
\usepackage{lipsum}

\backgroundsetup%
{   contents={%
        \begin{tikzpicture}[overlay]
            \pgfmathsetmacro{\mytop}{-(\thechapter-1)*2.5}
            \pgfmathsetmacro{\mybottom}{-\thechapter*2.5}
            \ifcase\thechapter
                \xdef\mycolor{black}
                \or \xdef\mycolor{red}
                \or \xdef\mycolor{orange}
                \or \xdef\mycolor{yellow}
                \or \xdef\mycolor{green}
                \or \xdef\mycolor{blue}
                \or \xdef\mycolor{violet}
                \else \xdef\mycolor{black}
            \fi
            \fill[\mycolor] ($(current page.north east)+(0,\mytop)$) rectangle ($(current page.north east)+(-0.5,\mybottom)$);
        \end{tikzpicture}
    },
    scale=1,
    angle=0
}

\newcommand{\mycolor}%
{   \foreach \x [count=\c] in {blue,violet,red,orange,yellow,green} {\ifthenelse{\c=\thechapter}{\x}{}}
}

\begin{document}

\lipsum

\chapter{First}
\lipsum

\chapter{Second}
\lipsum

\chapter{Third}
\lipsum

\chapter{Fourth}
\lipsum

\chapter{Fifth}
\lipsum

\chapter{Sixth}
\lipsum

\end{document}

输出

在此处输入图片描述


编辑1:现在检查章节总数并在奇数/偶数页使用左/右标记:

代码

\documentclass{book}
\usepackage[a4paper,margin=15mm]{geometry}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{xifthen}
\usepackage{totcount}
\usepackage{lipsum}

\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}
                \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}
Total chapters: \total{chapter}

Paper height: \pgfmathsetmacro{\yxc}{\paperheight/28.453}\yxc cm

Paper width: \pgfmathsetmacro{\cxy}{\paperwidth/28.453}\cxy cm

\lipsum

\chapter{First}
\lipsum

\chapter{Second}
\lipsum

\chapter{Third}
\lipsum

\chapter{Fourth}
\lipsum

\chapter{Fifth}
\lipsum

\chapter{Sixth}
\lipsum

\end{document}

输出

在此处输入图片描述

相关内容