# Total number of pages of section

For all sections (or even subsections) in my document (beamer presentation), I want to show the page number only relative to the (sub)section, and also the total number of pages per (sub)section. So in the end, in my footer, I can show sth like:

"Sec 1.2 page 3 / 10"

I found this to reset the page number per section. This does not work for Beamer, though. But I think the resetting of the page number is not too difficult to fix. I guess I could just use \AtBeginSubsection in some way.

I don't really know how to count the pages per each (sub)section, though. Is it possible to create a separate counter per (sub)section? How? And then I would need to collect all these counters, and store their last value to a file, and import that file. Sounds reasonably simple, but my Latex knowledge is lacking too much.

This would do it manually for two sections. But I need to have it automatically for any number of (sub)sections. (Or maybe I misunderstand the answer?)

Here are a lot of references how to count page numbers, and many packages on CTAN are linked, like totcount, pageslts, totpages, lastpage, etc, but from looking through them, it doesn't look like they help me (or at least I don't know how).

The idea can be the following: put a label at the end of every subsection and get the last page number using \pageref{}. The code below adds a label subsection:<number> at every frame, so it produces many warnings about duplicate labels, but seems to work. Also, it needs additional tweaking (e.g. removing page number from the titlepage).

\documentclass[12pt]{beamer}

\counterwithin{subsection}{section}

\usepackage{etoolbox}

\makeatletter
\pretocmd{\frame}{\label{subsection:\thesubsection}}{}{}
\apptocmd{\beamer@section}{\setcounter{page}{1}}{}{}
\apptocmd{\beamer@subsection}{\setcounter{page}{1}}{}{}
\setbeamertemplate{footline}{%
\hfill
Section \thesubsection, page
\thepage/\pageref{subsection:\thesubsection}%
}

\makeatother

\begin{document}

\title{Title}
\author{Author}
\institute{Firm}
\date{Date}

\frame{\titlepage}

\section{First section}
\subsection{First subsection}

\begin{frame}
\frametitle{Title}
\end{frame}

\begin{frame}
\frametitle{Title}
\end{frame}

\begin{frame}
\frametitle{Title}
\end{frame}

\subsection{Second subsection}

\begin{frame}
\frametitle{Title}
\end{frame}

\begin{frame}
\frametitle{Title}
\end{frame}

\end{document}


• Thanks. In the meantime, I came up with a similar solution, so that kind of shows that the general idea was right. I also add \labels. Why couldn't you do it once in \apptocmd? I use \AtBeginSection. I also use some custom counters. Oct 17, 2020 at 11:07
• Just curious, how long is your talk that you need subsections (the second level of sectioning) which require separate frame numbering (they should be fairly long for that)? Oct 17, 2020 at 12:06
• The whole talk is about 2.5h. We only have two big sections (and otherwise short intro and conclusion section), and within those sections, the subsections vary in length, up to 30 slides, up to 30 mins or so. We also record each subsection individually on video (by different people), and concatenate them later, that is why we want to have the page numbers independent per subsection. Oct 17, 2020 at 21:22

In the meantime, after a lot of trial and error, I came up with an own solution. It works, but it kind of ugly, and required lots of tweaks to get some edge cases nicer or right, e.g. first (sub)section or last (sub)section did not behave quite right.

The main code / idea was inspired from here, and then adopted.

\documentclass[handout, t]{beamer}
\usefonttheme[onlymath]{serif}

\usepackage{lmodern}
%\usepackage[default]{sourcesanspro}
\usepackage[utf8]{inputenc}
% See mail / GitHub for discussion on fontenc.
\usepackage[T1]{fontenc}  % always recommended, https://tex.stackexchange.com/questions/1774
\renewcommand*\familydefault{\sfdefault}
\usepackage{amssymb}
\usepackage{pifont}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{ulem}
\usepackage{lastpage}

% ...
% other imports, setup, ...
% ...

% stuff from my beamer template:

% distance of section from the page top
\vspace{4mm}

