12

How do I get page numbers / slide numbers in the tableofcontents in a beamer presentation?

1
  • Thank Gonzalo Medina for your wonderful code. How to show frame numbers up to subsubsections in the table of contents? The code of Gonzalo only shows frame numbers of sections and subsections. If a table of contents has subsubsections. These subsubsections don't have their frame numbers in TOC. I guess one can modify the above code. However, I am just a beginner to beamer. So any help is truly appreciated.
    – Truong
    Dec 9, 2021 at 16:21

3 Answers 3

9

One possibility is to redefine \beamer@section and \beamer@subsection as defined in beamerbasesection.sty to include the corresponding page number; here's an example showing the necessary redefinition in action:

\documentclass{beamer}
\usetheme{CambridgeUS}

\makeatletter
\long\def\beamer@section[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{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\hfill\the\c@page}{\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}%

\def\beamer@subsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsection}%
    \beamer@ifempty{#2}{\long\def\subsecname{#1}\long\def\lastsubsection{#1}}
    {%
      \long\def\subsecname{#2}%
      \long\def\lastsubsection{#1}%
      \addtocontents{toc}{\protect\beamer@subsectionintoc{\the\c@section}{\the\c@subsection}{#2\hfill\the\c@page}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{%
      \protect\headcommand{\protect\beamer@subsectionentry{\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@page}{\lastsubsection}}%
      \protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}%
    }%
    \beamer@subsectionstartpage=\c@page%
    \edef\subsectionlink{{Navigation\the\c@page}{\noexpand\subsecname}}%
    \def\insertsubsection{\expandafter\hyperlink\subsectionlink}%
    \def\insertsubsubsection{}%
    \def\insertsubsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \def\insertsubsubsectionhead{}%
    \Hy@writebookmark{\the\c@subsection}{#2}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}\hyper@anchorend%
    \beamer@ifempty{#2}{\beamer@atbeginsubsections}{\beamer@atbeginsubsection}%
  \fi%
  \beamer@resumemode}

\makeatother
\begin{document}

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\section{Test Section One}
\begin{frame}test1\end{frame}
\subsection{Test Subsection One One}
\begin{frame}test2\end{frame}
\begin{frame}test3\end{frame}
\subsection{Test Subsection One Two}
\begin{frame}test4\end{frame}
\begin{frame}test5\end{frame}

\section{Test Section Two}
\begin{frame}test6\end{frame}
\subsection{Test Subsection One One}
\begin{frame}test7\end{frame}
\begin{frame}test8\end{frame}
\begin{frame}test9\end{frame}
\subsection{Test Subsection One Two}
\begin{frame}test10\end{frame}

\end{document}

And an image of the resulting ToC:

enter image description here

Here's a variation producing the frame number in the ToC:

\documentclass{beamer}
\usetheme{CambridgeUS}

\newcounter{sectionpage}

\makeatletter
\long\def\beamer@section[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{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}%
    \setcounter{sectionpage}{\insertframenumber}\stepcounter{sectionpage}%
      \addtocontents{toc}{\protect\beamer@sectionintoc{\the\c@section}{#2\hfill\thesectionpage}{\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}%

\def\beamer@subsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsection}%
    \beamer@ifempty{#2}{\long\def\subsecname{#1}\long\def\lastsubsection{#1}}
    {%
      \long\def\subsecname{#2}%
      \long\def\lastsubsection{#1}%
    \setcounter{sectionpage}{\insertframenumber}\stepcounter{sectionpage}%
      \addtocontents{toc}{\protect\beamer@subsectionintoc{\the\c@section}{\the\c@subsection}{#2\hfill\thesectionpage}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{%
      \protect\headcommand{\protect\beamer@subsectionentry{\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@page}{\lastsubsection}}%
      \protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}%
    }%
    \beamer@subsectionstartpage=\c@page%
    \edef\subsectionlink{{Navigation\the\c@page}{\noexpand\subsecname}}%
    \def\insertsubsection{\expandafter\hyperlink\subsectionlink}%
    \def\insertsubsubsection{}%
    \def\insertsubsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \def\insertsubsubsectionhead{}%
    \Hy@writebookmark{\the\c@subsection}{#2}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}\hyper@anchorend%
    \beamer@ifempty{#2}{\beamer@atbeginsubsections}{\beamer@atbeginsubsection}%
  \fi%
  \beamer@resumemode}

\makeatother

\AtBeginSection[] % Do nothing for \section*
{
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[currentsection]
\end{frame}
}


\begin{document}

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\section{Test Section One}
\begin{frame}test1\pause test11\end{frame}
\subsection{Test Subsection One One}
\begin{frame}test2\end{frame}
\begin{frame}test3\end{frame}
\subsection{Test Subsection One Two}
\begin{frame}test4\end{frame}
\begin{frame}test5\end{frame}

\section{Test Section Two}
\begin{frame}test6\end{frame}
\subsection{Test Subsection Two One}
\begin{frame}test7\end{frame}
\begin{frame}test8\end{frame}
\begin{frame}test9\end{frame}
\subsection{Test Subsection Two Two}
\begin{frame}test10\end{frame}

\end{document}
2
  • Thanks, that partly answers my questions. But this solution doesn't work correctly if I use the \pause command.
    – Ivo
    Aug 8, 2012 at 7:47
  • @Ivo you said you wanted the page number in your question and my solution does exactly that. I guess that now you want the frame number instead, so I've added a new code showing this number. Aug 9, 2012 at 1:21
0

I'm researching the matter, and there seems to be no satisfying answer.

However, if your purpose for the pagenumber/slide numbers is for printing a handout,

    \documentclass[options]{scrartcl} 
    \usepackage{beamerarticle} 

does this when you replace it for \documentclass{beamer}.

3
  • It gives the pagenumbers for (sub)sections and the like in the Table of Content. Aug 7, 2012 at 10:13
  • What if the OP really wants the page number in a presentation? Aug 7, 2012 at 10:29
  • Yes, I want the slide number in the presentation.
    – Ivo
    Aug 7, 2012 at 11:28
0

You can try this:

https://bitbucket.org/rivanvx/beamer/issue/64/add-tableofcontents-styles-that-display

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .