如何使 titlesec 的垂直对齐与 tocloft 相匹配?

如何使 titlesec 的垂直对齐与 tocloft 相匹配?

我想将包括水平线的目录标题(使用tocloft)与简介章节标题(使用titlesec)垂直对齐。但是,我很困惑,因为这两个包似乎测量不同的空间。我可以通过反复试验来匹配它们,但有人能告诉我如何准确测量吗?或者我应该尝试不同的方法?

\documentclass[draft,a4paper,11pt,oneside]{book}
\usepackage{lipsum}     
\usepackage{sectsty} % Used for sectionrule in ToC
\usepackage{titlesec}   

%---Change Margins
\usepackage[DIV=9,BCOR=2mm,headinclude=true,footinclude=false]{typearea}

%---Chapter Title Format 
\titleformat{\chapter}[display]{\usefont{T1}{qhv}{b}{n}\selectfont\huge}
{\chaptertitlename\ \thechapter}{22pt}{\huge}[\vspace{1.5ex}{\titlerule[1pt]}]

\titlespacing*{\chapter}{0mm}{-22pt}{40pt} % Removes gap above chapters.

%---ToC Format
\usepackage{tocloft} 
\renewcommand{\cfttoctitlefont} % ToC title
         %---How to match sectionrule with the 'Introduction' chapter?  
         {\usefont{T1}{qhv}{b}{n}\selectfont\huge\sectionrule{0ex}{0pt}{-2ex}{1pt}} 
\renewcommand{\cftchapfont} % chapter titles
         {\usefont{T1}{qhv}{b}{n}\selectfont}
\renewcommand{\cftsecfont} % section titles
         {\usefont{T1}{bch}{m}{n}\selectfont}
\renewcommand{\cftsubsecfont} % subsection titles
         {\usefont{T1}{bch}{m}{n}\selectfont} 
\renewcommand{\cftchappagefont} % chapter page numbers
         {\usefont{T1}{bch}{b}{n}\selectfont}
\renewcommand{\cftsecpagefont} % section page numbers
         {\cftsecfont} 

%---Removes gap above TOC. How to match 'Contents' with chapter 'Introduction'?                     
\setlength{\cftbeforetoctitleskip}{-22pt} 

\begin{document}

\tableofcontents

\chapter*{Introduction} %Without Chapter Number
\addcontentsline{toc}{chapter}{Introduction}

\lipsum[1]

\chapter{Cats vs Dogs} %With Chapter Number

\lipsum[2]  

\section{History}

\lipsum[3]

\end{document}  

答案1

您必须tocloft使用该titles选项进行加载(参见手册中的第 2.1 节)。

\documentclass[draft,a4paper,11pt,oneside]{book}
\usepackage[DIV=9,BCOR=2mm,headinclude=true,footinclude=false]{typearea}
\usepackage{titlesec}   
\usepackage[titles]{tocloft} 

\usepackage{lipsum}     

%---Chapter Title Format 
\titleformat{\chapter}[display]
  {\usefont{T1}{qhv}{b}{n}\selectfont\huge}
  {\chaptertitlename\ \thechapter}
  {22pt}
  {\huge}
  [\vspace{1.5ex}{\titlerule[1pt]}]

\titlespacing*{\chapter}{0mm}{-22pt}{40pt} % Removes gap above chapters.
\addtocontents{toc}{\protect\vspace{-10pt}} % remove gap before first chapter

%---ToC Format
\renewcommand{\cftchapfont} % chapter titles
         {\usefont{T1}{qhv}{b}{n}\selectfont}
\renewcommand{\cftsecfont} % section titles
         {\usefont{T1}{bch}{m}{n}\selectfont}
\renewcommand{\cftsubsecfont} % subsection titles
         {\usefont{T1}{bch}{m}{n}\selectfont} 
\renewcommand{\cftchappagefont} % chapter page numbers
         {\usefont{T1}{bch}{b}{n}\selectfont}
\renewcommand{\cftsecpagefont} % section page numbers
         {\cftsecfont} 

\begin{document}

\tableofcontents

\chapter*{Introduction} %Without Chapter Number
\addcontentsline{toc}{chapter}{Introduction}

\lipsum[1]

\chapter{Cats vs Dogs} %With Chapter Number

\lipsum[2]  

\section{History}

\lipsum[3]

\end{document} 

在此处输入图片描述

相关内容