2

I have a problem in equations with same label. for example:

\section*{first section}
   \begin{equation}\label{a1}
    E=mc^2
   \end{equation}
   where \eqref{a1} obtained by ....
   \section*{second section}
   \begin{equation}\label{a1}
     a^2=b^2+c^2
   \end{equation}
   in \eqref{a1} we have ....

I can't change labels because of I have many and many similar labels in my 30,000 line document. I want to reset the equation labels before arbitrary sections. Does anyone have good ideas?

Thanks.

5
  • 4
    Welcome to TeX.SX. I really really can't see the point of putting numbers in your labels :S The whole point of the \label{} system is that it isn't hard numbered by hand, meaning you can sub things in, take bits out, move them all around and LaTeX keeps track of the numbering for you. Now if you decide to move a1 somewhere else, LaTeX will still keep track of the numbering correctly for you, but you might well find your own source code mighty confusing when you start talking about \eqref{a1} five equations into your section!
    – Au101
    Jun 24, 2016 at 1:35
  • Having said that, i find it difficult to come up with meaningful, author-friendly labels too and it's not such a sin if the ordering of the equations is more or less set in stone by the very nature of whatever it is you're writing, but I'd really urge you to learn the obvious lesson here, fix or no fix
    – Au101
    Jun 24, 2016 at 1:36
  • 1
    Have you considered some strategies to undertake renaming the labels such as those provided: tex.stackexchange.com/questions/82091/…
    – EngBIRD
    Jun 24, 2016 at 1:38
  • Yes. my question is similar. but I confused.
    – user108724
    Jun 24, 2016 at 1:57
  • See also another question, but it is somewhat related: tex.stackexchange.com/questions/308898/…. LaTeX does only use the latest definition of a \label{a1}, i.e. overwritting it will only produce the last reference, not something in between -- and it will complain of course about multiply defined labels then.
    – user31729
    Jun 24, 2016 at 4:20

1 Answer 1

2

You can do it (but only for equations in any of the amsmath environments). But it is wrong, very wrong: you should think ahead before tweaking this way the cross reference mechanism. When you search for labels, you'll have no clue what each one refers to without first knowing what section the reference/label appears in.

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\newcounter{dummysec}
\preto\section{\stepcounter{dummysec}}
\makeatletter
\def\label@in@display#1{%
    \ifx\df@label\@empty\else
        \@amsmath@err{Multiple \string\label's:
            label '\df@label' will be lost}\@eha
    \fi
    \xdef\df@label{\thedummysec-\unexpanded{#1}}%
}
\renewcommand{\eqref}[1]{\textup{\tagform@{\ref{\thedummysec-#1}}}}
\makeatother

\begin{document}

\section*{first section}

Here is an equation
\begin{equation}\label{a1}
E=mc^2
\end{equation}
where \eqref{a1} obtained by ....

\section*{second section}

Here is another equation
\begin{equation}\label{a1}
a^2=b^2+c^2
\end{equation}
in \eqref{a1} we have ....

\end{document}

enter image description here

4
  • Excuse me. in the simple document it is work correctly but in my document it does not work. I think it is better to we put an flag on everywhere we want to reset the label(before sections).
    – user108724
    Jun 24, 2016 at 14:41
  • @C.F.G I'm sorry if it doesn't work; try leaving out the \preto line and adding \stepcounter{dummysec} before each \section*. But the recommended way is to get rid of duplicate labels.
    – egreg
    Jun 24, 2016 at 14:45
  • Thank you for attention. can you make a code without hyperref package for clickable content? because of my document correctly work without hyperref.
    – user108724
    Jun 24, 2016 at 14:49
  • @C.F.G Where is hyperref in my code?
    – egreg
    Jun 24, 2016 at 14:52

You must log in to answer this question.