在一个使用“article”类的latex示例中,我在.toc文件中得到了这个:
\contentsline {section}{\numberline {1}test section without number}{1}{section.1}
这里重要的部分是
\numberline
宏。将其重新定义为空,例如:
\def\numberline#1{}
这将从目录中删除所有编号,而不是其他地方。
如果在.toc文件中出现像\tocsubsection
这样的内容(请参见其他答案),那么您可能可以执行以下操作:
\let\oldtocsubsection=\tocsubsection
\def\tocsubsection#1#2#3{\oldtocsubsection{#1}{}{#3}}
然而,这会移除目录中的
所有数字。如果您想要控制数字消失的级别,
\contentsline
宏将根据上下文扩展为不同的宏,例如
\l@section
。这些宏又使用通用的
\@dottedtocline
宏。这是您需要修改的宏,我们将有条件地重新定义
\numberline
。
为了控制停止显示数字的深度,让我们定义一个新计数器:
\newcounter{sectocnonumdepth}
\setcounter{sectocnonumdepth}{2}
接下来的条件重新定义将遵循以下行(为了更好的可读性从代码中提取)。
\ifnum #1>\c@sectocnonumdepth \def\numberline##1{}\fi%
我只是从latex.ltx
源文件中复制并粘贴了\@dottedtocline
的定义,并在其中添加了检查。以下是整个示例的代码:
\newcounter{sectocnonumdepth}
\setcounter{sectocnonumdepth}{2}
\makeatletter
\def\@dottedtocline#1#2#3#4#5{
\ifnum #1>\c@tocdepth \else
\vskip \z@ \@plus.2\p@
{\ifnum #1>\c@sectocnonumdepth \def\numberline##1{}\fi
\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
\parindent #2\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima #3\relax
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{#4}\nobreak
\leaders\hbox{$\m@th
\mkern \@dotsep mu\hbox{.}\mkern \@dotsep
mu$}\hfill
\nobreak
\hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}
\par}
\fi}
\makeatother
最后提醒:这将使章节和小节的标题从同一水平位置开始,因为没有数字显示。如果您需要更多填充,可以在
\numberline
的新定义中添加例如
\quad
,或者甚至只使用去除了
#1
的原始定义:
\def\numberline##1{\hb@xt@\@tempdima{\hfil}}