使用 tocloft 将章节编号与目录中的标题底线对齐

使用 tocloft 将章节编号与目录中的标题底线对齐

我修改了一个自定义目录,tocloft对此我非常满意(见下文)。问题是,当我的章节标题超过一行时。大章节编号与第一行对齐。我希望它与章节标题的第二行(或可能第三行)底线对齐,并且当添加“附录”行作为附录时。

实现这一点的一种方法是将章节标题放在parbox底部对齐的 中。这里的问题是章节标题不明确(即为#1),tocloft所以我不知道如何将其作为参数包含。另一种选择是将其minipage放在底部对齐的 中。然后可以重新定义\cftchapaftersnumb为包含\begin{minipage}。但是我应该把环境的结尾放在哪里呢?

\cftchapaftersnumb{\textsc{appendix}\par}请注意,附录中需要一个解决方案。(\\\par这里更好吗?)

\documentclass{book}
\usepackage{tocloft}
\usepackage{color}
\usepackage{etoolbox}
\usepackage[toc]{multitoc}

\def\marked{red}

\setlength\cftchapnumwidth{2.5em}


\renewcommand\cftchapleader{\hspace{1.5em plus 1fil}} % Ensure space between title and pagenumber.
\renewcommand\cftchappresnum{\upshape\liningfont\bfseries\LARGE\color{\marked}}
\renewcommand\cftchapafterpnum{\smallskip\nobreak}

\renewcommand\cftchapfont{\itshape\bfseries}
\renewcommand\cftchappagefont{\itshape\bfseries}

\preto\frontmatter{\addtocontents{toc}{
    \def\protect\cftchapfont{\normalfont}
    \def\protect\cftchappagefont{\normalfont}
    \protect\setlength\cftbeforechapskip{\smallskipamount}
  }}
\preto\mainmatter{\addtocontents{toc}{
  \def\protect\cftchapfont{\itshape\bfseries}
  \def\protect\cftchappagefont{\itshape\bfseries}
   \protect\setlength\cftbeforechapskip{\bigskipamount}
  }}
\preto\appendix{
  \addtocontents{toc}{\def\protect\cftchapfont{\normalfont}}
  \addtocontents{toc}{\def\protect\cftchappagefont{\normalfont}}
  \addtocontents{toc}{\def\protect\cftchapaftersnumb{\textsc{appendix}\par}}
}

\begin{document}

\tableofcontents

\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter with long name}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}

\appendix
\chapter{Some appendix}
\chapter{Some appendix}
\end{document}

在此处输入图片描述

答案1

您可以修补\chapter。直接尝试使用etoolbox失败,原因是我没有时间调查,因此我只是从 复制了定义book.cls并对其进行了自定义。 这.66\columnwidth是经验性的,您可能比我更清楚 tocloft 文档中到底说了什么。

编辑:我不确定你想要什么\appendix(如对之前答案的评论)但也许就是这样。

\documentclass{book}
\usepackage{tocloft}
\usepackage{color}
\usepackage{etoolbox}
\usepackage[toc]{multitoc}

\DeclareRobustCommand*\AppTocLine {}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
       \addcontentsline{toc}{chapter}%
       {\protect\numberline{\thechapter}\protect\parbox[b]{.66\columnwidth}{\AppTocLine #1}}%
                       \else
       \addcontentsline{toc}{chapter}{\protect\parbox[b]{.66\columnwidth}{\AppTocLine #1}}%
                       \fi
                    \else
       \addcontentsline{toc}{chapter}{\protect\parbox[b]{.66\columnwidth}{\AppTocLine #1}}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother



\def\marked{red}

\setlength\cftchapnumwidth{2.5em}


\renewcommand\cftchapleader{\hspace{1.5em plus 1fil}} % Ensure space between title and pagenumber.
%\renewcommand\cftchappresnum{\upshape\liningfont\bfseries\LARGE\color{\marked}}
% \liningfont ?
\renewcommand\cftchappresnum{\upshape\bfseries\LARGE\color{\marked}}
\renewcommand\cftchapafterpnum{\smallskip\nobreak}

\renewcommand\cftchapfont{\itshape\bfseries}
\renewcommand\cftchappagefont{\itshape\bfseries}

\preto\frontmatter{\addtocontents{toc}{
    \def\protect\cftchapfont{\normalfont}
    \def\protect\cftchappagefont{\normalfont}
    \protect\setlength\cftbeforechapskip{\smallskipamount}
  }}
\preto\mainmatter{\addtocontents{toc}{
  \def\protect\cftchapfont{\itshape\bfseries}
  \def\protect\cftchappagefont{\itshape\bfseries}
   \protect\setlength\cftbeforechapskip{\bigskipamount}
  }}
\preto\appendix{
  \addtocontents{toc}{\def\protect\cftchapfont{\normalfont}}
  \addtocontents{toc}{\def\protect\cftchappagefont{\normalfont}}
%  \addtocontents{toc}{\def\protect\cftchapaftersnumb{\textsc{appendix}\par}}
  \addtocontents{toc}{\def\AppTocLine{\textsc{appendix}\protect\par}}
}

\begin{document}

\tableofcontents

\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter with long name}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}

\appendix
\chapter{Some appendix}
\chapter{Some appendix}
\end{document}

在此处输入图片描述


第一个答案

\documentclass{book}
\usepackage{tocloft}
\usepackage{color}
\usepackage{etoolbox}
\usepackage[toc]{multitoc}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
       \addcontentsline{toc}{chapter}%
       {\protect\numberline{\thechapter}\protect\parbox[b]{.66\columnwidth}{#1}}%
                       \else
       \addcontentsline{toc}{chapter}{\protect\parbox[b]{.66\columnwidth}{#1}}%
                       \fi
                    \else
       \addcontentsline{toc}{chapter}{\protect\parbox[b]{.66\columnwidth}{#1}}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother



\def\marked{red}

\setlength\cftchapnumwidth{2.5em}


\renewcommand\cftchapleader{\hspace{1.5em plus 1fil}} % Ensure space between title and pagenumber.
%\renewcommand\cftchappresnum{\upshape\liningfont\bfseries\LARGE\color{\marked}}
% \liningfont ?
\renewcommand\cftchappresnum{\upshape\bfseries\LARGE\color{\marked}}
\renewcommand\cftchapafterpnum{\smallskip\nobreak}

\renewcommand\cftchapfont{\itshape\bfseries}
\renewcommand\cftchappagefont{\itshape\bfseries}

\preto\frontmatter{\addtocontents{toc}{
    \def\protect\cftchapfont{\normalfont}
    \def\protect\cftchappagefont{\normalfont}
    \protect\setlength\cftbeforechapskip{\smallskipamount}
  }}
\preto\mainmatter{\addtocontents{toc}{
  \def\protect\cftchapfont{\itshape\bfseries}
  \def\protect\cftchappagefont{\itshape\bfseries}
   \protect\setlength\cftbeforechapskip{\bigskipamount}
  }}
\preto\appendix{
  \addtocontents{toc}{\def\protect\cftchapfont{\normalfont}}
  \addtocontents{toc}{\def\protect\cftchappagefont{\normalfont}}
  \addtocontents{toc}{\def\protect\cftchapaftersnumb{\textsc{appendix}\par}}
}

\begin{document}

\tableofcontents

\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter with long name}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}
\chapter{Some chapter}

\appendix
\chapter{Some appendix}
\chapter{Some appendix}
\end{document}

在此处输入图片描述

相关内容