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.

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}
\hypersetup{colorlinks}
\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:

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

And another one:

\begin{equation}
\sin^2(\varphi) + \cos^2(\varphi) = 1
\label{ThisOne}
\end{equation}

\end{document}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

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}

\hypersetup{colorlinks}
\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:

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

And another one:

\begin{equation}
\sin^2(\varphi) + \cos^2(\varphi) = 1
\label{ThisOne}
\end{equation}

\end{document}

enter image description here

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

share|improve this answer
    
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
1  
@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:

enter image description here

\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@addprop{special}{section}% Add a section property to special
\zref@newprop{equation}{\arabic{equation}}% Equation property holds \arabic{equation}
\zref@addprop{special}{equation}% Add an equation property to special
\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
\hypersetup{colorlinks}
\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:

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

And another one:

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

\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}{??}}}

enter image description here

Hyper-referencing is also possible, if needed.


With the addition of

\let\oldlabel\label
\renewcommand{\label}[1]{\oldlabel{#1}\splabel{#1}}%

in your preamble, you only have to use one \label command to obtain the desired referencing output rather than \label + \splabel.

share|improve this answer
    
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
1  
I've updated it to make it more "immediately applicable." Now only a single \label works. –  Werner May 25 '12 at 16:07
    
For the label redefinition to work with hyperref, one needs to take into account tex.stackexchange.com/questions/85956/… –  Alex May 4 '13 at 20:47

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.