# Add an progress bar image based on page number

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

-
set last page number to, let's say, 1000 in beginning of document. – Eddy_Em May 23 '13 at 4:40
Related Progress bar for latex-beamer and it's linked Q's and The progressbar Beamer theme – texenthusiast May 23 '13 at 4:43
@Eddy_Em, but lastpagenum is not defined as a counter. – Ali Mehrizi May 23 '13 at 5:02
@texenthusiast, Interesting that I searched for many combinations of keywords, but missed the obvious one that would include "progress bar." – Ali Mehrizi May 23 '13 at 5:03
hint: beamer tag search, look at first one. hence tagging/title is very important on SX – texenthusiast May 23 '13 at 5:08

You could simply check whether the lastpage reference (i.e. \r@lastpagenum) is defined (using the \ifdefined macro) before performing your calculations:

\makeatletter% required as some of the commands we will use contain "@" in their name
% only perform the calculations if the "lastpagenum" reference is defined
\ifdefined\r@lastpagenum%
% 1400 below can be replaced with any number number big enough
\setcounter{dummy}{\getrefbykeydefault{lastpagenum}{page}{1400}-1}%
\setcounter{imagenum}{(\thepage-1)*\ratio{24 cm}{\thedummy cm} +1}%
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
\theimagenum%
\fi%
}
\makeatother


The next step is to remove the use of the dummy counter, by getting / converting the last page reference into a number that can be used inside the calculations, using refcount's \getpagerefnumber:

\makeatletter% required as some of the commands we will use contain "@" in their name
% only perform the calculations if the "lastpagenum" reference is defined
\ifdefined\r@lastpagenum%
\setcounter{imagenum}{(\thepage-1)*24/(\getpagerefnumber{lastpagenum}-1) +1}%
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
\theimagenum%
\fi%
}
\makeatother

-
I was just doing the same (removing dummy) but you beat me to it. Just a minor modification to keep the equation correct: \setcounter{imagenum}{(\thepage-1)*24/(\getpagerefnumber{lastpagenum}-1) + 1} – Ali Mehrizi May 23 '13 at 5:06
@AliMehrizi Indeed; somehow that -1 got removed too :) – Xavier May 23 '13 at 5:08
I'm accepting your answer. Probably one of the fastest complete accepted answers on TeX.SX! – Ali Mehrizi May 23 '13 at 5:10
@AliMehrizi I was just refreshing the unanswered list a last time before going to bed :) – Xavier May 23 '13 at 5:16