如何使用 tocloft 模拟 titletoc

如何使用 tocloft 模拟 titletoc

我一直在使用,titletoc直到我意识到它与 存在一些不兼容性hyperref。问题是,titletoc可以以更简单的方式做同样的事情,更少的。

\titlecontents*{subsection}[5.4em]
{\small}
{\thecontentslabel~ }
{}
{}
[.~\textbullet\ ]
[.]

此部分将小节作为段落(带有节号,没有页码)。我意识到问题在于tocloft目录中条目使用的伪代码

{\cftXfont {\cftXpresnum SNUM\cftXaftersnum\hfil} \cftXaftersnumb TITLE}%
{\cftXleader}{\cftXpagefont PAGE}\cftXafterpnum\par

我的意思是,最后一个命令\par使得无法在同一文本行中留下两个子节条目。有什么想法吗?另一个有趣的解决方案是使用etoolbox,该包中的一些示例非常棒,但我手头没有。

这是一个 MWE:

\documentclass{book}

\usepackage{titletoc}
\titlecontents*{subsection}[5.4em]{\small}
{\thecontentslabel~ }{}{}[.~\textbullet\ ][.]

\begin{document}

\tableofcontents

\chapter{Test Chapter 1}
\section{Test Section 1.1}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section 1.2}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter 2}
\section{Test Section 2.1}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section 2.2}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

答案1

一种可能的解决方案\l@subsection是;重新定义以抑制页码,然后etoolbox使用该包进行修补\section\subsection:每个小节(第一个除外)\unskip.~\textbullet\在目录中的相应条目之前添加,并且每个小节都\section将最后一个点(\unskip.)添加到目录中每个小节的最后一个小节:

\documentclass{article}          
\usepackage{etoolbox}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-3em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \parshape 2  0em \rightmargintoc \parindent \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
    \else\protect\addcontentsline{toc}{subsection}{\unskip.~\textbullet\ }
  \fi}{}{}{}

\pretocmd{\section}{%
  \ifnum\value{subsection}>0\relax\addcontentsline{toc}{subsection}{\unskip.}
    \else\fi%
  \addtocontents{toc}{\par}}{}{}

\AtEndDocument{\ifnum\value{subsection}>0\relax\addcontentsline{toc}{subsection}{\unskip.}\else\fi}

\begin{document}

\tableofcontents
\section{Test Section One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}

\section{Test Section Two}

\section{Test Section Three}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

在此处输入图片描述

book评论中要求对文档类进行修改:

\documentclass{book}
\usepackage{etoolbox}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-5em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \setlength\@tempdima{3em}%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
  \else\protect\addcontentsline{toc}{subsection}{\unskip.~\textbullet\ }
  \fi}{}{}{}

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addcontentsline{toc}{subsection}{\unskip.}\addtocontents{toc}{\par}
\else\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

生成的 ToC 的图像:

在此处输入图片描述

如果hyperref已加载,则必须采取一些额外的预防措施:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{hyperref}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-5em}

\makeatletter
\renewcommand*\l@subsection[2]{%
  \setlength\@tempdima{3em}%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc%
  \leavevmode#1}
\makeatother

\pretocmd{\subsection}{%
  \ifnum\value{subsection}=0\relax
  \else\protect\addcontentsline{toc}{subsection}{\texorpdfstring{\unskip.~\textbullet\ }{}}
  \fi}{}{}{}

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addcontentsline{toc}{subsection}{\texorpdfstring{\unskip.}{}}\addtocontents{toc}{\par}
\else\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

现在有一个改进的版本:

\documentclass{book}
\usepackage{etoolbox}
\usepackage[colorlinks=true,linkcolor=cyan]{hyperref}

\newlength\rightmargintoc
\setlength\rightmargintoc{\linewidth}
\addtolength\rightmargintoc{-7em}

\makeatletter
\def\subsectocline#1#2#3#4#5{%
\parshape 2  2.5em \rightmargintoc \dimexpr\parindent+2.5em\relax \rightmargintoc
\@tempdima#3
\ifdim\lastskip=1sp\relax\ \textbullet~\else\fi{\small#4.}\hskip1sp%
}
\renewcommand*\l@subsection{\subsectocline{1}{0em}{3em}}
\makeatother

\pretocmd{\chapter}{\addtocontents{toc}{\par}}{}{}
\pretocmd{\section}{\addtocontents{toc}{\par}}{}{}

\AtEndDocument{%
\ifnum\value{subsection}>0\relax
  \addtocontents{toc}{\par}
\fi}

\begin{document}
\tableofcontents

\chapter{Test Chapter One}
\section{Test Section One One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}
\subsection{Third Test Subsection}
\subsection{Fourth Test Subsection}
\section{Test Section One Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Two}
\section{Test Section Two One}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\chapter{Test Chapter Three}
\section{Test Section Three One}
\section{Test Section Three Two}
\subsection{First Test Subsection}
\subsection{Second Test Subsection}

\end{document}

最终的目录如下:

在此处输入图片描述

相关内容