我一直在尝试使用 KOMA 和 TikZ 在页码周围使用彩色样本来指示章节;每个章节的文本上方还有一个相同颜色的破折号,以增添风格。颜色在每个章节之前使用 定义\chaptercolor{colorname}
,其中\chaptercolor
定义为\newcommand{\chaptercolor}[1]{\colorlet{chaptercolor}{#1}}
。
\chaptercolor
必须在章节之前调用该命令来设置线条色板,但这会导致前一页的颜色色板也发生变化。我还尝试创建第二个\swatchcolor
命令来单独更改章节线条标记的色板,这会导致新章节之前的页面获得色板的默认颜色。
我该如何避免这种情况?
我尝试了各种 KOMA 选项组合(例如cleardoublepage
,open
),但似乎都没有我想要的效果。甚至在 的定义中添加\clearpage
before似乎也不起作用。这是一个几乎有效的 MWE。\colorlet
\chaptercolor
\documentclass{scrbook}
\usepackage{scrlayer-scrpage}
%% KOMA options
\KOMAoption{chapterprefix}{true}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{11pt}
\KOMAoption{headings}{normal}
\KOMAoption{open}{any}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{twoside}{semi}
\KOMAoption{titlepage}{false}
%% Subfiles for shared preambles
\usepackage[math]{blindtext}
%% Colours - put this before typography so additional named colours can be defined.
\usepackage[table]{xcolor}
\colorlet{darkblue}{blue!40!black}
\usepackage{tikz}
\tikzset{%
small wedge/.pic={
\fill (0, 0) -- ++(-1.2, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- cycle;
},
}
\setkomafont{pagehead}{\small\color{darkblue}\selectfont}
\setkomafont{pagenumber}{\small\color{darkblue}\selectfont}
% Chapters are now "Tutorials"
% Chapters have an em-dash that is \chaptercolor
\newcommand{\chaptercolor}[1]{\colorlet{chaptercolor}{#1}}
\chaptercolor{white}
\renewcommand*{\chapapp}{Tutorial}
\renewcommand*{\chapterheadstartvskip}{\vspace*{-\topskip}}
\renewcommand\chapterformat{{\fontsize{50}{50}\upshape\selectfont\textcolor{chaptercolor}{—}\par\nobreak\chapapp~\thechapter}}
%% Use this to set up the headers and footers
\DeclareNewLayer[
bottommargin,
background,
evenpage,
mode=picture,
contents={%
\putLR{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape] at (0,0) {small wedge};
\node[align=right] at (-10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.even}
\DeclareNewLayer[
bottommargin,
background,
oddpage,
mode=picture,
contents={%
\putLL{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape, xscale=-1] at (0,0) {small wedge};
\node[align=left] at (10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.odd}
\AddLayersAtBeginOfPageStyle{scrheadings}{wedge.odd,wedge.even}%
\pagestyle{scrheadings}
\renewcommand*{\chapterpagestyle}{scrheadings}
\clearscrheadfoot
\lohead{This is a test}
\rehead{\leftmark}
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
% % Metadata goes here
\titlehead{Test document}
\subject{Test subject}
\title{Testing of page number colors}
\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother
\begin{document}
\frontmatter
\maketitle
\tableofcontents
\mainmatter
\setcounter{secnumdepth}{1}
\chaptercolor{cyan!50}
\chapter{The first chapter of this work}
\blindtext
\blindmathpaper
\blindtext
\chaptercolor{magenta!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blinditemize
\blindtext[10]
\chaptercolor{orange!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blindenumerate
\blindtext[12]
\chaptercolor{green!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blinddescription
\blindtext[20]
\end{document}
我已经根据其他问题制定了 MWE 的部分内容,但我还没有找到或想出一个完全符合我要求的解决方案。经过进一步思考,我喜欢能够分别设置两种颜色的想法,但重要的是,颜色更改必须仅在新章节之后生效。
答案1
现在\chaptercolor
定义一个, 当执行时,newchaptercolor
它将变为,而它又用于一切。此颜色变化通过 进行广播。chaptercolor
\chapter
\globalcolorstrue
\documentclass{scrbook}
\usepackage{scrlayer-scrpage}
%% KOMA options
\KOMAoption{chapterprefix}{true}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{11pt}
\KOMAoption{headings}{normal}
\KOMAoption{open}{any}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{twoside}{semi}
\KOMAoption{titlepage}{false}
%% Subfiles for shared preambles
\usepackage[math]{blindtext}
%% Colours - put this before typography so additional named colours can be defined.
\usepackage[table]{xcolor}
\colorlet{darkblue}{blue!40!black}
\usepackage{tikz}
\tikzset{%
small wedge/.pic={
\fill (0, 0) -- ++(-1.2, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- ++(0, 0.6) -- ++(0.6, 0) -- cycle;
},
}
\setkomafont{pagehead}{\small\color{darkblue}\selectfont}
\setkomafont{pagenumber}{\small\color{darkblue}\selectfont}
% Chapters are now "Tutorials"
% Chapters have an em-dash that is \chaptercolor
\newcommand{\chaptercolor}[1]{\colorlet{newchaptercolor}{#1}}
\chaptercolor{white}\colorlet{chaptercolor}{white}
\renewcommand*{\chapapp}{Tutorial}
\renewcommand*{\chapterheadstartvskip}{\vspace*{-\topskip}}
\renewcommand\chapterformat{{\fontsize{50}{50}\upshape\selectfont%
\globalcolorstrue\colorlet{chaptercolor}{newchaptercolor}\textcolor{chaptercolor}{-}\par\nobreak\chapapp~\thechapter}}
%% Use this to set up the headers and footers
\DeclareNewLayer[
bottommargin,
background,
evenpage,
mode=picture,
contents={%
\putLR{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape] at (0,0) {small wedge};
\node[align=right] at (-10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.even}
\DeclareNewLayer[
bottommargin,
background,
oddpage,
mode=picture,
contents={%
\putLL{%
\begin{tikzpicture}[remember picture, overlay]
\path pic[fill=chaptercolor, transform shape, xscale=-1] at (0,0) {small wedge};
\node[align=left] at (10mm, 10mm) {\pagemark};
\end{tikzpicture}
}
}]{wedge.odd}
\AddLayersAtBeginOfPageStyle{scrheadings}{wedge.odd,wedge.even}%
\pagestyle{scrheadings}
\renewcommand*{\chapterpagestyle}{scrheadings}
\clearscrheadfoot
\lohead{This is a test}
\rehead{\leftmark}
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
% % Metadata goes here
\titlehead{Test document}
\subject{Test subject}
\title{Testing of page number colors}
\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\let\thedate\@date
\makeatother
\begin{document}
\frontmatter
\maketitle
\tableofcontents
\mainmatter
\setcounter{secnumdepth}{1}
\chaptercolor{cyan!50}
\chapter{The first chapter of this work}
\blindtext
\blindmathpaper
\blindtext
\chaptercolor{magenta!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blinditemize
\blindtext[10]
\chaptercolor{orange!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blindenumerate
\blindtext[12]
\chaptercolor{green!50}
\chapter{Another chapter to test this thing}
\blindmathpaper
\Blinddescription
\blindtext[20]
\end{document}
答案2
一种方法是将\clearpage
(或\cleardoublepage
)添加到\chaptercolor
命令中
\newcommand{\chaptercolor}[1]{\clearpage\colorlet{chaptercolor}{#1}}
另一种方法是使用当前 KOMA 提供的钩子之一。然后您必须更改颜色的初始化代码:
\colorlet{chaptercolor}{white} %init
\newcommand{\chaptercolor}[1]{%
\AddtoOneTimeDoHook{heading/postinit/chapter}{\colorlet{chaptercolor}{#1}}}