如何将当前章节和部分添加到节点?
\node[color=white, anchor=west] at ($(current page.north west) - (-10ex,3.5ex)$)
{\ifnum\value{chapter}>0 \thechapter.~\chaptermark\fi};
\chaptermark
或\sectionmark
在这里不起作用。
\documentclass{scrbook}
\usepackage[
a4paper, top=25mm, bottom=30mm,
inner=20mm, outer=70mm,
marginparsep=7mm, marginparwidth=45mm,
]{geometry}
\usepackage{xcolor}
\definecolor{myviolet}{HTML}{4c0068}
\usepackage{microtype}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[ER]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[fill=myviolet!90, draw=none] ($(current page.north west) + (8.5ex,7ex)$) rectangle ($(current page.north east) - (0ex,7ex)$);
\draw[fill=myviolet, draw=none] ($(current page.north west) + (8ex,7ex)$) rectangle ($(current page.north west) - (0ex,7ex)$);
\node[color=white] at ($(current page.north west) - (-4ex,3.5ex)$) {\textbf{\Large\thepage}};
\node[color=white, anchor=west] at ($(current page.north west) - (-10ex,3.5ex)$) {\ifnum\value{chapter}>0 \thechapter.\fi};
\end{tikzpicture}%
}
\fancyhead[OL]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[fill=myviolet!90, draw=none] ($(current page.north west) + (0,7ex)$) rectangle ($(current page.north east) - (8.5ex,7ex)$);
\draw[fill=myviolet, draw=none] ($(current page.north east) - (8ex,7ex)$) rectangle ($(current page.north east) + (0ex,7ex)$);
\node[color=white] at ($(current page.north east) - (4ex,3.5ex)$) {\textbf{\Large\thepage}};
\node[color=white, anchor=east] at ($(current page.north east) - (10ex,3.5ex)$) {\ifnum\value{section}>0 \thesection.\fi};
\end{tikzpicture}%
}
\usepackage{lipsum}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[1-3]
\newpage
\section{Test Section 2}
\lipsum[1-3]
\newpage
\section{Test Section 3}
\lipsum[1-3]
\end{document}
答案1
\chaptermark
\markboth
然后,您可以\leftmark
在标题中将用作章节。同样,将 用作\sectionmark
章节条目\markright
。因此,\rightmark
您可以将 用作标题中的节条目。
\documentclass{scrbook}
\usepackage[
top=25mm, bottom=30mm,
inner=20mm, outer=70mm,
marginparsep=7mm, marginparwidth=45mm,
]{geometry}
\usepackage{tikz}% loads also xcolor
\definecolor{myviolet}{HTML}{4c0068}
\usepackage{microtype}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyhead[ER]{%
\begin{tikzpicture}[overlay,remember picture]
\fill[myviolet!90] ([xshift=8.5ex]current page.north west) rectangle ([yshift=-7ex]current page.north east);
\node[anchor=west] at ([xshift=10ex,yshift=-3.5ex]current page.north west) {\leftmark};
\fill[myviolet] ([yshift=-7ex]current page.north west) rectangle ([xshift=8ex]current page.north west);
\node at ([xshift=4ex,yshift=-3.5ex]current page.north west) {\pagemark};
\end{tikzpicture}%
}
\fancyhead[OL]{%
\begin{tikzpicture}[overlay,remember picture]
\fill[myviolet!90] (current page.north west) rectangle ([xshift=-8.5ex,yshift=-7ex]current page.north east);
\node[anchor=east] at ([xshift=-8.5ex,yshift=-3.5ex]current page.north east){\rightmark};
\fill[myviolet] ([xshift=-8ex,yshift=-7ex]current page.north east)rectangle (current page.north east);
\node at ([xshift=-4ex,yshift=-3.5ex]current page.north east){\pagemark};
\end{tikzpicture}%
}
% font for the pagenumber and the head entries
\setkomafont{pagenumber}{\color{white}\bfseries\Large}
\setkomafont{pageheadfoot}{\color{white}\bfseries\Large}
\renewcommand\chaptermark[1]{\markboth{\protect\usekomafont{pageheadfoot}\thechapter\autodot\ #1}{}}
\renewcommand\sectionmark[1]{\markright{\protect\usekomafont{pageheadfoot}\thesection\autodot\ #1}}
\usepackage{lipsum}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[1-3]
\newpage
\section{Test Section 2}
\lipsum[1-3]
\newpage
\section{Test Section 3}
\lipsum[1-3]
\end{document}
但我认为最好将scrlayer-scrpage
(或较旧的scrpage2
)与 一起使用scrbook
,而不是fancyhdr
。
\documentclass{scrbook}
\usepackage[
top=25mm, bottom=30mm,
inner=20mm, outer=70mm,
marginparsep=7mm, marginparwidth=45mm,
]{geometry}
\setlength\headheight{18pt}% suggested by scrlayer-scrpage
\setlength\footheight{18pt}% suggested by scrlayer-scrpage
\usepackage{tikz}% loads also xcolor
\definecolor{myviolet}{HTML}{4c0068}
\usepackage{microtype}
\usepackage[automark]{scrlayer-scrpage}% or the older scrpage2
\pagestyle{scrheadings}
\clearscrheadfoot
\lehead{%
\begin{tikzpicture}[overlay,remember picture]
\fill[myviolet!90] ([xshift=8.5ex]current page.north west) rectangle ([yshift=-7ex]current page.north east);
\node[anchor=west] at ([xshift=10ex,yshift=-3.5ex]current page.north west) {\leftmark};
\fill[myviolet] ([yshift=-7ex]current page.north west) rectangle ([xshift=8ex]current page.north west);
\node at ([xshift=4ex,yshift=-3.5ex]current page.north west) {\pagemark};
\end{tikzpicture}%
}
\rohead{%
\begin{tikzpicture}[overlay,remember picture]
\fill[myviolet!90] (current page.north west) rectangle ([xshift=-8.5ex,yshift=-7ex]current page.north east);
\node[anchor=east] at ([xshift=-8.5ex,yshift=-3.5ex]current page.north east){\rightmark};
\fill[myviolet] ([xshift=-8ex,yshift=-7ex]current page.north east)rectangle (current page.north east);
\node at ([xshift=-4ex,yshift=-3.5ex]current page.north east){\pagemark};
\end{tikzpicture}%
}
% font for the pagenumber and the head entries
\setkomafont{pagenumber}{\color{white}\bfseries\Large}
\setkomafont{pageheadfoot}{\color{white}\bfseries\Large}
\usepackage{lipsum}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[1-3]
\newpage
\section{Test Section 2}
\lipsum[1-3]
\newpage
\section{Test Section 3}
\lipsum[1-3]
\end{document}