Cross-Referencing – How to Identify the Section an Equation Is In

cross-referencing

Suppose in my document in section 1 I have a labeled equation, say \label{eq1}. Then much later, say in section 8, I want to reference this equation and the section it is in. I know \ref{eq1} will give me the equation number. But is there a command (or a way to write a command) that would let me say something like \refsec{eq1} and have it return the section number that the equation is in?

I'm trying to avoid having to always look up the section labels. It would be great if this worked across chapters as well.

Best Answer

This following approach based on the idea of label defined by the LaTeX-Kernel. First you have to define a new kind of label which write the needed information to the aux file. Normally the command \label writes the following in the aux file

\newlabel{#1}{{\@currentlabel}{\thepage}}

where \@currentlabel is the current number (in your case the equation number).

Now you need a the current section number instead of the equation number so the information in the aux file should be:

\newlabel{sec@#1}{{\thesection}{\thepage}}

The prefix sec@ is used to avoid multiple labels.

amsmath uses his own definition of \label named \label@in@display. To make sure that you don't have to type two different labels the definition will expand by the new label command.

Now I have to define a new reference command which is able to handle the new label. The idea based on the definition of \ref (see latex.ltx).

The explanation of the commands \@bsphack a.s.o. can be found in the document macros2e.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand\sec@label[1]{%
\@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{sec@#1}{{\thesection}{\thepage}}}%
  \@esphack}

\def\label@in@display#1{%
    \ifx\df@label\@empty\else
        \@amsmath@err{Multiple \string\label's:
            label '\df@label' will be lost}\@eha
    \fi
    \sec@label{#1}\gdef\df@label{#1}%
}
\def\secref#1{\expandafter\@setref\csname r@sec@#1\endcsname\@firstoftwo{#1}}
\def\secpageref#1{\expandafter\@setref\csname r@sec@#1\endcsname\@secondoftwo{#1}}
\makeatother


\begin{document}
\section{foo}
Text
\section{foo}
Text
\section{foo}
Text
\begin{equation}
a+b=c\label{eq1}
\end{equation}


\section{bar}
See equation in section \secref{eq1} on page \secpageref{eq1}
\end{document}

enter image description here

The same with hyperref

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\makeatletter
\newcommand\sec@label[1]{%
      \@bsphack
      \if@filesw
        \begingroup
          \edef\@currentlabstr{%
            \expandafter\strip@prefix\meaning\@currentlabelname
          }%
          \protected@write\@auxout{}{%
            \string\newlabel{seceq@#1}{%
              {\@currentlabel}%
              {\thepage}%
              {\@currentlabstr}%
              {\@currentHref}%
              {\theHsection}%%%%%%%%<- Define the reference counter
            }%
          }%
        \endgroup
      \fi
      \@esphack}

\def\label@in@display#1{%
    \ifx\df@label\@empty\else
        \@amsmath@err{Multiple \string\label's:
            label '\df@label' will be lost}\@eha
    \fi
    \sec@label{#1}\gdef\df@label{#1}%
}
\newcommand*\@refsecstar{}
\newcommand*\T@secref{}

\long\def\@fifthoffive#1#2#3#4#5{#5}
\def\@refsecstar#1{%
  \HyRef@StarSetRef{seceq@#1}\@fifthoffive
}
\def\T@secref#1{\hyperref[{#1}]{\secref*{#1}}}%
 \DeclareRobustCommand\secref{%
    \@ifstar\@refsecstar\T@secref
  }%
\makeatother

\begin{document}

\section{foo}
Text
\section{foo}
Text

\section{foo bar}\label{sec}
Text

\begin{table}[!ht]
\caption{table cpation}
\label{tab}
\end{table}

\addtocounter{equation}{5}
\begin{equation}
a+b=c\label{eq1}
\end{equation}

\begin{align}
a+b=c\label{eq2}
\end{align}
\clearpage
\section{bar}
See equation in section \secref{eq1} on page \pageref{eq1}

See equation in section \secref*{eq1} on page \pageref*{eq1}
\end{document}