\hangindent 违反了目录中点的细长顺序

\hangindent 违反了目录中点的细长顺序

我已经编写了\l@appendix用于格式化目录中附录条目的命令。这里有一个最小的测试示例:

\documentclass[14pt]{extreport}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
\usepackage[hidelinks]{hyperref}

\makeatletter
\newif\if@mainmatter\@mainmattertrue
\newcommand\mainmatter{\@mainmattertrue}
\newcommand\frontmatter{\@mainmatterfalse}
\newcommand\aftermatter{\@mainmatterfalse}

\def\@chapter[#1]#2{\ifnum\c@secnumdepth>\m@ne%
                  \if@mainmatter%                 
                     \refstepcounter{chapter}%
                     \addcontentsline{toc}{chapter}%
  {\protect\numberline\thechapter\texorpdfstring{\MakeUppercase{#1}}{#1}}%
                  \else%
                     \refstepcounter{chapter}%
                     \phantomsection
                     \addcontentsline{toc}{appendix}%
    {\texorpdfstring{\@chapapp\space\protect\numberline\thechapter\space#1}        {\@chapapp\space\thechapter\space#1}}%
                  \fi%
                \else%
                   \addcontentsline{toc}{chapter}{#1}%
                \fi%
                \chaptermark{#1}%
                \addtocontents{lof}{\protect\addvspace{10\p@}}%
                \addtocontents{lot}{\protect\addvspace{10\p@}}%
                  \@makechapterhead{#2}%
                  \@afterheading}

\renewcommand\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -    \rightskip
     \parindent 0.0em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.0em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
   mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}
     \par}%
  \fi}

\newcommand\l@appendix[2]{%
  \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -\rightskip
     \parindent 0.0em\relax \hangindent 4.3em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.5em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
       mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}
     \par}%
  \fi}

\renewcommand\appendix{\par%
  \aftermatter%
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}%
}\makeatother

\begin{document}
\tableofcontents
\chapter{First chapter}
\chapter{Second chapter}
\appendix
\chapter{Appendix with very long heading for test describing issue}
\chapter{Appendix}
\end{document}

它可以工作,但是\hangindent当标题超出单行时,命令会破坏目录中点的细长顺序。

在此处输入图片描述

我该如何修复它而不破坏以前的缩进?

答案1

在此处输入图片描述

悬挂缩进移动了包含框,因此失去了领导者的对齐。

在这里我强制重复的引线框为已知宽度(.7em),并强制悬挂缩进为该宽度的倍数(4.2em 而不是 4.3em)。

\documentclass[14pt]{extreport}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
\usepackage[hidelinks]{hyperref}

\makeatletter
\newif\if@mainmatter\@mainmattertrue
\newcommand\mainmatter{\@mainmattertrue}
\newcommand\frontmatter{\@mainmatterfalse}
\newcommand\aftermatter{\@mainmatterfalse}

\def\@chapter[#1]#2{\ifnum\c@secnumdepth>\m@ne%
                  \if@mainmatter%                 
                     \refstepcounter{chapter}%
                     \addcontentsline{toc}{chapter}%
  {\protect\numberline\thechapter\texorpdfstring{\MakeUppercase{#1}}{#1}}%
                  \else%
                     \refstepcounter{chapter}%
                     \phantomsection
                     \addcontentsline{toc}{appendix}%
    {\texorpdfstring{\@chapapp\space\protect\numberline\thechapter\space#1}        {\@chapapp\space\thechapter\space#1}}%
                  \fi%
                \else%
                   \addcontentsline{toc}{chapter}{#1}%
                \fi%
                \chaptermark{#1}%
                \addtocontents{lof}{\protect\addvspace{10\p@}}%
                \addtocontents{lot}{\protect\addvspace{10\p@}}%
                  \@makechapterhead{#2}%
                  \@afterheading}

\renewcommand\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -    \rightskip
     \parindent 0.0em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.0em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox to .7em{\hss.\hss}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}
     \par}%
  \fi}

\newcommand\l@appendix[2]{%
  \ifnum \c@tocdepth >\m@ne
    \vskip \z@ \@plus.2\p@
    {\leftskip 0.0em\relax \rightskip \@tocrmarg plus 15em \parfillskip -\rightskip
     \parindent 0.0em\relax \hangindent 4.2em\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima 1.5em\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#1}\nobreak
     \leaders\hbox to .7em{\hss.\hss}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil \normalcolor #2}
     \par}%
  \fi}

\renewcommand\appendix{\par%
  \aftermatter%
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}%
}\makeatother

\begin{document}

\tableofcontents
\chapter{First chapter}
\chapter{Second chapter}
\appendix
\chapter{Appendix with very long heading for test describing issue}
\chapter{Appendix}
\end{document}

相关内容