使用 titletoc 创建的目录中 \part 和 \chapter 的对齐问题

使用 titletoc 创建的目录中 \part 和 \chapter 的对齐问题

在我使用 titletoc 创建的目录中,存在一些我无法调试的对齐问题。

问题 1: 我的代码允许我控制章节和部分的标签宽度,但不能控制部分的标签宽度:标签宽度似乎取决于用作部分标签的字母(我需要使用自定义部分标签,涉及与部分标题相关的字母),产生的W标签宽度比 宽得多I。这导致部分标题与章节标题不一致。

问题2: 右侧的页码对齐不佳——对于大于 100 的部分页码,这一点非常明显,但对于粗体章节号和非粗体章节号,实际上也是如此。我需要\large对部分使用粗体字体,对章节使用正常大小的粗体字体。

非常感谢您的帮助!我花了几个小时使用 titletoc 包自定义目录,因此我更愿意使用该包而不是切换到其他包。

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss} 
\usepackage[pagestyles, outermarks]{titlesec}
\usepackage{titletoc}

\titlecontents{part}[0mm]  
  {\vspace{12pt}\large\normalfont\bfseries}  
  {\contentslabel{10mm}}{}            
  {\titlerule*[.75em]\bfseries{\contentspage}}  

\titlecontents{chapter}[9.3mm]     
  {\vspace{4pt}\normalsize\normalfont\bfseries}  
  {\contentslabel{9.3mm}}{}   
  {\titlerule*[.75em]\bfseries{\contentspage}}  

\titlecontents{section}[21.6mm]   
  {\vspace{0pt}\normalsize\normalfont} 
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}{\contentspage}}   

\begin{document}

\pagenumbering{roman}   
\tableofcontents  
\clearpage

\chapter*{Preface}  
\addcontentsline{toc}{chapter}{Preface}  
\clearpage 

\pagenumbering{arabic}   
\setcounter{chapter}{0}
\renewcommand\thepart{I} 
\renewcommand{\thechapter}{{I\arabic{chapter}}}
\renewcommand{\thesection}{{I\arabic{chapter}.\arabic{section}}} 
\renewcommand{\thesubsection}{}
\clearpage 
\part{Island}               
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}
\setcounter{chapter}{0}
\renewcommand\thepart{W} 
\renewcommand{\thechapter}{{W\arabic{chapter}}}
\renewcommand{\thesection}{{W\arabic{chapter}.\arabic{section}}} 

\setcounter{page}{50}  
\clearpage
\part{Water}              
\chapter{Ice} 
\section{Snow}
\chapter{Liquid}
\section{Wave}

\clearpage 
\setcounter{page}{400} 
\renewcommand\thepart{L}  
\renewcommand{\thechapter}{{L\arabic{chapter}}}
\renewcommand{\thesection}{{L\arabic{chapter}.\arabic{section}}} 
\part{Land}             
\chapter{Continent}
\section{Mountain}
\chapter{Rock}  
\section{Stone}

\clearpage
\setcounter{chapter}{0}
\renewcommand\thepart{A} 
\renewcommand{\thechapter}{{A\arabic{chapter}}}
\renewcommand{\thesection}{{A\arabic{chapter}.\arabic{section}}} 
\part{Animals}               
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}  

\end{document}  

最小工作示例

我刚刚找到了一个解决第 2 个问题(页码不对齐)的方法这个帖子 显然 \bfseries{\contentspage} 以不想要的方式影响了行填充。解决方案是使用 \hfill\bfseries\thecontentspage,如下面的修订代码所示,它可以生成很好的右对齐页码,见下文。不过,我仍然被问题 1 困住了。

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss} 
\usepackage[pagestyles, outermarks]{titlesec}
\usepackage{titletoc}

\titlecontents{part}[0mm]  
  {\vspace{12pt}\large\normalfont\bfseries}  
  {\contentslabel{10mm}}{}            
  {\hfill\bfseries\thecontentspage}  

\titlecontents{chapter}[9.3mm]     
  {\vspace{4pt}\normalsize\normalfont\bfseries}  
  {\contentslabel{9.3mm}}{}   
  {\hfill\bfseries\thecontentspage}

\titlecontents{section}[21.6mm]   
  {\vspace{0pt}\normalsize\normalfont} 
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}   

\begin{document}

\pagenumbering{roman}   
\tableofcontents  
\clearpage

\chapter*{Preface}  
\addcontentsline{toc}{chapter}{Preface}  
\clearpage 

\pagenumbering{arabic}   
\setcounter{chapter}{0}
\renewcommand\thepart{I} 
\renewcommand{\thechapter}{{I\arabic{chapter}}}
\renewcommand{\thesection}{{I\arabic{chapter}.\arabic{section}}} 
\renewcommand{\thesubsection}{}
\clearpage 
\part{Island}               
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}
\setcounter{chapter}{0}
\renewcommand\thepart{W} 
\renewcommand{\thechapter}{{W\arabic{chapter}}}
\renewcommand{\thesection}{{W\arabic{chapter}.\arabic{section}}} 

