我准备制作一个较长的演示文档。在安装为目录中的section编号的模板之后,由于section的编号是从1
开始连续自增,所以没什么问题。
为了实际使用的便利,我准备将整体内容分为两个beamer文档。然而,两份文档都会从1
开始为section重新编号——这是我不希望看到的。
在下面这份实际上是第二份演示文稿中,我希望目录中出现的第一个section的编号为3
,但是,我在导言区使用\addtocounter{section}{2}
却并不奏效。
\documentclass{beamer}
\setbeamertemplate{section in toc}[sections numbered]
\addtocounter{section}{2}
\begin{document}
\frame{
\contentsname
\tableofcontents
}
\section{sectionthree}
\frame{\frametitle{sectionthree}some text}
\section{sectionfour}
\frame{\frametitle{sectionfour}some text}
\end{document}
在...\texmf-dist\tex\latex\beamer\beamerbasesection.sty
文件的第176行附近,似乎提供了beamer的section的相关定义。有如下表述:
\beamer@tocsectionnumber=0\relax
虽然不知道其含义,但是,只要将其放在导言区,并重新赋值,即可将第二份演示文稿的目录中的section的起始编号设置为问题所需。即,在导言区添加:
\makeatletter
\beamer@tocsectionnumber=2\relax
\makeatother
即可得到:
在下面这则article
文档类的示例文档中,你将得到一个section
条目从3
开始编号的目录:
% mwe-article.tex
\documentclass{article}
\addtocounter{section}{2}
\begin{document}
\tableofcontents
\section{section one} text one.
\section*{section two} text two.
\section{section three} text three.
\end{document}
在下面这则beamer
文档类的示例文档中,你将得到一个section
条目从1
开始编号的目录:
% mwe-beamer.tex
\documentclass{beamer}
\setbeamertemplate{section in toc}[sections numbered]
\addtocounter{section}{2}
\begin{document}
\frame{ \frametitle{\contentsname} \tableofcontents }
\section{section one}
\frame{ \frametitle{section one} text one. }
\section*{section two}
\frame{ \frametitle{section two} text two. }
\section{section three}
\frame{ \frametitle{section three} text three. }
\end{document}
尽管两份示例文档都在导言区使用\addtocounter{section}{2}
,但是预期效果只在artile
文档类中实现,而在beamer
文档类中没有实现。出现这种情况的原因是,虽然这两个文档类都定义了名字相同的\section
命令及其星号版本,但这两个文档类对\section
命令及其星号版本的定义方式不同。更具体地说,是两个文档类各自的\section
命令及其星号版本对写入目录的要求不同。
在beamer.cls
中,有以下代码:
%% line 79
\newcount\beamer@tocsectionnumber
其含义是,新分配一个名为\beamer@tocsectionnumber
且缺省值为0
的count型寄存器
。count型寄存器
能够储存一个数值的浮点数部分和整数部分。要对该寄存器赋值,可以使用<count型寄存器>=<数值>
。要打印它的当前数值,可以使用\the<count型寄存器>
。当浮点数部分不存在时,只会打印整数部分。在TeX
中,可以使用\advance <count型寄存器> by <数值>
,来使<count型寄存器>
内部储存的数值加或减<数值>。通过这种方式,这类寄存器可以被用于计数,所以可以也可以叫它“计数器”。
现在,要找到beamer
目录的section
条目编号的具体症结,需要查看.toc
文件:
% mwe-article.toc
\contentsline {section}{\numberline {3}section one}{1}{}%
\contentsline {section}{\numberline {4}section three}{1}{}%
% mwe-beamer.toc
\beamer@sectionintoc {3}{section one}{2}{0}{1}
\beamer@sectionintoc {5}{section three}{4}{0}{2}
在mwe-article.toc
中,第一则目录条目的构成是(见source2e.pdf
的\contentsline
条目):
\contentsline
{<该标题的标题类型:section>}
{\numberline {<该标题在正文中的计数器section的当前值:3>}<该标题的正文内容:section one>}
{<该标题所在页面的计数器page的当前值:1>}
{<该标题的超链接锚点:>}`%
在mwe-beamer.toc
中,第一则目录条目的构成是(见beamerbasesection.sty
的代码行\addtocontents{toc}{\protect\beamer@sectionintoc{...}}
中省略号处的内容):
\beamer@sectionintoc
{<该标题在正文中的计数器section的当前值:3>}
{<该标题的正文内容:section one>}
{<该标题所在的页面的计数器page的当前值:2>}
{<该标题所在的部分的计数器part的当前值:0>}
{<该标题在目录中的计数器\beamer@tocsectionnumber的当前值:1>}
从中可见,命令\section
及其星号版本,在写入目录时,其在beamer
文档类中的行为与在标准文档类中的行为不同。
在mwe-article.toc
中,写入目录的是该标题在正文中的计数器section
的值,而在mwe-beamer.toc
中,写入目录的是该标题在目录中的计数器\beamer@tocsectionnumber
的值。也就是说,在beamer
中,由\section
命令及其星号版本给出的标题,其在正文中和目录中的编号采用的是不同的编号方法。
那么二者是否存在数值上的依赖关系?我的答案是:我没找到,所以我认为目前二者不存在数值依赖关系。
那么二者具有怎么样的关系?答案是:在beamer
中,
\beamer@tocsectionnumber
只给由\section
给出的标题从1
开始编号——换言之,它是一个“纯粹”且全局连续的计数器。所以在mwe-beamer.toc
中,我们看到的两个条目的第5个参数,是连续的从1
开始的编号1
和2
——因为\addtocounter{section}{2}
没有对计数器\beamer@tocsectionnumber
产生影响,所以服从默认情况,从1
开始编号;而“连续”是显然的,因为计数器\beamer@tocsectionnumber
只给由\section
给出的标题编号。section
既会给由\section
给出的标题编号,也会给由\section*
给出的标题编号——换言之,它是一个“不纯粹”且全局连续的计数器。由于\addtocounter{section}{2}
对计数器section
产生影响,所以从3
开始编号。所以在mwe-beamer.toc
中,我们看到的两个条目的第1个参数,是连续的从3
开始的编号3
和5
——遇到\section*{section two}
时,计数器section为其编号为4
,但beamer
默认不让由\section*
给出的标题进入目录,所以\section*{section two}
没有被写入.toc
文件,不过本质上3,4,5
是连续的。如果使\section*{section two}
进入目录,我们将看到:% (如果允许星号标题进入目录的)mwe-beamer.toc
\beamer@sectionintoc {3}{section one}{2}{0}{1}
\beamer@sectionintoc {4}{section two}{3}{0}{}%%这是我的预测和想象,不一定准确
\beamer@sectionintoc {5}{section three}{4}{0}{2}
最后,解决办法是:
% way 1:
\makeatletter
\beamer@tocsectionnumber=2\relax
\makeatother
% way 2:
\makeatletter
\advance\beamer@tocsectionnumber by 2\relax
\makeatother
https://github.com/josephwright/beamer/blob/5616b785a9dff1024fa31cc3a3bd11f8d9b40aaf/base/beamerbasesection.sty#L172-L176C1
@u70550 beamer里的section编号在增加到底是不是依靠计数器啊?太神秘了~