[Tex/LaTex] Progress bar within section/subsection for beamer

beamer

Here it was answered how to implement a progress bar for a beamer presentation. In particular, I liked the first solution. However it has one shortcoming, namely that the progress bar is valid for the entire presentation, so the main part of the code defining the size of the bar is \insertframenumber/\inserttotalframenumber.

Now, imagine I have slides with several sections and subsections, where each subsection has several/many slides (the entire document is more than 100 pages). So I would like to have a progress bar that shows progress in the current section (or even subsection). Actually, many of the default themes have this functionality (as noted here) but not in the form of a progress bar.

I could easily adapt the code in the first solution, but for this I need to find commands that would give the number of frames and the number of the first frame in the current section, if they exist.

Best Answer

Add the following to your preamble.

    \definecolor{lightgr}{rgb}{0.7 0.7 0.7}
    \makeatletter
    \newcount\beamer@sectionstartframe
    \newcount\beamer@sectionendframe

    \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}

    \def\beamertextwidth{\dimexpr 0.875 \textwidth\relax}
    \makeatother

    \addtobeamertemplate{footline}{%
        \color{lightgr}% to color the progressbar
        \rlap{\rule{\numexpr (\insertframenumber-\insertsectionstartframe)+1  \dimexpr  \beamertextwidth/(\insertsectionendframe -\insertsectionstartframe+1) \relax}{3pt}}%
    }%

The above code should create a section progressbar in the footline of each slide. Note that the above code does work properly if you create new frames inside the AtBeginSection environment.