\setcounter{page}{50}  
\clearpage
\part{Water}              
\chapter{Ice} 
\section{Snow}
\chapter{Liquid}
\section{Wave}

\clearpage 
\setcounter{page}{400} 
\renewcommand\thepart{L}  
\renewcommand{\thechapter}{{L\arabic{chapter}}}
\renewcommand{\thesection}{{L\arabic{chapter}.\arabic{section}}} 
\part{Land}             
\chapter{Continent}
\section{Mountain}
\chapter{Rock}  
\section{Stone}

\clearpage
\setcounter{chapter}{0}
\renewcommand\thepart{A} 
\renewcommand{\thechapter}{{A\arabic{chapter}}}
\renewcommand{\thesection}{{A\arabic{chapter}.\arabic{section}}} 
\part{Animals}               
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}  

\end{document}  

最小工作示例

按照 esdd 建议的补丁程序,为了控制部件的标签宽度,我发布了 essd 的版本,其中添加了两行代码来格式化部件的标题。这两行代码以某种方式干扰了补丁程序,因为标题标签现在向右移动了。

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss}
\usepackage[pagestyles, outermarks]{titlesec}
\usepackage{titletoc}

\titleclass{\part}{top}
\titleformat{\part}[display]%     this line messes up the patch
{\normalfont\Huge}{\centering\thepart}{1em}{\centering}                      
\titleformat{\chapter}{\normalfont\LARGE}{\thechapter}{1em}{}
\titleformat{\section}{\normalfont\Large}{\thesection}{1em}{} 

\titlecontents{part}[10mm]
  {\vspace{12pt}\large\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{chapter}[10mm]
  {\vspace{4pt}\normalsize\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{section}[22.3mm]
  {\vspace{0pt}\normalsize\normalfont}
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\@part
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\protect\numberline{\thepart}#1}}
  {}{\PatchFailed}
\makeatother

\usepackage[linktocpage]{hyperref}% after the patch

更新的MWE

答案1

更新(重新定义\part使用\titleformat

关于问题中的补充:如果你重新定义\part使用\titleformat修补\@part不再有帮助。但是然后添加newparttoc包选项titlesec将解决您的问题:

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss}
\usepackage[pagestyles, outermarks,
  newparttoc% <- added
]{titlesec}
\usepackage{titletoc}
\usepackage[linktocpage]{hyperref}

\titleclass{\part}{top}
\titleformat{\part}[display]
  {\normalfont\Huge}{\centering\thepart}{1em}{\centering}%
  [\setcounter{chapter}{0}]% <- added to reset the chapter counter for each part

\contentsmargin{0pt}%<- added (as suggested by jvd in a comment)

\titlecontents{part}[10mm]% <- changed
  {\vspace{12pt}\large\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{chapter}[10mm]
  {\vspace{4pt}\normalsize\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{section}[22.3mm]
  {\vspace{0pt}\normalsize\normalfont}
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}

\renewcommand\thechapter{\thepart\arabic{chapter}}
\setcounter{secnumdepth}{1}
\begin{document}
\pagenumbering{roman}
\tableofcontents
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
\cleardoublepage 
\pagenumbering{arabic}
\renewcommand\thepart{I}
\part{Island}
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}

\cleardoublepage% only for the example
\setcounter{page}{51}% only for the example
\renewcommand\thepart{W}
\part{Water}
\chapter{Ice}
\section{Snow}
\chapter{Liquid}
\section{Wave}

\cleardoublepage% only for the example
\setcounter{page}{401}% only for the example
\renewcommand\thepart{L}
\part{Land}
\chapter{Continent}
\section{Mountain}
\chapter{Rock}
\section{Stone}

\clearpage
\renewcommand\thepart{A}
\part{Animals}
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}
\end{document}

结果:

在此处输入图片描述


原始答案(无需重新定义\part使用\titleformat

您可以修补\@part以删除零件编号和输入文本之间的固定空格,并使用\numberline

\usepackage{xpatch}
\makeatletter
\xpatchcmd\@part
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\protect\numberline{\thepart}#1}}
  {}{\PatchFailed}
\makeatother

在此处输入图片描述

代码:

\documentclass[12pt,a4paper,openany]{book} 
\renewcommand{\familydefault}{cmss}
\usepackage[pagestyles, outermarks]{titlesec}
\usepackage{titletoc}

\titlecontents{part}[10mm]
  {\vspace{12pt}\large\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{chapter}[10mm]
  {\vspace{4pt}\normalsize\normalfont\bfseries}
  {\contentslabel{10mm}}{}
  {\hfill\thecontentspage}

\titlecontents{section}[22.3mm]
  {\vspace{0pt}\normalsize\normalfont}
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}


\usepackage{xpatch}
\makeatletter
\xpatchcmd\@part
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\protect\numberline{\thepart}#1}}
  {}{\PatchFailed}
\makeatother

\usepackage[linktocpage]{hyperref}% after the patch

\xpretocmd\part{\setcounter{chapter}{0}}{}{\PatchFailed}
\renewcommand\thechapter{\thepart\arabic{chapter}}
\setcounter{secnumdepth}{1}


\begin{document}
\pagenumbering{roman}
\tableofcontents
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
\cleardoublepage 
\pagenumbering{arabic}
\renewcommand\thepart{I}
\part{Island}
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}

\cleardoublepage% only for the example
\setcounter{page}{51}% only for the example
\renewcommand\thepart{W}
\part{Water}
\chapter{Ice}
\section{Snow}
\chapter{Liquid}
\section{Wave}

\cleardoublepage% only for the example
\setcounter{page}{401}% only for the example
\renewcommand\thepart{L}
\part{Land}
\chapter{Continent}
\section{Mountain}
\chapter{Rock}
\section{Stone}

\clearpage
\renewcommand\thepart{A}
\part{Animals}
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}
\end{document}

