[Tex/LaTex] Tagging under an equation using underbrace

amsmathcross-referencingequations

I would like to tag two parts of a huge equation using underbraces (and call them, say term A and term B). Later in the text, I want to talk about term A and term B. Is there a way to tag and label A and refer to it using ref later? Currently, I'm having to manually type in A, B, C, etc. and keep referring to
check if I'm referring to the right tag.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\begin{equation*}
\sum_{i=1}^n (i + i^2) = \underbrace{\sum_{i=1}^n i}_{A}  + \underbrace{\sum_{i=1}^n i^2}_{B}
\end{equation*}

Later in the text, I want to talk about term A and term B.  Is there a
way to tag and label A and refer to it using ref later?  Currently,
I'm having to manually type in A, B, C, etc. and keep referring to
check if I'm referring to the right tag.

\end{document}

I want to replace _{A} with something like _{\tag{A}\label{eq:term1}}. And then, later in the text say Term~\ref{eq:term1} is blah blah blah (where of course, blah blah blah is $n*(n+1)/2$.)

Best Answer

You can use \mytag{<tag>}{<label>} as defined in the following MWE:

enter image description here

\documentclass{article}
%\usepackage{amsmath}% http://ctan.org/pkg/amsmath - loaded by {mathtools}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\makeatletter
\newcommand{\mytag}[2]{%
  \text{#1}%
  \@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{#2}{{#1}{\thepage}}}%
  \@esphack
}
\makeatother

\begin{document}

\begin{equation*}
  \sum_{i=1}^n (i + i^2) = 
    \underbrace{\sum_{i=1}^n i}_{\mytag{A}{termA}} + 
    \underbrace{\sum_{i=1}^n i^2}_{\mytag{B}{termB}}
\end{equation*}

\newpage

Later in the text, I want to talk about term~\ref{termA} and term~\ref{termB}.
\end{document}

You supply the <tag> you want to put down for the label, as well as the regular label <label>. \mytag prints <tag> using \text{<tag>} (from amsmath) and then performs the regular label-writing to the .aux file (taken from latex.ltx). That allows you to use \ref{<label>} to retrieve <tag> again.

Note that this does not support usage with hyperref. Modifications are required in order for that, but it is doable. Wait for it...


...and now with hyperref support:

enter image description here

\documentclass{article}
%\usepackage{amsmath}% http://ctan.org/pkg/amsmath - loaded by {mathtools}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\makeatletter
\newcommand{\mytag}[2]{%
  \text{#1}%
  \@bsphack
  \begingroup
    \@onelevel@sanitize\@currentlabelname
    \edef\@currentlabelname{%
      \expandafter\strip@period\@currentlabelname\relax.\relax\@@@%
    }%
    \protected@write\@auxout{}{%
      \string\newlabel{#2}{%
        {#1}%
        {\thepage}%
        {\@currentlabelname}%
        {\@currentHref}{}%
      }%
    }%
  \endgroup
  \@esphack
}
\makeatother

\begin{document}
\begin{equation*}
  \sum_{i=1}^n (i + i^2) = 
    \underbrace{\sum_{i=1}^n i}_{\mytag{A}{termA}} + 
    \underbrace{\sum_{i=1}^n i^2}_{\mytag{B}{termB}}
\end{equation*}

\newpage

Later in the text, I want to talk about term~\ref{termA} and term~\ref{termB}.
\end{document}

As it stands, these two methods are exclusive. That is, if you want hyperref support, the second approach works. If you do not want support for hyperref, then you should go with the former.

Related Question