Extract Section number from Equation reference

How can I obtain the number of the Section in which an Equation (or Figure) appears? For instance, I have the reference 3.2, which refers to the second (2nd) Equation in Section 3. How can I extract the 3 (or the 2), such that it is still clickable (using hyperref)?

Preferably it should return "2nd" when it is the second Equation, "3d" when it's the third one, and so on.

MWE:

\documentclass[10pt,a4paper,fleqn]{article}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{hyperref}
\numberwithin{equation}{section}
\setlength{\parindent}{0pt}
\begin{document}

\section{Start}
\lipsum[1]

\section{Halfway}

The equation \eqref{ThisOne} is the (1st, 2nd, 3d, ...?) Equation in Section (1, 2, 3, ...?).

\section{End}

A famous formula:

$$x_1, x_2 = \frac{ -b \pm \sqrt{b^2 - 4ac} }{ 2a }$$

And another one:

$$\sin^2(\varphi) + \cos^2(\varphi) = 1 \label{ThisOne}$$

\end{document}

-

You can do this easily with the help of the refcount (to turn the reference number into a string), xstring (to extract the number before and after the dot), and engord (to get the desired format as ordinal number) packages.

In the following example, I defined the commands \SecNum and \EqNum; the first one gives the section number of the reference and the second one gives the equation number in ordinal notation and turns it into a hyperlink to the given equation:

\documentclass[10pt,a4paper,fleqn]{article}
\usepackage{amsmath}
\usepackage{refcount}
\usepackage{xstring}
\usepackage{engord}
\usepackage{xspace}
\usepackage{lipsum}
\usepackage{hyperref}

\numberwithin{equation}{section}
\setlength{\parindent}{0pt}

\newcommand\equ{}
\newcommand\EqNum[1]{%
\StrBehind{\getrefnumber{#1}}{.}[\equ]%
\hyperref[#1]{\engordnumber{\equ}\xspace}%
}
\newcommand\SecNum[1]{%
\StrBefore{\getrefnumber{#1}}{.}\xspace%
}

\begin{document}

\section{Start}
\lipsum[1]

\section{Halfway}

The equation \eqref{ThisOne} is the \EqNum{ThisOne}~Equation in Section \SecNum{ThisOne}.

\section{End}

A famous formula:

$$x_1, x_2 = \frac{ -b \pm \sqrt{b^2 - 4ac} }{ 2a }$$

And another one:

$$\sin^2(\varphi) + \cos^2(\varphi) = 1 \label{ThisOne}$$

\end{document}


I added \xspace from the xspace package to take care of proper spacing.

-
Thanks, this seems a very nice solution. But for some reason, I'm unable to compile it (using PDFLaTeX): ! Missing number, treated as zero., l.29 ...ion \eqref{ThisOne} is the \EqNum{ThisOne}. For some reason, it expects another argument after ThisOne? –  Ailurus May 25 '12 at 9:34
@Ailurus this will happen the first time you process your document with a new \EqNum command (the string for the reference has not yet been generated); press "q" when prompted and process the document again. –  Gonzalo Medina May 25 '12 at 13:32
Thanks, that solved the problem (I had to run it manually, normally I use a shortcut within my editor). One other thing, why is there so much whitespace around 2^{nd}? –  Ailurus May 25 '12 at 14:20
@Ailurus problem solved! It was just some spurious blank spaces in the code that I've corrected in my updated answer. –  Gonzalo Medina May 25 '12 at 14:45
Ah I see. Now I also know what the %-sign means at the end of a line. Thanks! –  Ailurus May 25 '12 at 14:55

The zref package allows for an extention of the regular 2-part \label-\ref system into any number of properties/elements. The following MWE creates a new property list (called special) and adds section and equation counters (in \arabic format) to this list for referencing:

\documentclass[10pt,a4paper,fleqn]{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{zref}% http://ctan.org/pkg/zref

\makeatletter
\zref@newlist{special}% Create a new property list called special
\zref@newprop{section}{\arabic{section}}% Section property holds \arabic{section}
\zref@newprop{equation}{\arabic{equation}}% Equation property holds \arabic{equation}
\newcommand*{\eqnref}[1]{\zref@extractdefault{#1}{equation}{??}}
\newcommand*{\secref}[1]{\zref@extractdefault{#1}{section}{??}}
\newcommand*{\spref}[2][section]{\zref@extractdefault{#2}{#1}{??}}
\newcommand*{\splabel}[1]{\zref@labelbylist{#1}{special}}%
\makeatother
\numberwithin{equation}{section}
\setlength{\parindent}{0pt}
\begin{document}

\section{Start}
\lipsum[1]

\section{Halfway}

The equation \eqref{ThisOne} is Equation~\eqnref{ThisOne} in Section~\secref{ThisOne}.

\section{End}

A famous formula:

$$x_1, x_2 = \frac{ -b \pm \sqrt{b^2 - 4ac} }{ 2a }$$

And another one:

$$\sin^2(\varphi) + \cos^2(\varphi) = 1 \label{ThisOne}\splabel{ThisOne}$$

\end{document}


zref labels are set using \splabel, while equations are referenced using \eqnref and sections using \secref. These are specific implementations of a more general \spref[<type>]{<refname>} (where <type> defaults to section). Since the macros used are expandable, fmtcount can also be used to provide ordinal references:

\usepackage{fmtcount}% http://ctan.org/pkg/fmtcount
%...
\newcommand*{\eqnref}[1]{\ordinalnum{\zref@extractdefault{#1}{equation}{??}}}


Hyper-referencing is also possible, if needed.

\let\oldlabel\label

in your preamble, you only have to use one \label command to obtain the desired referencing output rather than \label + \splabel.
Thanks Werner, nice to see such different solutions for my question. The only drawback seems that one has to use a different kind of label (\splabel), such that it is not immediately applicable to an existing document. –  Ailurus May 25 '12 at 9:42
I've updated it to make it more "immediately applicable." Now only a single \label works. –  Werner May 25 '12 at 16:07