控制目录元素之间的距离

控制目录元素之间的距离

我想了解以下示例中的长度是dimension #1多少。#2

另外,图中蓝色尺寸等于的长度吗\parskip

我怎样才能用 titletoc 控制尺寸#1#2?我在 MWE 中创建了两个新的长度,我怎样才能将它们作为\titlecontents{section}[]{}{}{}{}[]和的正确参数传递\titlecontents{chapter}[]{}{}{}{}[]

在此处输入图片描述

% !TEX TS-program = xelatex
\documentclass{book}
\usepackage{fontspec}
\setlength{\parskip}{10pt}
\setlength{\parindent}{0pt}

\usepackage{titletoc}
  \dottedcontents{section}[3.8em]{}{2.3em}{0pc}
  \dottedcontents{subsection}[6.1em]{}{3.2em}{0pc}

\newlength\dimone \setlength\dimone{8pt}  % << new length
\newlength\dimtwo \setlength\dimtwo{14pt} % << new length

\usepackage[english]{babel}
\begin{document}
\tableofcontents
\chapter{First chapter}
\section{one}
\subsection{a}
\subsection{b}
\subsection{c}
\section{two}
\section{three}
\section*{Summary}
\addcontentsline{toc}{section}{Summary}
\section*{Exercises}
\addcontentsline{toc}{section}{Exercises}
\chapter{second chapter}
\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}
\section{one}
\section{two}
\section{three}
\end{document}

答案1

John Kormylo 在他的回答中已经解决了您问题的第一部分,因此我只会回答第二部分。

首先,我们希望删除由 添加的额外空间\parskip(通常不应手动更改)。可以使用软件包来完成此操作parskip,因为它就是为此而设计的,但此软件包不会修补chapters,因此我们将自己执行此操作。

使用以下代码,您将获得章节标题和目录条目之前和之后的原始垂直间距:

% !TEX TS-program = xelatex
\documentclass{book}
\usepackage{fontspec}


\usepackage[skip=10pt,parfill]{parskip}
\makeatletter
\patchcmd\@makeschapterhead{\parindent\z@}{\parskip\z@\parindent\z@}{}{}
\patchcmd\@makechapterhead{\parindent\z@}{\parskip\z@\parindent\z@}{}{}
\patchcmd\@starttoc{\vskip-\lastskip\vskip\skip@}{}{}{}
\makeatother

\usepackage{titletoc}
\dottedcontents{section}[3.8em]{}{2.3em}{0pc}
\dottedcontents{subsection}[6.1em]{}{3.2em}{0pc}

\newlength\dimone \setlength\dimone{8pt}  % << new length
\newlength\dimtwo \setlength\dimtwo{14pt} % << new length

\usepackage[english]{babel}


\begin{document}
    \tableofcontents
    \chapter{First chapter\the\parskip}
    \section{one}
    \subsection{a}
    \subsection{b}
    \subsection{c}
    \section{two}
    \section{three}
    \section*{Summary}
    \addcontentsline{toc}{section}{Summary}
    \section*{Exercises}
    \addcontentsline{toc}{section}{Exercises}
    \chapter{second chapter}
    \section*{Introduction}
    \addcontentsline{toc}{section}{Introduction}
    \section{one}
    \section{two}
    \section{three}
\end{document}

现在我们已经检索了原始间距,我们可以开始根据我们的需要进行修改。

% !TEX TS-program = xelatex
\documentclass{book}
\usepackage{fontspec}


\usepackage[skip=10pt,parfill]{parskip}
\makeatletter
\patchcmd\@makeschapterhead{\parindent\z@}{\parskip\z@\parindent\z@}{}{}
\patchcmd\@makechapterhead{\parindent\z@}{\parskip\z@\parindent\z@}{}{}
\patchcmd\@starttoc{\vskip-\lastskip\vskip\skip@}{\vskip-\lastskip\vskip\dimexpr\dimone-\dimtwo\relax}{}{}
\makeatother

\usepackage{titletoc}
\dottedcontents{chapter}[1.5em]{\addvspace\dimtwo}{1em}{0pc}
\dottedcontents{section}[3.8em]{}{2.3em}{0pc}
\dottedcontents{subsection}[6.1em]{}{3.2em}{0pc}


\newlength\dimone \setlength\dimone{8pt}  % << new length
\newlength\dimtwo \setlength\dimtwo{14pt} % << new length

\usepackage[english]{babel}


\begin{document}
    \tableofcontents
    \chapter{First chapter\the\parskip}
    \section{one}
    \subsection{a}
    \subsection{b}
    \subsection{c}
    \section{two}
    \section{three}
    \section*{Summary}
    \addcontentsline{toc}{section}{Summary}
    \section*{Exercises}
    \addcontentsline{toc}{section}{Exercises}
    \chapter{second chapter}
    \section*{Introduction}
    \addcontentsline{toc}{section}{Introduction}
    \section{one}
    \section{two}
    \section{three}
\end{document}

答案2

这里的目标不是改变间距,而是验证它。请注意,1em 取决于字体大小\chapskip

\documentclass{report}
\usepackage{fontspec}
\setlength{\parskip}{10pt}
\setlength{\parindent}{0pt}

\newlength{\chapskip}
\setlength{\chapskip}{1em}

\usepackage[english]{babel}
\begin{document}
\renewcommand{\contentsname}{Contents\hfill\llap{\rule{\linewidth}{0.5pt}\smash{\rule[-40pt]{1pt}{40pt}}}}
\tableofcontents

\chapter{First chapter\hfill\strut\llap{\rule{\linewidth}{0.5pt}\smash{\rule{1pt}{\dimexpr \baselineskip+\chapskip+\parskip}}}}
\section{one\hfill\llap{\rule{0.5\textwidth}{0.5pt}\smash{\rule{1pt}{\dimexpr \baselineskip+\parskip}}}}
\subsection{a\hfill\llap{\rule{0.5\textwidth}{0.5pt}\smash{\rule{1pt}{\dimexpr \baselineskip+\parskip}}}}
\subsection{b}
\subsection{c}
\section{two}
\section{three}
\section*{Summary}
\addcontentsline{toc}{section}{Summary}
\section*{Exercises}
\addcontentsline{toc}{section}{Exercises\hfill\llap{\rule{0.5\textwidth}{0.5pt}}}
\chapter{second chapter\hfill\strut\llap{\rule{\linewidth}{0.5pt}\smash{\rule{1pt}{\dimexpr \baselineskip+\chapskip+\parskip}}}}
\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}
\section{one}
\section{two}
\section{three}

\end{document}

演示

相关内容