如何创建带有副标题的部分标题?

如何创建带有副标题的部分标题?

我想自定义我的目录以获得以下布局(见下图)。

在此处输入图片描述

如您所见,此标题部分包含三个部分:带有字母的数字(“第一部分”)、标题(“为什么 Latex 如此复杂?”)和副标题(“答案和问题”)。我该如何修改跟随MWE为了得到这个结果?

\documentclass[hidelinks,12pt,twoside,openright,a4paper]{book}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{setspace}
\usepackage{lipsum}
\usepackage{tocloft}

%%%% center part titles
\renewcommand{\cftpartfont}{\hfil\bfseries}
\renewcommand{\cftpartleader}{\hfil}

\setlength{\cftbeforechapskip}{3pt}%espace entre chapitres
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{fancyhdr}

\pagestyle{fancy}
% We don’t want chapter and section numbers
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{} % sets both header and footer to nothing
\fancyfoot{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[CE]{\textit{A title}} % even pages: chapter title
\fancyhead[CO]{\textit\leftmark} % odd pages: book title

\setlength{\headheight}{15pt} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\title{A title}
\author{A name}
\date{}
\maketitle

\tableofcontents
    
\frontmatter
\part{Why is Latex so complicated?}
\chapter{A chapter}
\lipsum
    
\mainmatter
\part{How to customize a part section?}
\chapter{Another chapter}
\lipsum

\backmatter

\end{document}

答案1

要完全自定义toc行为,您可能需要重新定义\l@partand\@part命令。要了解这些内部命令是如何定义的,您需要查看文件book.cls。我添加了一个新的计数器来表示“第一、第二、第三……等”中各部分的顺序。我只定义了最多 10 个部分的情况。如果您需要超过 10 个,则需要在命令中扩展定义\partorder。您没有提到是否希望页码跟在目录中的部分条目后面。我会假设您这样做。此外,现在文档中的部分标题仍为原始形式(例如第一部分标题……)。我不确定您是否希望它们也采用“第一部分标题……”的形式,所以我没有更改它。我加载了包,hyperref因为hidelinks您的中有选项\documentclass。如果您不加载hyperref包,则此选项无用。并且因为您想在部分标题中手动换行,您可以通过输入\\标题来实现。但是,如果hyperref加载,则\\不允许在 PDF 书签中使用。您需要使用来\texorpdfstring{}{}告诉 latex 如何在 PDF 书签中输入此标题。

\documentclass[hidelinks,12pt,twoside,openright,a4paper]{book}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{setspace}
\usepackage{lipsum}
\usepackage{tocloft}
\setlength{\cftbeforechapskip}{3pt}%espace entre chapitres
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters

\makeatletter
\renewcommand\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\hfill\parbox{0.6\linewidth}{\centering\large \bfseries #1}\hfill
       \hb@xt@\@pnumwidth{\hss #2%
                          \kern-\p@\kern\p@}}\par
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}

\newcounter{partorder}
\newcommand{\partorder}{%
\ifcase\value{partorder}\or First\or Second\or Third\or Fourth\or Fifth\or Sixth\or Seventh\or Eighth\or Ninth\or Tenth\else ??(out range)\fi%
} % If you have more than 10 parts, you need extend this define

\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >-2\relax
      \refstepcounter{part}\stepcounter{partorder}%
      \addcontentsline{toc}{part}{\texorpdfstring{\partorder\space\partname\\#1}{\partorder\space\partname\ --\ #1}}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    \markboth{}{}%
    {\centering
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname\nobreakspace\thepart
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
    \@endpart}
\makeatother



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{fancyhdr}

\pagestyle{fancy}
% We don’t want chapter and section numbers
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{} % sets both header and footer to nothing
\fancyfoot{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[CE]{\textit{A title}} % even pages: chapter title
\fancyhead[CO]{\textit\leftmark} % odd pages: book title

\setlength{\headheight}{15pt}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\title{A title}
\author{A name}
\date{}
\maketitle

\tableofcontents
    
\frontmatter
\part{\texorpdfstring{Why is Latex so complicated?\\Answer and questions}{Why is Latex so complicated?\ --\ Answer and questions}}
\chapter{A chapter}
\lipsum
    
\mainmatter
\part{How to customize a part section?}
\chapter{Another chapter}
\lipsum

\setcounter{part}{10}
\setcounter{partorder}{10}
\part{\texorpdfstring{How about more than 10 parts?\\You need define it in \textbackslash partorder command}{How about more than 10 parts?\ -- \ You need define it in \textbackslash partorder command}}
\chapter{Another chapter}

\backmatter

\end{document}

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

相关内容