17

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}
4
  • Seems like a job for zref package...
    – Lev Bishop
    Jan 14, 2011 at 23:32
  • I moved the text from your answer with the question text and merged your two unregistered accounts. Please consider proper registration to avoid the creation of new accounts once your cookie expires. Jun 9, 2011 at 17:02
  • I had no problems compiling your above document. I am running texlive 2011, though. Jun 10, 2011 at 12:42
  • @Ricardo: I updated my hyperref and several of the Oberdiek packages. Now it works magnificently! Muchas gracias to you, and Danke to @Lockstep.
    – Carlos R
    Jun 11, 2011 at 17:45

2 Answers 2

5

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.

4
  • Lockstep, thank you for a suggested first step. How would the code be modified for \documentclass{book}, where sections are numbered within chapters? Also, I tried adding \renewcommand{\theequation}{\thesection.\@arabic\c@equation} as in my original post, but then your approach adds a duplicative section number... do I understand correctly that redefining \theequation this way is incompatible with your approach?
    – Carlos R
    Jan 17, 2011 at 22:29
  • @Carlos: I modified my solution in order to work with the book class. As for your second question: You are right -- my approach is basically incompatible with \renewcommand{theequation}. I also think that "abbreviated" equation numbers (not displaying the section number) go together well with cross-references that are also "abbreviated" when issued in the "home" section.
    – lockstep
    Jan 17, 2011 at 23:04
  • Thank you for adjusting for book class. I tend to agree with you regarding the "abbreviated" numbers in the "home" section; I guess \renewcommand{theequation} was the only way I could see to have the three-number reference outside of the home section, but this may not be necessary or even desireable. As you point out, your approach is unfortunately not compatible with hyperref... can you give a brief explanation of what it would take to get a solution that works with hyperref, or is it pretty complicated?
    – Carlos R
    Jan 21, 2011 at 22:01
  • @Carlos: hyperref itself is very complex, and I don't have a clue what needs to be done to make my solution work with hyperref. It may be pretty easy for someone familiar with the particulars of hyperef.
    – lockstep
    Jan 22, 2011 at 20:10
4

Here is the solution that I currently use, which I believe I learned from http://www.tug.org/pipermail/macostex-archives/2006-April/021810.html.

This solution is based on the package smartref. I use it for chapter instead of section, and so far it seems to work with 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}%
}

\newcommand*{\sref}[1]{\hbox{\srefaux}}% puts an hbox around the reference

With this code, I use the command \sref instead of \ref.

With hyperref the only part of the reference that becomes the link is the non-section part. One can also change this by using the following code instead.

\usepackage{hyperref,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}}}}

Again, \sref becomes the command to use instead of \ref. It creates a reference as you require, in which all of its text becomes the link.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .