EDIT added a way to set the plural names, which in effect gives foreign language support
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:
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)
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
by default the plural form is obtained simply by adding an s
to the "singular" name (so it is English centric). This can be overridden by defining a plural version of the autorefname, for example \providecommand*{\lemmaautorefnameplural}{Lots of lemmas}
. A more realistic application would be to define plurals for other languages. The ...autorefnameplural
form is optional
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}{}}%
\xdef\@tmp{\expandafter\@throw@dot\@tmp.@}%
\ltx@IfUndefined{\@tmp autorefnameplural}%
{\def\@refname{\@nameuse{\@tmp autorefname}s}}%
{\def\@refname{\@nameuse{\@tmp autorefnameplural}}}%
}
\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}
%\providecommand*{\lemmaautorefnameplural}{Lots of lemmas}
\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}
If you uncomment the line
\providecommand*{\lemmaautorefnameplural}{Lots of lemmas}
then the output is:
A more realistic use of this feature would be to add foreign language support.
Because some redefining of \label
/\ref
by hyperref
/nameref
is done via \AtBeginDocument
, you have to use it also at a later time after the packages are loaded:
\usepackage{hyperref}
\usepackage{letltxmacro}
\AtBeginDocument{%
\LetLtxMacro{\TheRealLabel}{\label}%
\LetLtxMacro{\TheRealRef}{\ref}%
\LetLtxMacro{\TheRealPageRef}{\pageref}%
% redefining \label, \ref, \pageref (\renewcommand or \DeclareRobustCommand)
}
Updated with egreg's comment. \LetLtxMacro
of package letltxmacro
takes care of macros that are defined via \DeclareRobustCommand
(or as macros with optional parameters).
Best Answer
Package
zref-xr
provides many options for importing references with support of many packages (hyperref
,titleref
, ...).Example file
A.tex
withhyperref
, which contains the theorems:File
B.tex
without references imports references fromA
: