根据章节外观定制目录

根据章节外观定制目录

经过几次 fancyhdr 和 titlesec 的尝试,我终于能够得到我想要的关于页眉和章节首页的内容。老实说,我并不理解我写进代码的每一行,所以很多事情可能可以用更好的方式完成,但目前我感到很满意。我的最后一个问题是目录,我想自定义它,但我几乎不知道从哪里开始。我可能在之前的回答中收到了一些意见,但它关注的是另一个方面,我想确保找到一个与我的新审美要求和我迄今所做的事情兼容的解决方案。所以,这是我的 MWE:

\documentclass[11pt,twoside]{book}
\usepackage[a5paper,inner=14mm,outer=20mm,top=21mm,bottom=22.5mm]{geometry}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{graphicx}
\usepackage{units}
\usepackage{tcolorbox}

\usepackage{mwe}

\usepackage{titlesec}
\titleformat{\chapter}[display]
{\normalfont\Large\filcenter\rmfamily}
{\LARGE\sf\MakeUppercase{\chaptertitlename} \huge\thechapter}
{0.5pc}
{\color{green}\titleline*[c]{\titlerule[3pt]}\color{black}
\vspace{1pc}%
\Huge\bf}[\vspace{6pt}]
\setlength{\titlewidth}{0.7\textwidth}

