I would like to add a "progress bar" to each page of my report. I have 25 images named pbar1.png to pbar25.png, as shown below, to be put on slides:
pbar1.png: 
pbar12.png:
pbar25.png: 
Because I have exactly 25 images, but my report may have more pages (or less), I map the page number to image number using a simple linear map:

I can do the calculations using calc. My problem is to determine how many pages there are. I can use lastpage package or just simply add a label at the end of the pages and call \pageref{lastpagenum}, but then when I run LaTeX on the file for the first time, the reference is undefined and I get an Arithmatic overflow error (as it appears in the denominator of the fraction). I tried using \getrefbykeydefault{lastpagenum}{page}{1400} from refcount to avoid getting zero, but that didn't help. I also tried \ifstrequal, which didn't help either.
Here is the MWE. Some comments are provided in the code.
\documentclass{article}
\newcounter{imagenum}
\newcounter{dummy}
\usepackage{calc}
\usepackage{graphicx}
\usepackage{refcount}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\lhead{
% 1400 below can be replaced with any number number big enough
\setcounter{dummy}{\getrefbykeydefault{lastpagenum}{page}{1400}-1}
% To run without error, replace \thedummy below with 3 (which is 4-1)
\setcounter{imagenum}{(\thepage-1)*\ratio{24 cm}{\thedummy cm} +1}
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
% includegraphics above clearly won't work. So simply just print the scaled image number
\theimagenum
}
\begin{document}
\lipsum[1]\newpage
\lipsum[1]\newpage
\lipsum[1]\newpage
\lipsum[1]
\label{lastpagenum}
\end{document}
lastpagenumis not defined as a counter. – Ali Mehrizi May 23 '13 at 05:02beamertag, because the solution/question is not really limited tobeamer, nor does it needbeamer. It can well be used for other document styles. I certainly agree with your point though. – Ali Mehrizi May 23 '13 at 05:12