Macros – Setting Minimum and Maximum Label Values

macros

I was wondering if there is some LaTeX macro/command (I don't know the exact term) that, given a list of labels, gives the lowest/largest label value and has the same behaviour as \ref{} (i.e., inserting a hyperlink to the referenced object in the pdf).

Consider the following simple example, in which are defined 4 different equations.

\documentclass{article}

\usepackage[margin=3cm]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{equation}
\label{eq:energy_mass_lightspeed}
E = mc^2
\end{equation}

\begin{equation}
\label{eq:basic_arithmetics}
2 + 2 = 4
\end{equation}

\begin{equation}
\label{eq:pythagorean_theorem}
a^2 + b^2 = c^2
\end{equation}

\begin{equation}
\label{eq:handshaking_lemma}
\sum_{v\in V(G)} d_G(u) = 2|E(G)|
\end{equation}

In the following sections we use Equations from 
\firstlabel{eq:energy_mass_lightspeed,eq:basic_arithmetics,eq:pythagorean_theorem,eq:handshaking_lemma}
to 
\lastlabel{eq:energy_mass_lightspeed,eq:basic_arithmetics,eq:pythagorean_theorem,eq:handshaking_lemma}.

\end{document}

In the last four lines of the example the hypothetical macros \firstlabeland \lastlabel have as parameter a list of labels (the labels of the four equations). The first, \minimumlabel{..}, should have the same behaviour (in this case) as \ref{eq:energy_mass_lightspeed} since eq:energy_mass_lightspeed is the label that is defined first. The second, \lastlabel{..}, should have the same behaviour as \ref{eq:handshaking_lemma} since eq:handshaking_lemma is the label that is defined last.

Question Can this be done? If so, does there exist something like this?

The motivation is that sometimes the equations can be rearranged in a document in order to enhance readability, or even to be able to streamline explanations. If, then, the writer wants to refer to an equation range, they could use the first and last labels at the moment of referring to the range, but that can change in a future modification of the document. Writing the numbers of the equations is not an option since the authors may decide to add more equations before the first, thus changing the number of the equations. Command \vrefrange helps a bit but it needs the parameters to always be the first and the last (notice that equations may be reordered at some point).

Best Answer

As mentioned in a comment, a solution is to use the package cleveref. In order to do what I asked in the post, first include the package

\usepackage{cleveref}

and use the appropriate command in the last sentence of the document

In the following sections we use \cref{eq:energy_mass_lightspeed,eq:handshaking_lemma,eq:basic_arithmetics,eq:pythagorean_theorem}.

Notice that the labels are not given to \cref in the order they were defined. The command above produces

In the following sections we use eqs. (1) to (4).

Two interesting options are capitalize and noabbrev which, when passed to the package

\usepackage[capitalize,noabbrev]{cleveref}

the command above will produce

In the following sections we use Equations (1) to (4).

(Notice that it went from eqs. to Equations).

Related Question