Use total number of pages in beamer as counter

I'm trying to set a count in latex on the total number of pages:

\newcount\mypagecount
\mypagecount=\insertpresentationendpage


But whenver I do this, it auto "prints" the total number of pages instead of putting the number as a count. Is there a way to set it as a count?

Here is more precisely what I'm trying to do:

\newcount\mypagenum
\newcount\mypagecount
\newdimen\barwidth

\mypagenum=\insertpagenumber
\mypagecount=\insertpresentationendpage
\barwidth=\paperwidth

\multiply\barwidth by \mypagenum
\divide\barwidth by \mypagecount


I tried with counters, but that threw errors as well:

\newcounter{mypagecount}
\setcounter{mypagecount}{\insertpresentationendpage}


Any ideas how I can do the division at the end if I can't use these numbers and/or how to actually use these numbers?

• \insertpresentationendpage doesn't expand to a number (it isn't expandable at all), so you can't use it directly in an assignment. – Skillmon Sep 8 '20 at 19:04
• Also you have a typo, it should be \insertpagenumber (you're missing the s). – Skillmon Sep 8 '20 at 19:05
• Fixed the typo for the missing s. I double checked and my code did have the correct s. Sorry. – Aram Papazian Sep 8 '20 at 19:33

The following does essentially the same as the current version of \insertpresentationendpage of beamer, but in a way such that your counter is set instead of the result printed. I also included a proof of concept progress bar output.

\documentclass[]{beamer}

\newlength\barwidth
\newlength\tmpbarwidth
\newcount\mypagecount

\makeatletter
\newcommand*\progressbar
{%
\ifnum\mypagecount=0
\ifx\beamer@startpageofappendix\@empty
\mypagecount=\beamer@endpageofdocument\relax
\else
\mypagecount=\beamer@startpageofappendix\relax
\fi
\ifnum\mypagecount=0
\global\mypagecount=1
\else
\global\mypagecount=\mypagecount
\fi
\fi
\begingroup
\tmpbarwidth\insertpagenumber\barwidth
\divide\tmpbarwidth\mypagecount
\rule{\tmpbarwidth}{5pt}%
\textcolor{gray}{\rule{\barwidth}{5pt}}%
\endgroup
}
\makeatother

\setlength\barwidth{5cm}

\begin{document}
\begin{frame}
\progressbar
\end{frame}
\begin{frame}
\progressbar
\end{frame}
\begin{frame}
\progressbar
\end{frame}
\begin{frame}
\progressbar
\end{frame}

\end{document}

• I tried putting this into my template (in footline) and this isn't working. From what I gather there is no variable that expands to a number? – Aram Papazian Sep 8 '20 at 19:44
• @AramPapazian \beamer@endpageofdocument and \beamer@startpageofappendix both do. Be sure to execute this code late (that's why I put it into the definition of \progressbar), beamer sets them really late. – Skillmon Sep 8 '20 at 20:08
• Ok, I got it to work, but halfway through the document I start getting "Arithmetic overflow" errors ._. So like, the first 50 pages have the proper "progress bar", and then it fails after that. – Aram Papazian Sep 8 '20 at 20:17
• @AramPapazian either use \tmpbarwidth=\dimexpr\insertpagenumber\barwidth/\mypagecount\relax instead, or divide first and multiply afterwards. – Skillmon Sep 8 '20 at 21:11
• So your solution does technically work so I gave you credit. What I did instead was to replace \insertpresentationendpage with \insertdocumentendpage and then swap multiply and divid as you suggested and it worked. Thanks. – Aram Papazian Sep 8 '20 at 21:23