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

发布于 2024-09-28 14:51:28 字数 581 浏览 2 评论 0原文

我正在编写一个文档,我不希望在目录中显示小节编号(我希望小节标题在目录中可见),但我希望在实际文档标题中显示小节编号。

这就是我想要的,

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 条目中获取节号?

I am writing a document where I do not want subsection numbering to show in the TOC (I want the subsection heading visible in the TOC) but I want the subsection numbering to show in the actual document heading.

This is what I want

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

I tried using \setcounter{secnumdepth}{1} but this removes the number even from the section heading so what I have is,

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

Is it possible to get the section number in the document heading but not in the TOC entry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-10-05 14:51:28

在乳胶示例中(使用“article”类),我在 .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

最后注意:这将使节和子节的标题从相同的水平位置开始,因为没有要显示的数字。如果您想要更多填充,您可以将 \quad 添加到 \numberline 的新定义中,或者甚至使用仅包含 #1 的原始定义删除:

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

In a latex example (using the "article" class), I get this in the .toc file:

\contentsline {section}{\numberline {1}test section without number}{1}{section.1}

The important part here is the \numberline macro. Redefining it to something empty like

\def\numberline#1{}

will remove all numberings in the toc and not elsewhere.
If you get something like \tocsubsection instead in the .toc (see other answer), then you can probably do something like:

\let\oldtocsubsection=\tocsubsection
\def\tocsubsection#1#2#3{\oldtocsubsection{#1}{}{#3}}

However, this removes all numbers in the table of contents. If you want to control at which level the numbering disappear, the \contentsline macro expands to different macros depending on the context, e.g., \l@section. Those macros in turn use the generic \@dottedtocline macro. This is the one you need to modify, in which we will conditionally redefine \numberline.

To have control on the depth at which to stop displaying numbers, let us define a new counter:

\newcounter{sectocnonumdepth}
\setcounter{sectocnonumdepth}{2}

Then the conditional redefinition will be following line (extracted from the code for more readability).

 \ifnum #1>\c@sectocnonumdepth \def\numberline##1{}\fi%

I simply copy-pasted the definition of \@dottedtocline from the latex.ltx source file, and added the check inside. Here is the code for the whole example:

\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

Final note: this will make the title of section and subsection to start at the same horizontal position, since there is no number to display. If you want more padding, you can for instance add \quad to the new definition of \numberline, or to even use the original definition with just the #1 removed:

\def\numberline##1{\hb@xt@\@tempdima{\hfil}}
┊风居住的梦幻卍 2024-10-05 14:51:28

我不确定执行此操作的编程方式,但我确实知道您可以进入为文档生成的 *.toc 文件,并删除要抑制的部分的节号参数。

您可以将其更改为:

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

更改为:

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

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

I'm not sure of a programmatic way of doing this, but I do know that you can go into the generated *.toc file for your document and remove the section number argument for the section that you want to suppress.

You can change this:

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

To this:

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

Which will generate what you want. Watch out, this gets regenerated each time you compile your tex source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文