\hbox{%
%
\hspace*{8mm}%
%
\ifx\subsecname\empty%

\hspace*{1em}\Large\usebeamerfont{subsection}{}\strut%
\else%

% note: by intention, use section font (larger, bold) for subsection title
\ifnum\value{subsubsection}=0%
\else%
\fi%
\vspace{-2pt}
\fi%
\end{beamercolorbox}%
%
\hspace*{8mm}%
%
}%

\defbeamertemplate*{footline}{rwth}{
% ...
% ...
}

% end of beamer template stuff

% now the page number stuff

\AtBeginSection[]{%
\let\subsecname\empty%
\let\subsubsecname\empty%
\setcounter{subsection}{0}%
\setcounter{subsubsection}{0}%
\deffirstlastpagecounters{}%
}

\AtBeginSubsection[]{
\deffirstlastpagecounters{}%
\setcounter{subsubsection}{-1}%
\subsubsection*{Outline}
\begin{frame}
\end{frame}
\setcounter{subsubsection}{-1}%
\clearpage
\subsubsection*{}
\let\subsubsecname\empty%
}

% Counter calculation not needed anymore with page numbers per subsection.
% https://tex.stackexchange.com/questions/2541/beamer-frame-numbering-in-appendix
\newcommand{\backupbegin}{
% Use it one last time.
\deffirstlastpagecounters{}
% Ref to one after last page.
\setcounterpageref{seclastpage}{LastPage}
% Now disable it.
\let\deffirstlastpagecounters\empty%
}
\newcommand{\backupend}{
}

% Inspired via https://tex.stackexchange.com/questions/232785/numbers-for-first-and-last-page-of-chapters
\newcounter{subsecabs}  % absolute sub section
\newcounter{secfirstpage}
\newcounter{seclastpage}
\def\deffirstlastpagecounters{%
\ifnum\value{subsecabs}=0\else% If _not_ the first chapter
\label{sec\thesubsecabs:lastpage}% Insert an end-of-chapter \label
\fi%
%\clearpage
\label{sec\thesubsecabs:firstpage}% Insert a start-of-chapter \label
\setcounterpageref{secfirstpage}{sec\thesubsecabs:firstpage}%
\setcounterpageref{seclastpage}{sec\thesubsecabs:lastpage}%
}
%\AtBeginSection[]{\deffirstlastpagecounters{}}
%\AtBeginSubsection[]{\deffirstlastpagecounters{}}
\AtBeginDocument{\deffirstlastpagecounters{}}
%\AtEndDocument{\label{sec\number\value{subsecabs}:lastpage}}

% counters: \thepage -- \theframenumber -- \thesecfirstpage -- \theseclastpage
\ifnum\value{section}=1%
Introduction%
\else
\ifnum\value{subsection}=0%
Section \number\numexpr\thesection-1%
\else
Section \number\numexpr\thesection-1.\thesubsection%
\fi%
\fi,\\
page \number\numexpr\thepage-\thesecfirstpage+1{} of \number\numexpr\theseclastpage-\thesecfirstpage}

% ...
% other stuff
% ...

\begin{document}

\section*{Introduction}

\input{section_introduction}

\section{Part 1: ...}
\begin{frame}
\vfill
\begin{center}
\secname
% Or sth more custom?
% Or just outline again?
\end{center}
\vfill
\end{frame}

\subsection{Sub 1 ...}
\input{section_part1_sub1}

\subsection{Conclusion}
\input{section_part1_conclusion}

\section{Part 2: ...}
\begin{frame}
\vfill
\begin{center}
\secname
\end{center}
\vfill
\end{frame}

\subsection{Introduction}

% ...

\section{Conclusion}
\input{section_conclusion}

\setbeamertemplate{final page}[rwth][Any questions?]{Thank you for your attention}
\begin{frame}[plain]
\usebeamertemplate{final page}
\end{frame}

\appendix
\backupbegin

%\section{Appendix}
% ... (in case we need any appendix)

\section{References}

\begin{frame}[allowframebreaks]
\centerline{Reference}
%\bibliographystyle{ieeetr}
\bibliographystyle{i6bibstyle}
\bibliography{references}
\end{frame}

\backupend

\end{document}