Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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
\begin{equation}\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}}
\end{equation}
\eject

\begin{equation}\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}}
\end{equation}

\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}
share|improve this answer
    
Please, don't add "non answers", but rather edit your original question. –  egreg Apr 29 '12 at 23:07
    
But it is an answer to the question I posed. With @Werner's help and a little bit of coding (mostly copy/paste from cleveref.sty and amsmath.sty), I managed to create the command I was asking for. I thought the answer might help others who have similar questions. –  Maronka Apr 30 '12 at 6:10
1  
@Maronka It is an answer now, but the original version still had some questions in it and I would imagine that that is what egreg is referring to (since his comment dates from that time). Posting your own answer is absolutely fine, but when there's still stuff you're asking for help with then it is best to edit the original question. Sometimes it's a fine line! The point is that people are more likely to see edits to questions and so be thinking how best to help, whereas they assume that answers are answers. –  Andrew Stacey Apr 30 '12 at 7:35
    
@AndrewStacey: Sounds good. Am new to this website; still learning the ways. Thanks! –  Maronka May 1 '12 at 6:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.