[Tex/LaTex] Incorrect page number counter in beamer using sections

beamernumberingsectioning

I am trying to put together a beamer document put together using multiple sections where frame numbers restart at each section and I can leave certain frames at the beginning of the document and at the beginning of each section uncounted.

My master document looks like this:

\documentclass{beamer}

\mode<presentation>
{
\usetheme{default} 
\usecolortheme{default}
\usefonttheme{default} 
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{caption}[numbered]
 \setbeamertemplate{footline}[frame number]
}

\AtBeginSection{\setcounter{framenumber}{0}}

\usepackage{hyperref}

\title[MATH]{Daily Notes}
\author{Me}
\institute{My College}
\date{\today}

\begin{document}
{
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]
\titlepage
\end{frame}
}

{
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]{Table of Contents}
\setcounter{tocdepth}{1}
\label{ToC}
\tableofcontents
\end{frame}
}

\input{Day_1.tex}
\input{Day_2.tex}

\end{document}

I use hyperref to create links in the table of contents. The two input files are comparable except in the number of frames and that seems to be the trouble. If Day_1.tex has 4 frames like so:

\section{The First}
\subsection{Housekeeping}

{
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]{Announcements}
Hello world!
\end{frame}
}

\subsection{Content}
\begin{frame}
First slide
\end{frame}

\begin{frame}
Second slide
\end{frame}

\begin{frame}
Third slide
\end{frame}

\begin{frame}
Fourth slide
\end{frame}

and Day_2.tex is the same except having only 3 frames, then the numbering for Day_1 is given as 1/3, 2/3, 3/3, 4/3. If Day_1 has fewer frames than Day_2 (say 2 for Day_1 and 3 for Day_2), then Day_1's numbering is given as 1/3, 2/3.

How can I get the correct number of frames for each section in the frame numbering?

Best Answer

The following answer is based on https://tex.stackexchange.com/a/279364/36296

The problem with your approach is that \totalframenumber is saved at the end of the document, so in your case it is the length of your last section. But instead you can calculate the length of each section.

\documentclass{beamer}

\usepackage{etoolbox}
\makeatletter
\newcount\beamer@sectionstartframe
\beamer@sectionstartframe=1
\apptocmd{\beamer@section}{\addtocontents{nav}{\protect\headcommand{%
            \protect\beamer@sectionframes{\the\beamer@sectionstartframe}{\the\c@framenumber}}}}{}{}
\apptocmd{\beamer@section}{\beamer@sectionstartframe=\c@framenumber\advance\beamer@sectionstartframe by1\relax}{}{}
\AtEndDocument{\immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\beamer@sectionframes{\the\beamer@sectionstartframe}{\the\c@framenumber}}}}}{}{}
\def\beamer@startframeofsection{1}
\def\beamer@endframeofsection{1}
\def\beamer@sectionframes#1#2{%
    \ifnum\c@framenumber<#1%
    \else%
    \ifnum\c@framenumber>#2%
    \else%
    \gdef\beamer@startframeofsection{#1}%
    \gdef\beamer@endframeofsection{#2}%
    \fi%
    \fi%
}
\newcommand\insertsectionstartframe{\beamer@startframeofsection}
\newcommand\insertsectionendframe{\beamer@endframeofsection}
\makeatother

\usepackage{tikz}
\usetikzlibrary{calc}

\def\inserttotalsectionframenumber{%
    \pgfmathparse{(\insertsectionendframe-\insertsectionstartframe+1)}%
    \pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}%
}

\def\insertsectionframenumber{%
    \pgfmathparse{(\insertframenumber-\insertsectionstartframe+1)}%
    \pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}%
}

\setbeamertemplate{footline}{\insertsectionframenumber/\inserttotalsectionframenumber}

\title[MATH]{Daily Notes}
\author{Me}
\institute{My College}
\date{\today}

\begin{document}
{
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]
\titlepage
\end{frame}
}

{
\setbeamertemplate{footline}{}
\begin{frame}[noframenumbering]{Table of Contents}
\setcounter{tocdepth}{1}
\label{ToC}
\tableofcontents
\end{frame}
}

\section{The First}
\subsection{Housekeeping}

{
    \setbeamertemplate{footline}{}
    \begin{frame}[noframenumbering]{Announcements}
        Hello world!
    \end{frame}
}

\subsection{Content}
\begin{frame}
    First slide
\end{frame}

\begin{frame}
    Second slide
\end{frame}

\begin{frame}
    Third slide
\end{frame}

\begin{frame}
    Fourth slide
\end{frame}

\section{The Second}
\subsection{Housekeeping}

{
    \setbeamertemplate{footline}{}
    \begin{frame}[noframenumbering]{Announcements}
        Hello world!
    \end{frame}
}

\subsection{Content}
\begin{frame}
    First slide
\end{frame}

\begin{frame}
    Second slide
\end{frame}

\begin{frame}
    Third slide
\end{frame}

\end{document}