7

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: pbar1.png

pbar12.png: pbar12.png

pbar25.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:

enter image description here

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

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
\lhead{
  % 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
\lhead{
  % 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
| improve this answer | |
  • 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
  • 1
    @AliMehrizi I was just refreshing the unanswered list a last time before going to bed :) – Xavier May 23 '13 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.