9

I'm wondering if there's a way to reference the section that an equation is in.

\documentclass{article}
\begin{document}
\section{section 1}\label{sec:first}
\begin{equation}\label{eq:eq1}
    1+1=2 \\
\end{equation}

\section{section2}\label{sec:second}
From equation \eqref{eq:eq1} in section \ref{sec:first}...
\end{document}

Basically, I'd like to replace \ref{sec:first} with a reference to the equation and get the section that it's in. Thank you in advance!

Andrew Swann
  • 95,762

2 Answers2

9

Probably you want to add the number of the section to the equation number.

In this way every reference to an equation contains also the section number and you don't have to add things like \ref{sec:first}.

You just have to add this line in your preamble:

\numberwithin{equation}{section}

MWE:

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{section}

\begin{document}
\section{section 1}\label{sec:first}
\begin{equation}\label{eq:eq1}
    1+1=2 \\
\end{equation}

\section{section2}\label{sec:second}
\begin{equation}\label{eq:eq2}
    2+2=4 \\
\end{equation}

From equation \eqref{eq:eq1}...
\end{document} 

Output:

enter image description here

karlkoeller
  • 124,410
5

If you want to have the section number of a reference, you have to use tricks, e.g. the zref package. There is already a solution to a similar question Extract Section number from Equation reference, however, so the merits should go to Werner ;-)

My version sets a special label using zref capabilities, however, you have to use \label{eq:eq1} and \localspeciallabel{eq:eq1} both, but I wrapped it into a common \commonlabel{eq:eq1} command.

The reference to the section is done with \eqsecref{eq:eq1} etc. I changed the name from \eqref, since there is already a command with that name in amsmath package.

\documentclass{article}
\usepackage{zref}

\makeatletter
\zref@newlist{specialreflist}
\zref@newprop{section}{\arabic{section}}
\zref@addprop{specialreflist}{section}
\newcommand*{\localspeciallabel}[1]{\zref@labelbylist{#1}{specialreflist}}%

\newcommand*{\eqsecref}[1]{%
% Do formatting of number here or outside
\zref@extractdefault{#1}{section}{??}
}%
\makeatother


\newcommand{\commonlabel}[1]{%
\label{#1}%
\localspeciallabel{#1}%
}%

\begin{document}


\section{section 1}\label{sec:first}

\section{section2}\label{sec:second}
\begin{equation}\commonlabel{eq:eq1}
    1+1=2 \\
\end{equation}



\section{section 3}%\label{sec:three}

\begin{equation}\commonlabel{eq:eq2}
    \sqrt{2} \approx 1.414 \\
\end{equation}

From equation \ref{eq:eq1} in section \eqsecref{eq:eq1} it is clear that\ldots, whereas equation \ref{eq:eq2} in section \eqsecref{eq:eq2} shows that\ldots

\end{document}

enter image description here