带标记的部分目录条目中的距离

带标记的部分目录条目中的距离

我尝试通过重新定义\l@section和定义\@section宏来定制部分的目录条目。

\sectiontitlemark我的目的是在正文和目录中的节计数器前添加不同的标记( )。我实现了这个目标。但是在目录中,节计数器和其主体之间存在“空格”问题。

此“空间”只是文本从页面左边框的偏移,与节计数器(和\sectiontitlemark)的存在无关,并且需要根据\sectiontitlemark+ \thesection+ 点的总宽度进行计算。但总宽度取决于 的宽度\sectiontitlemark。在 MWE 中,它由 控制\setlength\@tempdima{2.7em}

平均能量损失

\documentclass[]{book}
\usepackage{lipsum}
\makeatletter


\renewcommand*\l@section[2]{%
    \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 0.1em \@plus\p@
    \setlength\@tempdima{2.7em} % Space between number and section title need to be calculated
    \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \leavevmode
    \advance\leftskip\@tempdima
    \hskip -\leftskip
    #1\leaders\hbox to .5em{\hss.\hss}\hfil \nobreak\hb@xt@\@pnumwidth{\hss  #2}\par
    \penalty\@highpenalty%
    \endgroup
    \fi}

\renewcommand\section{\global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@section\empty}
\def\@section[#1]#2{\refstepcounter{section}%
                    \addcontentsline{toc}{section}{\protect\numberline{\sectiontitlemark~\thesection.}#1}%
                    \sectionmark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \@makesectionhead{#2}%
                    \@afterheading
                    }
\def\@makesectionhead#1{%
  \vspace*{10\p@}%
  {\parindent \z@ \raggedright \normalfont
        \normalfont\large\bfseries \sectiontitlemark~\thesection.\hspace{1ex}#1\par\nobreak
    \vskip 10\p@
  }}
\def\@ssection#1{\refstepcounter{section}%
                 \if@twocolumn
                   \@topnewpage[\@makessectionhead{#1}]%
                 \else
                   \@makessectionhead{#1}%
                   \@afterheading
                   \sectionmark{#1}%
                 \fi}
\def\@makessectionhead#1{%
  \vspace*{10\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \normalfont\large\bfseries  #1\par\nobreak
    \vskip 10\p@
  }}

\makeatother
\begin{document}
\tableofcontents
\def\sectiontitlemark{\S}
\section{section One may be very very very and very very long and wrap to next line}
\lipsum[1]
\def\sectiontitlemark{$\to$}
\section{section Two may be also very very very and very very long and wrap to next line}
\lipsum[1]
\end{document}

在此处输入图片描述

答案1

我找到了解决方案!主要思想是通过宏将带有标记的计数器长度保存到\@sechead@len-file 中toc,然后与 一起使用。\addtocontents{toc}{\setlength{\sechead@len}{\the\@sechead@len}}\@section\renewcommand*\l@section{\@dottedtocline{2}{3em}{\sechead@len}}

\documentclass[]{book}
\usepackage{lipsum}
\makeatletter
\def\sectiontitlemark{}
\newlength{\sechead@len}
\newlength{\@sechead@len}

\newif{\ifappendix}

\renewcommand*\l@section{\@dottedtocline{2}{3em}{\sechead@len}}


\renewcommand\section{\global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@section\empty}
\def\@section[#1]#2{\refstepcounter{section}%
                    \settowidth{\@sechead@len}{\sectiontitlemark~\thesection.\space}
                    \addtocontents{toc}{\setlength{\sechead@len}{\the\@sechead@len}}
                    \addcontentsline{toc}{section}{\protect\numberline{\sectiontitlemark~\thesection.}#1}%
                    \sectionmark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \@makesectionhead{#2}%
                    \@afterheading
                    }
\def\@makesectionhead#1{%
  \vspace*{10\p@}%
  {\parindent \z@ \raggedright \normalfont
        \normalfont\large\bfseries \sectiontitlemark~\thesection.\hspace{1ex}#1\par\nobreak
    \vskip 10\p@
  }}
\def\@ssection#1{\refstepcounter{section}%
                 \if@twocolumn
                   \@topnewpage[\@makessectionhead{#1}]%
                 \else
                   \@makessectionhead{#1}%
                   \@afterheading
                   \sectionmark{#1}%
                 \fi}
\def\@makessectionhead#1{%
  \vspace*{10\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \normalfont\large\bfseries  #1\par\nobreak
    \vskip 10\p@
  }}

\makeatother
\begin{document}
\tableofcontents
\def\sectiontitlemark{\S}
\section{section One may be very very very and very very long and wrap to next line}
\lipsum[1]


\def\sectiontitlemark{$\to$}
\section{section Two may be also very very very and very very long and wrap to next line}
\lipsum[1]

\def\sectiontitlemark{ ------------1 }
\section{section Two may be also very very very and very very long and wrap to next line}
\lipsum[1]
\end{document}

有趣的是,我在这里描述问题后经过几天的搜索找到了解决方案。

也许有人会找到更简洁的解决方案。

在此处输入图片描述

相关内容