[Tex/LaTex] How to redefine \label command in align environment

alignamsmathcross-referencing

I have a problem with the redefinition of \label inside of the align environment. The goal is to allow a different behavior of the label command inside of the align environment than outside of it (e.g. for figures and section).

This example does not make much sense, but should show the unexpected behavior of the label command which seems to be overwriten by some internal commands of the align environment. I would expect, that the text REPLACED LABEL: eq:three is printed after the formular and the reference below goes wrong (because of the missing 'real' label), but instead of this the label is handled as usual.

\documentclass{article}
\usepackage{amsmath}

%%% additional commands (for testing)
\newcommand{\mytext}{\textbf{MY TEXT}}
\newcommand{\replacedtext}{\textit{REPLACED TEXT}}
\newcommand{\replacedlabel}[1]{\textbf{REPLACED LABEL: #1}}

%%% replacement of align environment
\let\alignoriginal\align
\let\endalignoriginal\endalign
\renewenvironment{align}{
  \begingroup
  \let\mytext\replacedtext
  \let\label\replacedlabel
  \alignoriginal
  }{
  \endalignoriginal
  \endgroup}

%%% document
\begin{document}

\section{Section 1}    
\replacedlabel{sec:one}
Just for your information: this is \mytext

\begin{align}
  \mytext \\ %%% just to show that mytext is replaced
  f(x) &= x^2 
  \label{eq:three} \\ %%% this label should be replaced
  g(x) &= x^3 
  \replacedlabel{eq:four} %%% this should be the behavior
\end{align}

Take a look at equation \ref{eq:three} and \ref{eq:four} and on \mytext.     
\end{document}

I'd like to replace the \label command by the \replacedlabel command so that the first formular (2) has the same behavior as the second one (3). This seems to be a general problem of this command in combination with the align environment since other replacements work.

I hope someone can help me (or even tell me that this is not possible).

UPDATE

Okay, thank you a lot. That was the missing puzzle piece. In fact, I want to print out the labels for debugging purposes in a specific way (e.g. as shown below).

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}

\makeatletter

\let\labeloriginal\label
\renewcommand{\label}[1]{\labeloriginal{#1}\colorbox{red}{\scriptsize{\texttt{#1}}}\newline}
\newcommand{\eqlabel}[1]{\labeloriginal{#1}\intertext{\colorbox{red}{\scriptsize{\texttt{#1}}}}\nonumber}

\def\label@in@display#1{%
      \ifx\df@label\@empty\else
      \@amsmath@err{Multiple \string\label's:
          label '\df@label' will be lost}\@eha
      \fi
      \eqlabel{#1}%%%\gdef\df@label{#1}%
}

\makeatother

%%% document
\begin{document}

\section{Section 1}
\label{sec:one}

Just for your information:

\begin{align}
  f(x) &= x^2 
  \label{eq:three} \\
  g(x) &= x^3 
  \label{eq:four}
\end{align}

Take a look at equation \ref{eq:three} and \ref{eq:four}.

\end{document}

Finally, the \ref of the equations goes wrong (it returns the section number) but this is no problem since I use this only for debugging and not for the final version.

Thanks a lot,
Daniel

Best Answer

In the documentation of amsmath which is available via texdoc -s amsmath generated by the file amsmath.dtx you will find the handling of label (page 30).

Inside the math environment align the label will be handled separately so you don't need a redefinition of the environment.

I can't understand the intention so here a simple modification.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{letltxmacro}
%%% additional commands (for testing)
\newcommand{\mytext}{\textbf{MY TEXT}}
\newcommand{\replacedtext}{\textit{REPLACED TEXT}}
\newcommand{\replacedlabel}[1]{\rlap{\textbf{REPLACED: #1}}}
\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
    \replacedlabel{#1}\gdef\df@label{#1}%
}
\makeatother
\begin{document}

\section{Section 1}    
\replacedlabel
Just for your information: this is \mytext

\begin{flalign}
  \mytext \\ %%% just to show that mytext is replaced
  f(x) &= x^2 
    \label{eq:three} \\
  g(x) &= x^3 
  \label{eq:four}
\end{flalign}

Take a look at equation \ref{eq:three} and \ref{eq:four} and on \mytext.     
\end{document}