# Tagging under an equation using underbrace

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$.)

-

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

\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:

\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.

-
Thanks! That works! I'll look into hyperref (and cleveref since I use it) source code. Ideally, I would like "term" to appear automatically when I use \cref{termA}. –  Maronka Apr 29 '12 at 3:39
@Maronka: It would be possible to extend this to include usage with hyperref. However, adding term as part of the reference may be temperamental, since you might want to capitalize at the start of a sentence and sometimes not. Moreover, this is just one way of doing it - there are others. –  Werner Apr 29 '12 at 4:06
Thanks for the hyperref support. Turns out, using cleveref's macros (see code below) makes life much simpler. cleveref's macros take care of hyperref support depending on if hyperref has been included. –  Maronka Apr 29 '12 at 22:58

The natural way to number entities (here terms) is using a counter. The appearance of the counter can be changed from arabic numbers to uppercase letters. If package hyperref is loaded, also its \autoref feature can be used.

We need only to trick amsmath a little. It does not allow multiple labels, but it accepts the command under a different name. Because hyperref loads nameref during \begin{document} and that package redefines \label, the definition of the alias \mylabel for \label is delayed using \AtBeginDocument.

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}

\newcounter{term}
\renewcommand*{\theterm}{\Alph{term}}
\newcommand*{\termautorefname}{term}

\AtBeginDocument{%
\let\mylabel\label
}

\newcommand{\mytag}[1]{%
\begingroup % keep the effects of \refstepcounter local
\refstepcounter{term}%
\mylabel{#1}%
\text{\theterm}%
\endgroup
}

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

\newpage

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


This does also work in the new environments of package amsmath like gather that uses a two pass system. First the equation is measured, then typeset. Package amsmath takes care of the counter settings as long as the counter was defined using \newcounter. The \label or the destination by hyperref's \refstepcounter is set twice, but the first tries go into the measuring box that is thrown away.

-

Here's the modified MWE (from the question) along with a solution! (at least what I originally had in mind). Thanks: @Werner and the authors of cleveref, amsmath.

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \term definition
% Usage:  \underbrace{<long-expression>}_{\term[optlabel]{labelname}}
% Later on, use \cref{labelname} or \ref{labelname}
% if [optlabel] is missing, a counter (A, B, C, ...) is used.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{termcounter}
\renewcommand{\thetermcounter}{\Alph{termcounter}}
\crefname{term}{term}{terms}
\creflabelformat{term}{#2\textup{(#1)}#3}

\makeatletter
\def\term{\@ifnextchar[\term@optarg\term@noarg}%]
\def\term@optarg[#1]#2{%
\textup{#1}%
\def\@currentlabel{#1}%
\def\cref@currentlabel{[][2147483647][]#1}%
\cref@label[term]{#2}}
\def\term@noarg#1{%
\refstepcounter{termcounter}%
\textup{\thetermcounter}%
\cref@label[term]{#1}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{Main Section}

\eject
$$\label{eq:1} \sum_{i=1}^n (i + i^2) = \underbrace{\sum_{i=1}^n i}% _{\term{termA}} + \underbrace{\sum_{i=1}^n i^2}_{\term[{B but actually termC}]{termC}}$$
\eject

$$\label{eq:2}\tag{\dagger} \sum_{i=1}^n (i + i^2) = \underbrace{\sum_{i=1}^n i}% _{generic} + \underbrace{\sum_{i=1}^n i^2}_{\term[B]{termB}}$$

\eject
Later in the text, I want to talk about \cref{termA}, \cpageref{termA}
and \cref{termC}.  Is there a way to say \ref{termA} is a term inside
\cref{eq:1} ?

Is there a \ref{termA}, 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}

-