# Beamer \tableofcontents does not display the correct section number after \setcounter{section}{…}

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]

{
\leavevmode%
\hbox{%
\raggedright
\hspace*{2em}%
{\small%
\ifx\insertsection\empty%
\relax%
\else
\fi
}%
\hspace*{2em}%
\end{beamercolorbox}%
}%
}

\begin{document}

\begin{frame}
\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}


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]

{
\leavevmode%
\hbox{%
\raggedright
\hspace*{2em}%
{\small%
\ifx\insertsection\empty%
\relax%
\else
\fi
}%
\hspace*{2em}%
\end{beamercolorbox}%
}%
}

\begin{document}

\begin{frame}
\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}


• Thanks. And I completely agree that it makes more sense to set the section number to the value you want it to be rather than one less than what you want it to be – Nicholas Hill Mar 25 '13 at 14:40