Monday, July 09, 2007

\cref* and \crefrange*: temporarily disable cleveref hyperlinks

UPDATE: I've e-mail chatted with the author of cleveref, and it sounds like the changes below (the * macros and the new defaults) may get built into the next release. So that's exciting. The author has recently sent me new versions of cleveref that include these suggestions. Only time will tell when it's on CTAN. So that's exciting.

I've also had a chance to reflect on the poorman option of cleveref. It's an interesting idea. When loaded with this option, cleveref will generate a sed script that the author can use on the source files that will change each of the cleveref macros into old-fashioned references with everything typed out. That is, if you have a journal that does not support cleveref, you run sed on your source code before uploading it, and your journal works with the resulting files that are stripped of cleveref code. That's pretty clever.

I think that by the 1.0 version of cleveref package, it will be ready to replace hyperref's \autoref macro.

One thing that hyperref gives you that cleveref does not are starred versions of the referencing commands that disable hyperlinks. For example, in hyperref, \ref* and \autoref* work exactly like \ref and \autoref but are not hyperlinked. That's nice. Of course, there is no \Autoref or \autorefs or \Autorefs.

A major nice thing that cleveref does is let you format your expanded references with more detail than hyperref. For example, you can make an equation reference expand as "Equation (1)" where the "1" gets linked while having a figure reference expand as "Fig. 1" with the whole "Fig. 1" linked. Of course, this assumes that you've loaded both cleveref and hyperref, like this:
\usepackage{hyperref}
\usepackage[hyperref]{cleveref}
Additionally, cleveref does give the equivalent of those special versions of \autoref. cleveref supports single and multiple references, including reference ranges.

So consider the following, which gives you all the nice stuff about cleveref but also adds starred versions of its macros:
\makeatletter

% \cref and \cref*
\let\origcref\cref
\newcommand{\crefstar}[1]
{\begin{NoHyper}\origcref{#1}\end{NoHyper}}
\renewcommand{\cref}{\@ifstar{\crefstar}{\origcref}}

% \crefrange and \crefrange*
\let\origcrefrange\crefrange
\newcommand{\crefrangestar}[2]
{\begin{NoHyper}\origcrefrange{#1}{#2}\end{NoHyper}}
\renewcommand{\crefrange}
{\@ifstar{\crefrangestar}{\origcrefrange}}

% \Cref and \Cref*
\let\origCref\Cref
\newcommand{\Crefstar}[1]
{\begin{NoHyper}\origCref{#1}\end{NoHyper}}
\renewcommand{\Cref}
{\@ifstar{\Crefstar}{\origCref}}

% \Crefrange and \Crefrange*
\let\origCrefrange\Crefrange
\newcommand{\Crefrangestar}[2]
{\begin{NoHyper}\origCrefrange{#1}{#2}\end{NoHyper}}
\renewcommand{\Crefrange}
{\@ifstar{\Crefrangestar}{\origCrefrange}}

\makeatother
So there you go! That nearly beats \autoref!

Why nearly? Unfortunately, cleveref breaks some Springer journal document classes. The author also doesn't claim that it does or does not support item labels (he mentions that it may or may not, so it's uncertain). Finally, I don't like the default choices for some of the label formats. For example, you shouldn't ever start a sentence with an abbreviation, and the parentheses that go around equation numbers should be surrounded by \textup to keep them from looking silly in an italicized theorem environment. That's why I'm saying that by the 1.0 version (it was 0.6 since June of 2007), it should be a pretty awesome package... especially if the modifications above are built into the package.

No comments: