I'm looking for a pure (La)TeX solution (alas, no LuaTeX) for compressing a list of page references (given with \pageref) like cite.sty does with citations; that is "Compress lists of three or more consecutive numbers to one number range." The numbers are already sorted and can have up to four digits. Basically what MakeIndex does but in TeX.

Input would be like \pageref{foo}, \pageref{bar}, \pageref{foobar}, \pageref{quax} (where the delimiter can be choosen as needed) and the output should be something like "1, 2--4".


The latest version (V0.18) of cleveref includes \cpageref and \cpagerefrange which can be used to refer to page ranges as well. It is not yet on CTAN, so for the test below I obtained it from Toby Cubitt's site:

enter image description here

\usepackage{cleveref}% load last





Section One-One is on Page~\pageref{One-One}\par
Section One-Two is on Page~\pageref{One-Two}\par
Section Two-One is on Page~\pageref{Two-One}\par
Section Two-Two is on Page~\pageref{Two-Two}\par
Section Two-Three is on Page~\pageref{Two-Three}\par
Section Three-One is on Page~\pageref{Three-One}\par

Using the \textbf{cleveref package (V0.18)}:\par
See \cpageref{One-One,One-Two,Three-One}\par
See \cpageref{One-One,One-Two,Two-One,Two-Two,Three-One}\par
See \cpagerefrange{One-One}{Three-One}\par

This has not turned out as pretty as I initially hoped, but it seems to work. It takes a list of integers and it compacts them as you described. The code should be pretty self-explanatory, I can't help but feel it should be possible to do it much shorter, but hey.

  \last 0\relax
      \advance\last 1\relax%
          \advance\last -1\relax%
    \last \i\relax%
Some test ranges\\

And the output:

picture showing ranges

PS: what happened to the tex.sx image uploading? :(

  • [image uploading] See imgur.com: "Emergency maintenance We're currently expirencing issues with uploading and viewing some images, and are working on correcting them now. Please check back soon for updates." – topskip Oct 18 '11 at 11:53
  • 3
    I've moved the \newcount command outside the definition of \range: you were allocating a new counter for each call of \range. – egreg Oct 18 '11 at 12:18
  • Nice. Can this be improved wit LaTeX3 and/or eTeX? – Reinstate Monica - M. Schröder Oct 18 '11 at 13:57

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.