[Tex/LaTex] How to discard a portion of an equation number when cross-referencing

cross-referencingequationsnumbering

I am writing a LaTeX document in which I number equations within sections. Thus, in my preamble I have

\makeatletter
    \@addtoreset {equation}{section}
    \renewcommand{\theequation}{\thesection.\@arabic\c@equation}
\makeatother

and this works the way I would like it to. When I cross-reference an equation with "see Eq.\ (\ref{mylabel})" I get, for example, "see Eq. (3.4.15)" for the 15th equation in Sec. 3.4.

However, I would like to be able to cross-reference the equation from within the section where it resides and omit the section number. In this case, I would like to obtain "see Eq. (15)"

I am using the hyperref package, so the information in the .aux file looks like

\newlabel{mylabel}{{3.4.15}{43}{GENERALIZED SPEEDS\relax }{equation.3.4.15}{}}

I gather that the \ref command looks in the .aux file for {mylabel}, finds {3.4.15}, and uses the string within the braces. I am wondering if it is possible to define a new command, call it \myref, and have it discard "3.4." So the result of "see Eq.\ (\myref{mylabel})" would be "see Eq. (15)"

The \hyperref command has the syntax

\hyperref[mylabel]{text}

so if I could figure out a way to use mylabel to go into the .aux file, bring back the string 3.4.15, and discard "\thesection.", I could use what remains as the argument {text} for \hyperref.


@Ricardo: Thank you so very much for this solution. It works very well without hyperref, but unfortunately I run into a problem with hyperref. When I use

\renewcommand{\theequation}{\@arabic\c@equation}

to create abbreviated equation numbers in the home section, this leads to non-unique entries in the aux file, and I get the error

! pdfTeX warning (ext4): destination with the same identifier (name{equation.1}
) has been already used, duplicate ignored

The problem is that each section has an equation (1). I tried to fix the problem according to the advice on “Subordinate counters'' in the hyperref README, but have been unsuccessful. Here is a combination of your code and @Lockstep's example that should reproduce the problem:

\documentclass{book}

% The book class numbers equations within chapters.
% We want to number these within sections.    

\makeatletter
    \@addtoreset {equation}{section}
    \renewcommand{\theequation}{\@arabic\c@equation}
\makeatother

\usepackage[colorlinks=true, linkcolor={blue}]{hyperref}
\usepackage{smartref}

\addtoreflist{section}

\newcommand*{\srefaux}[1]{%
  \issectionchanged{#1}% checks if section number has changed
  \ifsectionchanged% if section is different
  \sectionref{#1}.% put the section reference
  \fi% else do nothing
  \ref*{#1}%  <-<-<--- we changed this to ref*
}

\newcommand*\sref[1]{\hyperref[#1]{\hbox{\srefaux{#1}}}}

\begin{document}

\chapter{first}
\section{bla}

If we include a first equation
\begin{equation}\label{eq:xyz}
x^2 + y^2 = z^2
\end{equation}
in this section then hyperref will not be happy with another first equation in the 
next section.

As shown in Eq.\ (\sref{eq:def}) \dots

\section{blubb}

As shown in Eq.\ (\sref{eq:abc}) \dots

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

\begin{equation}\label{eq:def}
d^2 + e^2 = f^2
\end{equation}

\section{foo}

As shown in Eq.\ (\sref{eq:abc}) \dots

\end{document}

Best Answer

While I'd like to see a solution involving the zref package, here's a quick fix using etoolbox. The new \myref command will automatically precede each equation number with the section number unless it is issued in the section where the associated label was defined.

\documentclass{book}

\usepackage{etoolbox}

\makeatletter

\apptocmd{\label}{%
  \protected@write\@auxout{}{%
    \csxdef{secnum@#1}{\thesection}%
  }%
}{}{}

\newcommand*{\myref}[1]{%
  (% Optional
  \edef\@tempa{\thesection}%
  \ifcsequal{secnum@#1}{@tempa}{%
  }{%
    \csname secnum@#1\endcsname.%
  }%
  \ref{#1}%
  )% Optional
}

\makeatother

\usepackage{chngcntr}
\counterwithout{equation}{chapter}
\counterwithin*{equation}{section}

\begin{document}

\chapter{first}

\section{bla}

As shown in equation~\myref{eq:abc} \dots

\section{blubb}

As shown in equation~\myref{eq:abc} \dots

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

\begin{equation}\label{eq:def}
d^2 + e^2 = f^2
\end{equation}

\section{foo}

As shown in equation~\myref{eq:abc} \dots

\end{document}

EDIT: I must stress that my code doesn't work with hyperref and therefore is at best a first step to a solution.

EDIT 2: Changed code in order to work with the book class, using package chngcntr.