[Tex/LaTex] How to reset equation label after arbitrary sections

cross-referencingequationslabels

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.

Best Answer

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