我想在书中按以下方式自定义标题:
- 在非文本页面上(例如部分标题或章节标题页):没有页眉,没有页脚
- 在文本页面上
- 偶数页:页码在左侧,章节标题在右侧,格式为
Chapter 1: Title
(粗体,灰色)。如果不在章节中,则为部分标题。如果不在部分中,则为书名。 - 奇数页:页码在右侧,章节标题在左侧,格式为
Title
(粗体,灰色)。如果不在章节中,则为章节标题。如果不在章节中,则为部分标题。如果不在部分中,则为书名。
- 偶数页:页码在左侧,章节标题在右侧,格式为
如何实现这个目的fancyhdr
?
起始文件:
\documentclass[twoside, 10pt]{book}
\usepackage[
paperwidth=6in,
paperheight=9in,
tmargin=0.75in,
bmargin=0.75in,
inner=0.75in,
outer=0.75in
]{geometry}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\edef\restoreparindent{\parindent=\the\parindent\relax}
\usepackage[parfill]{parskip}
\restoreparindent
\pagestyle{fancy}
\fancyhead[RO,LE]{\thepage}
\fancyhead[LO]{\nouppercase\rightmark}
\fancyhead[RE]{\nouppercase\leftmark}
\cfoot{}
\renewcommand{\headrulewidth}{}
\renewcommand{\footrulewidth}{}
\begin{document}
\tableofcontents
\chapter{This is a chapter name}
\chaptertoc
\lipsum
\section{First section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\end{document}
答案1
您应该将 \headrulewidth 重新定义为 0pt,而不是空。除此之外,您只需要设置正确的标记:
\documentclass[twoside, 10pt]{book}
\usepackage[
paperwidth=6in,
paperheight=9in,
tmargin=0.75in,
bmargin=0.75in,
inner=0.75in,
outer=0.75in
]{geometry}
\usepackage{lipsum}
\usepackage{fancyhdr,xcolor}
\usepackage{tabularx}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\edef\restoreparindent{\parindent=\the\parindent\relax}
\usepackage[parfill]{parskip}
\restoreparindent
\pagestyle{fancy}
\fancyhead[RO,LE]{\thepage}
\fancyhead[LO]{\bfseries\color{gray}\nouppercase\rightmark}
\fancyhead[RE]{\bfseries\color{gray}\nouppercase\leftmark}
\cfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\renewcommand\chaptermark[1]{\markboth{Chapter~\thechapter: #1}{Chapter~\thechapter: #1}}
\renewcommand\sectionmark[1]{\markright{#1}}
\usepackage{xpatch}
%patch \@part as book doesn't know \partname:
\makeatletter
\xpatchcmd\@part{\markboth{}{}}{\markboth{Part~\thepart: #1}{Part~\thepart: #1}}{}{\fail}
\makeatother
\begin{document}
\markboth{Book Title}{Book Title}
\lipsum
\tableofcontents
\part{Part}
blbllb
\newpage blblb
\chapter{This is a chapter name}
%\chaptertoc
\lipsum \lipsum
\section{First section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\end{document}