27

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?

30

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

| improve this answer | |
  • 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
    @AhmedMusa: Load cleveref after hyperref. – lockstep Dec 13 '11 at 21:03
  • 3
    @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

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.