Reset frame number and total frame count per Beamer section

beamerpage-numberingsectioning

I'm using a LaTeX Beamer frame counter that displays

\insertframenumber/\inserttotalframenumber

… which would mean that if my project has twenty frames and the slideshow is on the sixteenth frame, "16/20" is displayed.

My project contains very long sections (they mirror book chapters), and I want the frames of each section to have a counter local to that section.

For example, if that sixteenth frame is actually in section 2, and section 1 had 5 frames, then it should show "11/15".

Maybe there's a solution with \AtBeginSection, but I doubt it, since there would be no good way of calculating \inserttotalframenumber to my knowledge.

If it helps, here's an MWE of my current setup (with fewer frames):

%%% Preamble %%%
\documentclass{beamer}
\usetheme{Antibes}
\usebeamercolor{dolphin}

% Make slide numbers appear, and they appear next to navigation
\addtobeamertemplate{navigation symbols}{}{%
    \usebeamerfont{footline}%
    \usebeamercolor[fg]{footline}%
    \hspace{1em}%
    \insertframenumber/\inserttotalframenumber
}

%%% Document %%%
\begin{document}

    \section{First section}
    
    \begin{frame}
        \frametitle{First}
        This frame should show ``1/2'', not ``1/5''.
    \end{frame}
    
    \begin{frame}
        \frametitle{Second}
        This frame should show ``2/2'', not ``2/5''.
    \end{frame}
    
    \section{Second section}
    
    \begin{frame}
        \frametitle{Third}
        This frame should show ``1/3'', not ``3/5''.
    \end{frame}
    
    \begin{frame}
        \frametitle{Fourth}
        This frame should show ``2/3'', not ``4/5''.
    \end{frame}
    
    \begin{frame}
        \frametitle{Fifth}
        This frame should show ``3/3'', not ``5/5''.
    \end{frame}

\end{document}

Best Answer

The following approach inserts a "label" into the .aux at every \section (including at the end of the document) in order to capture the current frame number. This captured frame number (which resets with every \section thanks to chngcntr) is then inserted in the footer.

enter image description here

\documentclass{beamer}

\usetheme{Antibes}
\usebeamercolor{dolphin}

\usepackage{chngcntr}
\counterwithin{framenumber}{section}% Number frames within \section

% Make slide numbers appear, and they appear next to navigation
\addtobeamertemplate{navigation symbols}{}{%
  \usebeamerfont{footline}%
  \usebeamercolor[fg]{footline}%
  \hspace{1em}%
  \insertframenumber/\inserttotalframenumberbysection
}

\makeatletter
\let\oldsection\section
\renewcommand{\section}{% Store current \section's frame count
  \immediate\write\@auxout{\global\noexpand\@namedef{s@totalsectionframes-\arabic{section}}{\arabic{framenumber}}}%
  \oldsection
}
\AtEndDocument{% Store final \section's frame count
  \immediate\write\@auxout{\global\noexpand\@namedef{s@totalsectionframes-\arabic{section}}{\arabic{framenumber}}}%
}
\newcommand{\inserttotalframenumberbysection}{\csname s@totalsectionframes-\arabic{section}\endcsname}%
\makeatother

\begin{document}

\section{First section}

\begin{frame}
  \frametitle{First}
  This frame shows ``1/2'', not ``1/5''.
\end{frame}

\begin{frame}
  \frametitle{Second}
  This frame shows ``2/2'', not ``2/5''.
\end{frame}

\section{Second section}

\begin{frame}
  \frametitle{Third}
  This frame shows ``1/3'', not ``3/5''.
\end{frame}

\begin{frame}
  \frametitle{Fourth}
  This frame shows ``2/3'', not ``4/5''.
\end{frame}

\begin{frame}
  \frametitle{Fifth}
  This frame shows ``3/3'', not ``5/5''.
\end{frame}

\end{document}
Related Question