\usepackage{fancyhdr}
\pagestyle{fancy}
\headheight=14pt
\fancyhf{}
\fancyhead[LE,RO]{\textsf{\thepage}}
\fancyhead[LO]{\leftmark}
\renewcommand{\chaptermark}[1]{\markboth{\textsf{\textit{\small{\MakeUppercase%
{\chaptername}}\ \normalsize{\thechapter}}\quad\, #1}}{}}
\fancyhead[RE]{\textsf{\textit{John Doe}\quad\, \small{A SHORT ESSAY ON NOTHING}}}
\renewcommand{\headrulewidth}{3pt}
\let\oldheadrule\headrule
\renewcommand{\headrule}{{\color{green}\oldheadrule}}

\fancypagestyle{plain}{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}

\begin{document}

\tableofcontents

\setcounter{chapter}{-1}
\renewcommand*{\thechapter}{}% get rid of the number
\makeatletter\renewcommand*{\@chapapp}{Introduction}\makeatother
\chapter{There is a title here too}
\lipsum[1-3]

\renewcommand*{\thechapter}{\arabic{chapter}}% bring back the chapter numbers
\makeatletter\renewcommand*{\@chapapp}{\chaptername}\makeatother% and the chapter name
\chapter{Title of Chapter One}
\lipsum

\chapter{Title of Chapter Two}

\chapter{Title of Chapter Three is longer}

\chapter{Short}

\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}

\end{document}

它在每一章开始时生成的页面如下:

在此处输入图片描述

因此,我得到的未经任何定制的目录是这样的:

在此处输入图片描述

我想要达到的结果是这样的(我用Word做到了):

在此处输入图片描述

需要注意的主要细节是:

  • “CONTENTS”一词应被视为“CHAPTER 1”,而不是其标题,因此它应该放在多于绿线,且采用大写无衬线字体。

  • 每个条目的章节数故意比“CHAPTER”一词稍大一些(斜体,大写,无衬线)

  • 如果标题很长,章节名称最好用粗体,一行就可以

  • 页码为无衬线字体

  • 行与行之间的间隔,以及“目录”上方和下方的垂直空间应该是可自定义的,这样我就可以确保最多 15 个章节不需要额外的页面

  • 附加问题:我必须确保目录之前和之后的页面是空的(它们有标题,我不确定这是否与目录本身有关,可能是我的错,搞乱了 fancyhdr)

我并不是要求完整的代码,我愿意自己做,但我需要一些关于正确的包及其功能的解释和建议,以便能够根据默认值执行所有这些更改,可能(这是一种请求)保存我迄今为止所做的一切!

非常感谢!


既然有人问我,这就是章节计数器的结果:带有标题但不影响后续章节编号的介绍。这是我绝对想保存的东西:

在此处输入图片描述

答案1

这是一个解决方案。对于目录问题,我使用键numberlesstitlesec定义一个特别指定格式和titletoc包。我借此机会对您的代码进行了一些更正(例如,\bfseries在 的位置\bf,该位置已被弃用 20 多年)。

对于目录之后未编号的章节,我必须定义一个适应的页面样式,nonumber。我使用 定义了页面样式  titleps,我发现它比 更易于使用fancyhdr

至于您的“附加问题”,删除没有文本的页面中的页脚和页眉非常简单:可以通过选项clearempty获得titlesec

\documentclass[11pt,twoside, x11names]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a5paper,inner=14mm,outer=20mm,top=21mm,bottom=22.5mm]{geometry}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{textcomp}
\usepackage{gensymb}
\usepackage{graphicx}
\usepackage{units}
\usepackage{tcolorbox}

\usepackage{mwe}

\usepackage[clearempty, pagestyles]{titlesec}
\usepackage[rightlabels]{titletoc}
        \titlecontents{chapter}
        [6.55em] %5.3
        {\bigskip}
        {\contentslabel[\textsf{\itshape\footnotesize\MakeUppercase{\chaptername}~\thecontentslabel}]{6.55em}\quad\textbf}%\thecontentslabel
        {\hspace*{-6.55em}\textbf}% unnumbered chapters
        {\hfill\sffamily\contentspage}[\smallskip]%
    %

\titleformat{\chapter}[display]
{\thispagestyle{empty}\normalfont\Large\filcenter\rmfamily}
{\LARGE\sffamily\MakeUppercase{\chaptertitlename} \huge\thechapter}
{0.5pc}
{\color{DarkSeaGreen3}\titleline*[c]{\titlerule[3pt]}\color{black}
%
\Huge\bfseries}[\vspace{6pt}]

 \titleformat{name=\chapter, numberless}[block]
 {\thispagestyle{empty}\vspace*{0.7ex}\normalfont\LARGE\filcenter\sffamily}
 {}
 {0em}
 {\MakeUppercase}[\vspace{-0.8ex}{\color{DarkSeaGreen3}\rule{\titlewidth}{3pt}}]%

 \setlength{\titlewidth}{0.7\textwidth}

\newpagestyle{mystyle}{%
\renewcommand{\makeheadrule}{\color{DarkSeaGreen3}\rule[-1.2ex]{\linewidth}{3pt}}
%%
\sethead[\sffamily\thepage][][\sffamily\textit{John Doe}\quad\small A SHORT ESSAY ON NOTHING]%
{\sffamily\small\textit{\MakeUppercase{\chaptername}}~\thechapter\quad\normalsize\chaptertitle}{}{\sffamily\thepage}%
\setfoot{}{}{}
}
%
\newpagestyle{nonumber}{%
\renewcommand{\makeheadrule}{\color{DarkSeaGreen3}\rule[-1ex]{\linewidth}{3pt}}
%%
\sethead[\sffamily\thepage][][\sffamily\textit{John Doe}\quad\small A SHORT ESSAY ON NOTHING]%
{\sffamily\chaptertitle}{}{\sffamily\thepage}%
\setfoot{}{}{}
}

\begin{document}

\tableofcontents

\clearpage
\pagestyle{nonumber}
{
\titleformat{name=\chapter, numberless}[display]
{\thispagestyle{empty}\vspace*{0.6ex}\normalfont\Large\filcenter\rmfamily}
{\LARGE\sffamily INTRODUCTION}
{0.55pc}
{\color{DarkSeaGreen3}\titleline*[c]{\titlerule[3pt]}\color{black}
\Huge\bfseries}[\vspace{6pt}]
\chapter*{There is a title here too}}
\addcontentsline{toc}{chapter}{\textsf{\mdseries\itshape\footnotesize INTRODUCTION}\quad There is a title here too}
\chaptermark{\textsf{\mdseries\itshape\footnotesize INTRODUCTION}\quad There is a title here too}
\lipsum%[1-3]}
\clearpage
\pagestyle{mystyle}
\chapter{Title of Chapter One}
\lipsum

\chapter{Title of Chapter Two}

\chapter{Title of Chapter Three is longer}

\chapter{Short}

\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}
\chapter{Other normal title}

\end{document} 

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

相关内容