[Tex/LaTex] Beamer \tableofcontents does not display the correct section number after \setcounter{section}{…}

beamernumberingsectioningtable of contents

I use beamer to make presentations for my lectures (I despise PowerPoint). Each presentation represents one chapter of the textbook. Therefore, I need the section numbers in the beamer presentation to match the section numbers in the textbook chapter. However, since we sometimes skip sections, I must use \setcounter{section}{...}.

In the code displayed below, the headlines of the second and third slides display the section numbers correctly as 1 and 9, respectively. However, the table of contents on the first slide displays the section numbers incorrectly as 1 and 2. The table of contents seems to ignore the \setcounter{section}{...} command.

Looking in the toc file, this is indeed the case

\beamer@endinputifotherversion {3.26pt}
\beamer@sectionintoc {1}{First}{2}{0}{1}
\beamer@sectionintoc {9}{Ninth}{3}{0}{2}

The third and fifth parameters of the \beamer@sectionintoc macro have not been changed to reflect the non-sequential value of the section number.

Is there something else that needs to be set in order for beamer to display the correct section number in the table of contents?

\documentclass{beamer}
% This gets rid of the font warnings
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\setbeamertemplate{section in toc}[sections numbered]

\setbeamercolor{headline colour}{fg=blue,bg=yellow}
\setbeamertemplate{headline}
{
   \leavevmode%
   \hbox{%
      \begin{beamercolorbox}[wd=\paperwidth,ht=4ex,dp=1.5ex]{headline colour}%
      \raggedright
      \hspace*{2em}%
      {\small%
         \ifx\insertsection\empty%
            \relax%
         \else
            Section \#\thesection\ \insertsectionhead%
         \fi
      }%
      \hspace*{2em}%
      \end{beamercolorbox}%
   }%
}

\begin{document}

\begin{frame}
   {Table of Contents}
   \tableofcontents
\end{frame}

\section{First}
\begin{frame}
   Something in the First section.
\end{frame}

\setcounter{section}{8}
\section{Ninth}
\begin{frame}
   Another thing in the Ninth section.
\end{frame}

\end{document}

Best Answer

The beamer class uses an independent counter for the table of contents, so it's not sufficient to set the section counter, but also the TeX counter \beamer@tocsectionnumber (which must be operated on with primitive commands). I suggest using a command for setting directly the requested number:

\setnextsection{9}

which is easier than setting it one less than wanted.

\documentclass{beamer}
% This gets rid of the font warnings
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\makeatletter
\newcommand{\setnextsection}[1]{%
  \setcounter{section}{\numexpr#1-1\relax}%
  \beamer@tocsectionnumber=\numexpr#1-1\relax\space}
\makeatother

\setbeamertemplate{section in toc}[sections numbered]

\setbeamercolor{headline colour}{fg=blue,bg=yellow}
\setbeamertemplate{headline}
{
   \leavevmode%
   \hbox{%
      \begin{beamercolorbox}[wd=\paperwidth,ht=4ex,dp=1.5ex]{headline colour}%
      \raggedright
      \hspace*{2em}%
      {\small%
         \ifx\insertsection\empty%
            \relax%
         \else
            Section \#\thesection\ \insertsectionhead%
         \fi
      }%
      \hspace*{2em}%
      \end{beamercolorbox}%
   }%
}

\begin{document}

\begin{frame}
   {Table of Contents}
   \tableofcontents
\end{frame}

\section{First}
\begin{frame}
   Something in the First section.
\end{frame}

\setnextsection{9}
\section{Ninth}
\begin{frame}
   Another thing in the Ninth section.
\end{frame}

\end{document}

enter image description here