Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
1  
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
add comment

2 Answers

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

\documentclass{book}
\usepackage{lipsum}
\usepackage{cleveref}% load last

\begin{document}

\chapter{One}
\section{One-One}\label{One-One}
\lipsum[1-4]\label{One-Two}

\chapter{Two}
\section{Two-One}\label{Two-One}
\section{Two-Two}\label{Two-Two}
\section{Two-Three}\label{Two-Three}

\chapter{Three}
\section{Three-One}\label{Three-One}
\lipsum[1-9]

\newpage
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


\bigskip
Using the \textbf{cleveref package (V0.18)}:\par
\bigskip
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
\end{document}
share|improve this answer
    
That's exactly what I was looking for. Thanks! –  Martin Schröder Oct 18 '11 at 20:11
add comment

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.

\documentclass{article}
\makeatletter
\let\dotlessi\i
\newcount\last
\def\range#1{%
  \last 0\relax
  \def\temp{}
  \def\first{}
  \@for\i:=#1\do{%
    \ifnum\the\last=0%start
      \edef\temp{\i}%
    \else%
      \advance\last 1\relax%
      \ifnum\last=\i%
        \ifx\first\empty%
          \let\@temp\temp%
          \edef\temp{\@temp--}%
          \def\first{x}%
        \else%
        \fi%
      \else%
        \ifx\first\empty%
          \let\@temp\temp%
          \let\j\i%
          \edef\temp{\@temp,\j}
        \else%
          \advance\last -1\relax%
          \let\@temp\temp%
          \let\j\i%
          \edef\temp{\@temp\the\last,\j}%
        \fi%
      \fi%
    \fi%
    \last \i\relax%
  }%
  [\temp]%
}
\makeatother
\let\i\dotlessi
\begin{document}
\noindent
Some test ranges\\
\range{1,2,3,4,7}\\
\range{1,3,5,7}\\
\range{1,3,4,5,7}\\
\range{}
\end{document}

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
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
    
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
add comment

Your Answer

 
discard

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.