使用 KOMA-Script 类,就无需修补内部命令:

\documentclass[12pt,a4paper,openany]{scrbook} 
\renewcommand{\familydefault}{cmss} 
%\usepackage[pagestyles, outermarks]{titlesec}

\usepackage{titletoc}

\titlecontents{part}[11mm]
  {\vspace{12pt}\large\normalfont\bfseries}
  {\contentslabel{11mm}}{}
  {\hfill\thecontentspage}

\titlecontents{chapter}[11mm]
  {\vspace{4pt}\normalsize\normalfont\bfseries}
  {\contentslabel{11mm}}{}
  {\hfill\thecontentspage}

\titlecontents{section}[23.3mm]
  {\vspace{0pt}\normalsize\normalfont}
  {\contentslabel[\thecontentslabel]{12.3mm}}{}
  {\titlerule*[.75em]{.}\thecontentspage}

\usepackage{xpatch}
\usepackage[linktocpage]{hyperref}
\xpretocmd\part{\setcounter{chapter}{0}}{}{\PatchFailed}
\renewcommand\thechapter{\thepart\arabic{chapter}}
\setcounter{secnumdepth}{1}
\begin{document}

\pagenumbering{roman}
\tableofcontents
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}

\clearpage 
\pagenumbering{arabic}
\renewcommand\thepart{I} 
\renewcommand{\thechapter}{{\thepart\arabic{chapter}}}
\part{Island}
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}

\clearpage
\setcounter{page}{50}% only for the example
\renewcommand\thepart{W}
\part{Water}
\chapter{Ice} 
\section{Snow}
\chapter{Liquid}
\section{Wave}

\clearpage
\setcounter{page}{400}% only for the example
\renewcommand\thepart{L}
\part{Land}
\chapter{Continent}
\section{Mountain}
\chapter{Rock}
\section{Stone}

\clearpage
\renewcommand\thepart{A} 
\part{Animals}
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}
\end{document}

但是使用 KOMA-Script 类,也不需要使用tocloft来格式化目录条目:

\documentclass[12pt,a4paper,openany]{scrbook} 
\renewcommand{\familydefault}{cmss} 
%\usepackage[pagestyles, outermarks]{titlesec}

\setuptoc{toc}{numberline}
\DeclareTOCStyleEntry[
  beforeskip=12pt,
  pagenumberbox=\pnmbox,
  indent=0pt,
  numwidth=10mm,
]{tocline}{part}
\DeclareTOCStyleEntry[
  beforeskip=4pt,
  pagenumberbox=\pnmbox,
  indent=0pt,
  numwidth=10mm,
]{tocline}{chapter}
\DeclareTOCStyleEntry[
  indent=10mm,
  numwidth=12.3mm
]{tocline}{section}
\setkomafont{partentry}{\large\normalfont\bfseries}
\setkomafont{chapterentry}{\normalfont\bfseries}
\newcommand*\pnmbox[1]{\mbox{#1}}

\usepackage{xpatch}
\usepackage[linktocpage]{hyperref}
\xpretocmd\part{\setcounter{chapter}{0}}{}{\PatchFailed}
\renewcommand\thechapter{\thepart\arabic{chapter}}
\setcounter{secnumdepth}{1}
\begin{document}

\pagenumbering{roman}
\tableofcontents
\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}

\clearpage 
\pagenumbering{arabic}
\renewcommand\thepart{I} 
\renewcommand{\thechapter}{{\thepart\arabic{chapter}}}
\part{Island}
\chapter{Desert}
\section{Shelter}
\chapter{Paradise}
\section{Beach}

\clearpage
\setcounter{page}{50}% only for the example
\renewcommand\thepart{W}
\part{Water}
\chapter{Ice} 
\section{Snow}
\chapter{Liquid}
\section{Wave}

\clearpage
\setcounter{page}{400}% only for the example
\renewcommand\thepart{L}
\part{Land}
\chapter{Continent}
\section{Mountain}
\chapter{Rock}
\section{Stone}

\clearpage
\renewcommand\thepart{A} 
\part{Animals}
\chapter{Mammals}
\section{Humans}
\chapter{Reptiles}
\section{Lizards}
\end{document}

相关内容