Latex,目录中没有章节编号但在实际章节标题中可见

9

我正在撰写一份文档,我不希望在目录中显示子章节编号(我希望在目录中显示子章节标题),但我希望在实际的文档标题中显示子章节编号。

这就是我想要的。

Table of Contents
1. Chapter One
 1.1 Section One
       SubSection One

Chapter 1
Chapter One
Some chapter text

1.1 Section One
Some text

1.1.1 Subsection One
Some text

我尝试使用\setcounter{secnumdepth}{1},但这会连同章节标题一起删除编号,因此我现在的情况是:

Table of Contents
1. Chapter One
 1.1 Section One
       SubSection One

Chapter 1
Chapter One
Some chapter text

1.1 Section One
Some text

Subsection One
Some text

在文档标题中获取章节编号但不在目录条目中获取,是否可能?

2个回答

5
在一个使用“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}}

我只想说这对我非常有帮助。谢谢! - Tyr

2
我不确定有没有编程的方法可以达到这个目的,但我知道你可以进入文档生成的*.toc文件,删除你想要抑制的章节的章节号参数。您可以将其更改为:
\contentsline {subsection}{\tocsubsection {}{1.1}{subsection one}}{1}

转化成这个:

\contentsline {subsection}{\tocsubsection {}{}{subsection one}}{1}

这将生成你想要的内容。请注意,每次编译tex源代码时,此内容都会重新生成。


谢谢,如果没有编程方式,我会将其作为备份保留。 - nbz
虽然您需要在 .toc 文件中进行更改后构建输出一次,才能看到更改的效果。 - nbz
很抱歉我不得不更改我的采纳答案。Sparshong的解决方案非常好,以编程方式完成得非常干净。但在那之前,你的解决方案也很好地起到了快速修复的作用! - nbz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接