Latex,TOC中没有节号,但在实际节标题中可见

Latex,TOC中没有节号,但在实际节标题中可见,latex,Latex,我正在编写一份文件,其中我不希望在TOC中显示小节编号(我希望在TOC中显示小节标题),但我希望在实际文件标题中显示小节编号 这就是我想要的 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 我尝试使用\

我正在编写一份文件,其中我不希望在TOC中显示小节编号(我希望在TOC中显示小节标题),但我希望在实际文件标题中显示小节编号

这就是我想要的

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

是否可以在文档标题中而不是在TOC条目中获取节号?

我不确定是否有一种编程方式可以实现这一点,但我知道您可以进入为文档生成的*.TOC文件,并删除要禁止显示的节的节号参数

您可以更改此设置:

\contentsline {subsection}{\tocsubsection {}{1.1}{subsection one}}{1}
为此:

\contentsline {subsection}{\tocsubsection {}{}{subsection one}}{1}
这将产生你想要的。请注意,每次编译tex源代码时都会重新生成它。

在一个latex示例中(使用“article”类),我在.toc文件中得到:

\contentsline {section}{\numberline {1}test section without number}{1}{section.1}
这里的重要部分是
\numberline
宏。将它重新定义为空的东西,比如

\def\numberline#1{}
将删除toc中的所有编号,而不是其他位置。 如果您在.toc中得到类似于
\toc
的内容(请参见其他答案),那么您可能可以执行以下操作:

\let\oldtocsubsection=\tocsubsection
\def\tocsubsection#1#2#3{\oldtocsubsection{#1}{}{#3}}
但是,这将删除目录中的所有数字。如果要控制编号在哪个级别消失,则
\contentsline
宏将根据上下文扩展为不同的宏,例如
\l@section
。这些宏依次使用通用的
\@dottedtocline
宏。这是您需要修改的,我们将在其中有条件地重新定义
\numberline

为了控制停止显示数字的深度,让我们定义一个新计数器:

\newcounter{sectocnonumdepth}
\setcounter{sectocnonumdepth}{2}
然后,条件重新定义将跟随第行(从代码中提取以提高可读性)

我只是从
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
最后注意:这将使节和小节的标题从同一水平位置开始,因为没有数字显示。如果需要更多的填充,例如,您可以将
\quad
添加到
\numberline
的新定义中,或者甚至在删除
\1
的情况下使用原始定义:

\def\numberline##1{\hb@xt@\@tempdima{\hfil}}

谢谢,如果没有任何编程方法,我会将此作为备份。虽然在.toc文件中进行更改后确实需要生成一次输出以查看更改的效果。很抱歉,我不得不更改我接受的答案。Sparshong的工作非常好,并且以一种程序化的方式做得非常干净。但在那之前,你的快速修复也很好!我只想说这对我非常有帮助。非常感谢。
\def\numberline##1{\hb@xt@\@tempdima{\hfil}}