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 am currently using the \autoref command from the hyperref package to refer to my theorems etc. However, I want to be able to (easily) refer to two or more theorems/lemmas/..., e.g. by calling something like \autoref{lemma1,lemma2,lemma5} -- where of course lemmas 1, 2 and 5 are labeled lemma1, lemma2 and lemma5 respectively -- so that the reference shows: "Lemmas 1, 2 and 5".

Can anyone help me with this? It doesn't seem to me like the hyperref package provides this functionality.

Minimal example

\documentclass{memoir}
\usepackage{amsmath,amssymb}
\usepackage{mathtools}
\usepackage{cleveref}
\mathtoolsset{showonlyrefs,showmanualtags}
\begin{document}
This is the first equation
\begin{equation}
a^2 + b^2 = c^2
\end{equation}
and this is the second equation
\begin{equation}
\label{test}
\alpha^2 + \beta^2 = \gamma^2
\end{equation}
Referring to second equation here: \eqref{test}.
\end{document}
share|improve this question
5  
Have you checked out the cleveref package? It allows multiple references, with sorting and collapsing (e.g. turns "1,3,2,7" into "1--3 and 7"), among other nice things. –  Aaron Apr 13 '11 at 17:47
1  
Yes, I have. But it doesn't seem to work well in my document. See an example here: codeviewer.org/view/code:197c If I remove the cleveref package, the reference works. It seems to conflict with mathtools' showonlyrefs option. –  smlange Apr 13 '11 at 20:06
1  
Yes, mathtools redefines the eqref command to do some extra things (writing to the aux file, specifically). Cleveref also wants to patch the reference system, and the two don't play well together. I think that it would be non-trivial to graft them together, because you'd need to pull apart a comma-separated list of label names then write them all to the aux file to make mathtools happy. –  Aaron Apr 13 '11 at 22:40

2 Answers 2

I decided that I needed this feature of \autoref today and then I was surprised that I was unable to find it...so here is one way to define an \Autoref command that will do it. Specifically,

\Autoref{ref1,ref2,...}

will expand into the plural of the first reference followed by hyperlinks for all of the references. Here's the sample output from the MWE below:

Sample for multiple <code>\autoref</code>'s

There are a few minor(?) limitations in that the macro assumes that:

  • all of the references are of the same "type" (so all lemmas, or all theorems etc)

  • the plural is obtained by adding an s (in particular, it is English-centric)

  • doesn't handle the optional argument to \autoref or the *-form (would be easy to fix...)

  • it probably does not play well with equation references.

The \Autoref macro behaves like \autoref when it is given one reference.

Anyway, here's the code:

\documentclass[a4paper]{amsart}   
\usepackage[colorlinks=true]{hyperref}   
\makeatletter

% define a macro \Autoref to allow multiple references to be passed to \autoref
\newcommand\Autoref[1]{\@first@ref#1,@}
\def\@throw@dot#1.#2@{#1}% discard everything after the dot
\def\@set@refname#1{%    % set \@refname to autoefname+s using \getrefbykeydefault
    \edef\@tmp{\getrefbykeydefault{#1}{anchor}{}}%
    \def\@refname{\@nameuse{\expandafter\@throw@dot\@tmp.@autorefname}s}%
}
\def\@first@ref#1,#2{%
  \ifx#2@\autoref{#1}\let\@nextref\@gobble% only one ref, revert to normal \autoref
  \else%
    \@set@refname{#1}%  set \@refname to autoref name
    \@refname~\ref{#1}% add autoefname and first reference
    \let\@nextref\@next@ref% push processing to \@next@ref
  \fi%
  \@nextref#2%
}
\def\@next@ref#1,#2{%
   \ifx#2@ and~\ref{#1}\let\@nextref\@gobble% at end: print and+\ref and stop
   \else, \ref{#1}% print  ,+\ref and continue
   \fi%
   \@nextref#2%
}

\makeatother
\newtheorem{lemma}{Lemma}
\providecommand*{\lemmaautorefname}{Lemma}
\parindent=0pt
\begin{document}

\begin{lemma}1+1=2\label{one}\end{lemma}
\begin{lemma}2+2=4\label{two}\end{lemma}
\begin{lemma}3+3=6\label{three}\end{lemma}

By \autoref{one}, \autoref{two} and \autoref{three} we are amazed.

By \Autoref{one} we are not amazed.

By \Autoref{one,two} we are a little amazed.

By \Autoref{one,two,three} we are quite amazed.

\end{document}
share|improve this answer
    
Seems to work ok with equation references as far as I can tell. –  tdc Dec 16 at 14:37

The mathtool package does a lot of redefinition of referencing internals, meaning that other reference-related packages (such as cleveref) cannot do their changes as well. So with the set up as given, this is not really possible. You will have to choose between convenient referencing using cleveref and using the mathtool package.

(More of a comment, but enabling the question to be answered.)

share|improve this answer
    
The documentation says: "Cleveref is currently incompatible with the mathtools package’s showonlyrefs option, which automatically labels only those equations that are ross-referenced." Unfortunately, showonlyrefs=false doesn't help even a bit to alleviate the problem so the documentation should probably at least acknowledge that there is a deeper incompatibility between the two packages. –  Christian Mar 20 at 16:31
    
Maybe I should add that it's the cleveref doc that acknowledges the general incompatibility while mathtool doesn't even mention the issue, even though my very superficial understanding of the matter tells me that it would be much easier for mathtool to address the problem as long as showonlyrefs isn't needed. Is there any realistic chance of seeing a donttouchthereferencingstuff option in mathool, @JosephWright or do I have to try and surgically remove the parts of mathtool I need? –  Christian Mar 20 at 16:59

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.