Automatic, clever references with cleveref

I recently came into contact with a LaTeX package that saves me quite a bit of time: the cleveref package. This package is very easy to use, so I will start explaining things right away! But first, note that the hyperref package provides something similar with the autoref command, but in my eyes the cleveref package is more sophisticated.

Setup

As always, the package is called in the preamble by writing \usepackage{cleveref}. In your document, all you have to do is write \cref{...} instead of figure~\ref{...}. The cleveref package will automatically detect where the reference is about and it will print the corresponding text (e.g. figure when you refer to a figure environment).

That’s it! … Well, there are some more things that might be handy to know so I’ll briefly go through them.

Capitalized references

As far as I know, the only time you want your references capitalized is on the start of a sentence. For some reason, a lot of my fellow students and colleagues capitalize every reference they make. This is not only wrong (right?), it also looks ugly…
Anyway, to capitalize your references, use \Cref{...} instead of \cref{...}. Easy!

Multiple references

Another thing that is quite nice is that cleveref allows multiple references with one command. For example, \cref{eq1,eq2} prints ‘eqs. (1) and (2)‘. But that’s not all! When referencing to multiple environments (e.g. figures, equations and tables as in \cref{fig1,eq3,tb1}), the package automatically prints the right names to the right references.

Editing the reference names

I like my references to be written out in full, but the cleveref package prints ‘eq. …‘ instead of ‘equation …‘. Again, this is easily changed:

\crefname{equation}{equation}{equations}

In the first argument of \crefname{}{}{} comes the type of reference (equation, figure, table, section, etc.). The second argument contains the word that is printed if only one reference is made and the third argument contains the plural form for multiple references.

More

There is more what you can do with this package, but I tried not to go in-depth. For more information I suggest you to take a look at the package documentation.

13 Comments

  1. Miguel Román says:

    Hello, very good package but I have an issue. I created some enviroments, like corollary, remark, etc with the corresponding label, but Cref them always print Theorem instead of corollary, remark ,etc.

  2. Max says:

    Hi, here is my take on capitalization, although this is quite an old post.

    In the field I work in, the (non) capitalization of references depends on the document style and how the caption of the figure/table/.. is written.

    Hence, if you see ‘Figure 1’, it should be referenced as ‘Figure 1’.
    If it is ‘fig. 1’ then use ‘fig. 1’ or ‘TABLE 1’ results in ‘TABLE 1’.

    Fortunately, many classes provide commands such as \figurename or \tablename so you do not even have to care about spelling it correctly.
    You just have to remember to use that command.

    Personally, I use a \figref{} command I defined in the following way \newcommand{\figref}[1]{\figurename~??}

  3. […] Hence, another LaTeX package that simplifies the writing. The package was earlier mentioned in this post. In that post it is also shown how to change the words in front of […]

  4. Tung says:

    Wow, amazing! this is the really post I used to looking. Thanks!

  5. Richard says:

    I would add that the \usepackage{cleveref} needs to be the last \usepackage in the preamble. If anything else modifys the referencing system (like amsmath) it all goes wrong. This confused me for a while

  6. WOW just what I was searching for. Came here by searching for cleveref

  7. […] Define el comando cref{} que reemplaza a ref{}. La diferencia es que insertará el prefijo correcto (e.g. “eq.”, “fig.”) automáticamente antes del número del elemento referenciado. […]

  8. […] geometry paper might break your page layout. Therefore, I recommend to not use ist. Especially, the Cleverref package might facilitate referencing objects in your paper. If you use this package, you might […]

  9. Simon says:

    Nice tip.

    I think that the rules for capitalising references may be country/language (or even discipline) dependent. A quick survey of my bookshelf found that 9 out of 10 books and journals (mainly physics, but some computing) used capitalised references.

    As I understand it, the reference is in effect the name of the object that is been referenced so under UK/US english rules should be capitalised. Knuth uses capitalised references in the Art of Computer Programming series. If its good enough for the creator of TeX then it is good enough for me.

    • Frits says:

      Hi Simon,
      After some ‘Googling’ I came to the conclusion that most, but not all, journals (mainly physics/mechanics) capitalize all references. So I don’t think there’s a standard rule for this. However your Knuth argument is pretty convincing ;-)

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>