1

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}
2
  • It would really help the community if you could provide us with a complete, minimal example that replicates your current setup. Something with a couple of sections with different frame counts. Can you do that?
    – Werner
    Aug 18, 2021 at 1:40
  • @Werner I have added the bare minimum setup below the question.
    – Mew
    Aug 18, 2021 at 10:51

1 Answer 1

1

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}
2
  • It's kind of baffling to me that you had to put in non-trivial work to do this, as I'd expect Beamer to just have this built in. Great work.
    – Mew
    Aug 18, 2021 at 17:26
  • @Mew: The total frame number count is just a label that is stored at the end of the document. So in order to capture the per-section frame total you have to flex some other muscles. I'm sure there might be a more beamer-ish way, but my approach seems to work as expected.
    – Werner
    Aug 18, 2021 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.