我正在使用\usepackage{titlesec}
包并包含了最简单的示例来说明问题,并且第一个提出的解决方案仅在删除 titlesec 格式化命令时才有效\part
。
代码如下:
\documentclass{report}
\usepackage[rm, tiny,center, compact]{titlesec}
%%%%%%%% Code to change sectioning size in main body of the document
\titleformat{\part}{\centering\normalsize}{PART \thepart.}{1em}{}{} % conflcts with patchcmd
\titleformat{\chapter}{\centering\normalsize}{\thechapter.}{1em}{}
\titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
\titleformat{\subsection}{\centering\normalsize\it}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\centering\normalsize\it}{\thesubsubsection}{1em}{}
%%%%%%%%
\usepackage{etoolbox}
\begin{document}
\tableofcontents
\part{Name of part 1}
%%%%%%% Code to add 'PART' I First Part Title in the ToC both code parts don't work together.
\makeatletter
\patchcmd\@part
{\thepart\hspace{1em}}
{\MakeUppercase{\partname}\ \thepart\hspace{1em}}
{}{}
\makeatother
%%%%%%%
\part{Name of part 2}
\end{document}
@Phelype,运行此程序会导致错误.....
\documentclass{report}
\usepackage[rm, tiny,center, compact]{titlesec}
%%%%%%%% Code to change sectioning size in main body of the document
\titleformat{\part}{\centering\normalsize}{PART \thepart.}{1em}{}{} % conflcts with patchcmd
\titleformat{\chapter}{\centering\normalsize}{\thechapter.}{1em}{}
\titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
\titleformat{\subsection}{\centering\normalsize\it}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\centering\normalsize\it}{\thesubsubsection}{1em}{}
%%%%%%%%
\usepackage{titletoc}
\titlecontents{part}
[0pt]{}{}
{PART\ \thecontentslabel}
{\cftpartleader\contentspage}
[\endgraf\vskip\baselineskip]
\usepackage{etoolbox}
\begin{document}
\tableofcontents
\part{Name of part 1}
%%%%%%%% Code to add 'PART' I First Part Title in the ToC both code parts don't work together.
%\makeatletter
%\patchcmd\@part
% {\thepart\hspace{1em}}
% {\MakeUppercase{\partname}\ \thepart\hspace{1em}}
% {}{}
%\makeatother
%%%%%%%%
\part{Name of part 2}
\end{document}
答案1
ToC 行\part
由宏编写\@part
。您可以重新定义它,或者更简单地修补它:
\usepackage{etoolbox}
\makeatletter
\patchcmd\@part
{\thepart\hspace{1em}}
{\MakeUppercase{\partname}\ \thepart\hspace{1em}}
{}{}
\makeatother
注意力:你必须修补它前加载hyperref
。hyperref
重新定义\@part
并将旧的宏存储\@part
在中\H@old@part
。
编辑:之后长的在考虑大量的包相互覆盖彼此的定义时,我决定采用一个可行的解决方案,即在战斗坑中再添加一个包:
\usepackage{titletoc}
\titlecontents{part}
[0pt]{}{}
{PART\ \thecontentslabel}
{\cftpartleader\contentspage}
[\endgraf\vskip\baselineskip]
抱歉,我找不到更好的方法。
答案2
解决方案是在加载 titlesec 包后将以下内容添加到前言中。
\titlecontents{part}%
[0pt]{}%remove rule if you like
{}{\MakeUppercase{\partname}~}
{\cftpartleader\contentspage}%replaced with {} if don't want page number for parts
[\addvspace{2ex}]%remove rule if you like
这是根据另一篇文章创建的: 使用 titlesec 包时在 Toc 中的编号前添加“Part”