\insertsectionnumber 和 \inserttocsectionnumber 之间的区别

\insertsectionnumber 和 \inserttocsectionnumber 之间的区别

以下投影机代码

\documentclass{beamer}

\setbeamertemplate{section in toc}[sections numbered]

\begin{document}

\frame{\tableofcontents}

\section{section one}
\frame{\insertsectionnumber\quad\insertsection}

\section*{section two}
\frame{\insertsectionnumber\quad\insertsection}

\section{section three}
\frame{\insertsectionnumber\quad\insertsection}

\end{document}

得到以下结果:

在此处输入图片描述

我知道\tableofcontents调用\inserttocsectionnumber命令。当有一些\section*命令时,\inserttocsectionnumber\insertsectionnumber给出不同的结果:2第三节3 第三节。 为什么?

答案1

这是来自的一些代码beamerbasesection.sty,带有一些注释

与 等标准类相反,\section和之间\section*在计数器步进方面没有区别。因此,即使通过命令也可以增加计数器。sectionarticlesection\section*

这是个 bug 吗?不,我认为,这样做更容易记录各种复杂的功能beamer

\newcommand<>{\section}{\alt#1{\@ifnextchar[\beamer@section\beamer@@section}{\beamer@secgobble}}

\def\beamer@@section{\@ifnextchar*\beamer@@ssection\beamer@@@section}
\long\def\beamer@@ssection*#1{\beamer@section[{#1}]{}}  %% starred section
\long\def\beamer@@@section#1{\beamer@section[{#1}]{#1}}
\long\def\beamer@section[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{section}%  used in any case of \section or \section* 
    \beamer@ifempty{#2}%
    {\long\def\secname{#1}\long\def\lastsection{#1}}%
    {\global\advance\beamer@tocsectionnumber by 1\relax%
      \long\def\secname{#2}%
      \long\def\lastsection{#1}%
      \addtocontents{toc}{\protect\beamer@sectionintoc{\the\c@section}{#2}{\the\c@page}{\the\c@part}%
        {\the\beamer@tocsectionnumber}}}%
    {\let\\=\relax\xdef\sectionlink{{Navigation\the\c@page}{\noexpand\secname}}}%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \beamer@ifempty{#1}{}{%
      \addtocontents{nav}{\protect\headcommand{\protect\sectionentry{\the\c@section}{#1}{\the\c@page}{\secname}{\the\c@part}}}%
      \addtocontents{nav}{\protect\headcommand{\protect\beamer@sectionpages{\the\beamer@sectionstartpage}{\the\beamer@tempcount}}}%
      \addtocontents{nav}{\protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}}%
    }%
    \beamer@sectionstartpage=\c@page%
    \beamer@subsectionstartpage=\c@page%
    \def\insertsection{\expandafter\hyperlink\sectionlink}%
    \def\insertsubsection{}%
    \def\insertsubsubsection{}%
    \def\insertsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \def\insertsubsectionhead{}%
    \def\insertsubsubsectionhead{}%
    \def\lastsubsection{}%
    \Hy@writebookmark{\the\c@section}{\secname}{Outline\the\c@part.\the\c@section}{2}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section}\hyper@anchorend%
    \beamer@ifempty{#2}{\beamer@atbeginsections}{\beamer@atbeginsection}%
  \fi%
  \beamer@resumemode}%

编辑:

一些修补程序可以防止在使用\refstepcounter时发生作用\section*

\documentclass{beamer}

\usepackage{xpatch}

\makeatletter
\newif\ifisstarred%   new if command
\isstarredfalse%      % No starred section by default

\xpretocmd{\beamer@@ssection}{\isstarredtrue}{}{}  % enable `*` mode
\xpatchcmd{\beamer@@ssection}{\beamer@section[{#1}]{}}{\beamer@section[{#1}]{}\isstarredfalse}{\typeout{Patch success beamer@@ssection}}{\typeout{Patch failure}}
\xpatchcmd{\beamer@section}{%
  \refstepcounter{section}%
}{%
  \ifisstarred% 
  % Do nothing in here
  \else
  \refstepcounter{section}%
  \fi
}{\typeout{Patch success beamer@section}}{}
\makeatother


\setbeamertemplate{section in toc}[sections numbered]

\begin{document}

\frame{\tableofcontents}

\section{section one}
\frame{\insertsectionnumber\quad\insertsection}

%\inserttocsectionnumber


\section*{section two}
\typeout{Starred section}
\frame{\insertsectionnumber\quad\insertsection}

\section{section three}
\frame{\insertsectionnumber\quad\insertsection}

\end{document}

在此处输入图片描述

相关内容