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 would like to do the following:

Just type something like

... we describe this in sections \mycoolref{winter,spring,summer,fall} ...

and it will be typeset as something like

... we describe this in sections 2.1 to 2.4.

One might say that I could just type

... we describe this in sections \ref{winter} to \ref{fall} ...

But the tricky thing is that I might decide to change the order of my winter, spring, summer, fall sections later, and I don't want to have to change my references because of this.

One could imagine a fancy command that takes a set of labels and generates a compact description that consists of ranges if possible, like

2.1, 2.3 to 2.8, and 2.12

Or, does such a macro exist?

share|improve this question

1 Answer 1

up vote 24 down vote accepted

The cleveref package sorts and compresses cross-references. It also automatically determines the format of references, so that you don't have to type "section" in the source.

\documentclass{article}

\usepackage{cleveref}

\begin{document}

\section{winter}\label{winter}

\section{spring}\label{spring}

\section{summer}\label{summer}

\section{fall}\label{fall}

\section{christmas}\label{christmas}

\section{easter}\label{easter}

\section{pentecost}\label{pentecost}

We describe this in \cref{winter,fall,christmas,summer,pentecost} \dots

\end{document}

enter image description here

share|improve this answer
    
+1 -- Just what I was going to answer ;-) –  Anthony Labarre Dec 23 '10 at 12:43
    
Unfortunately, cleveref doesn’t hyperlink the text properly. It merely links the number(s), not the leading text. Which is understandable when there’s multiple references, but very annoying when there’s only a single reference (“see section X” will only make the “X” a link, not the whole “section X”). –  Konrad Rudolph Dec 23 '10 at 12:47
4  
@Konrad: Use \crefformat{section}{#2section~#1#3} to get what you want. Read the cleveref manual for details. –  Lev Bishop Dec 23 '10 at 16:57
1  
@Lev: I feel stupid now. Thanks. However, is there a way to adapt this format for all label types without the need redefine them all explicitly? I’m currently using \@for to solve this (and it works) but it’s clunky and has to be maintained manually whenever a new float type is added. –  Konrad Rudolph Dec 23 '10 at 17:23
1  
@KonradRudolph As you probably know by now, cleveref has the nameinlink option that puts the reference name into the hyperlink. –  mafp May 18 '13 at 16:11

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.