10

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.

7
  • 2
    Why not build your equation counters to depend on chapter/section?
    – Alan Munn
    Dec 30, 2011 at 18:32
  • 4
    You can consider numbering equations by section: with amsmath it's sufficient to say \numberwithin{equation}{section}.
    – egreg
    Dec 30, 2011 at 18:33
  • This looks like we would need to change the labels on each equation. Since we are nearing the end of the project (15 chapter, 1200 pages) that isn't feasible. We're looking for a way to speed up the final editing/cross-referencing process.
    – HTG
    Dec 30, 2011 at 18:45
  • 1
    Which documentclass are you using? If your document uses chapters then the default labelling should be (1.1), (1.2), (1.3), etc where 1 is the chapter number, and 1, 2, 3 etc are the equation numbers. If you use @egreg's suggestion, the labels and cross references should change automagically, to 1.1.1, 1.1.2, 1.1.3
    – cmhughes
    Dec 30, 2011 at 18:53
  • We're using the report document class. We are trying to avoid having the three-number references because we think it looks clunky. Right now the references show up as section.equation. The problem occurs where we currently have a \pageref, and the publisher has asked us to reference the section instead. That get's tedious, so we were looking for a fast change.
    – HTG
    Dec 30, 2011 at 19:18

1 Answer 1

16

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}
5
  • Wow! That's amazing! I've got to learn more about the detailed coding. Right now it's magic, but it works great!
    – HTG
    Dec 30, 2011 at 18:55
  • Absolutely brilliant! :) Dec 31, 2011 at 13:03
  • @Marco: Does this code work only for equation labels? If so, how difficult would it be to make it generic? so that you could say \secref{any label} and have it return the section number where the figure, or example, or table, or any other labeled item is?
    – HTG
    Dec 31, 2011 at 17:37
  • @HTG: very simple. \let\origlabel\label then \def\label#1{\origlabel{#1}\sec@label{#1}} Dec 31, 2011 at 17:44
  • @HTG: You are interested in referencing -- Maybe this answer is also helpful for you: tex.stackexchange.com/questions/14981/… Dec 31, 2011 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.