TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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".

share|improve this question
Have you try the package cleveref ? – projetmbc Oct 18 '11 at 17:18
@projetmbc: Not yet. But it seems to deal only with \ref, not \pageref. – Martin Schröder Oct 18 '11 at 17:39
@projetmbc: The latest version does; see below. – Martin Schröder Oct 18 '11 at 20:12
up vote 6 down vote accepted

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
share|improve this answer
That's exactly what I was looking for. Thanks! – Martin Schröder Oct 18 '11 at 20:11

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? :(

share|improve this answer
[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
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
Yes, thanks, that was a mistake :) – Roelof Spijker Oct 18 '11 at 12:20
Nice. Can this be improved wit LaTeX3 and/or eTeX? – Martin Schröder Oct 18 '11 at 13:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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