[Tex/LaTex] How to use \pageref{foo} as a number

calculationscross-referencingpage-numberingzref

What I want to do is to show a "progress bar" in the header to show how far you are in the document, by making a rule with width pagenumber divided by total pages times the paper width.

This is what I have:

\usepackage{fancyhdr,lastpage}
\pagestyle{fancy}
\lhead{\rule{\value{page}/\value{\pageref{LastPage}}*120mm}{2mm}}

but obviously it doesn't work.

Best Answer

Solving this kind of problem is the raison d'ĂȘtre of the refcount package. Here's one way to use it:

\documentclass[oneside]{book}
\usepackage{lipsum,fancyhdr,lastpage,refcount}
\pagestyle{fancy}
\setrefcountdefault{-1}
\lhead{\rule{\dimexpr \textwidth * \thepage/\getpagerefnumber{LastPage}}{2mm}}
\begin{document}
\lipsum[1-60] %Insert dummy text for demonstration
\end{document}
Related Question