使用下括号标记方程式下方

使用下括号标记方程式下方

我想使用下括号标记一个大方程的两个部分(并将它们称为术语 A 和术语 B)。在文章的后面,我想讨论术语 A 和术语 B。有没有办法标记和标记 A 并在稍后使用 ref 引用它?目前,我必须手动输入 A、B、C 等,并继续引用以检查我是否引用了正确的标记。

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

我想_{A}用类似 的内容替换_{\tag{A}\label{eq:term1}}。然后,在文本后面说Term~\ref{eq:term1} is blah blah blah(当然,blah blah blah 是$n*(n+1)/2$。)

答案1

您可以使用\mytag{<tag>}{<label>}以下 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}

您提供要<tag>放在标签上的,以及常规标签<label>\mytag使用(<tag>来自\text{<tag>}amsmath),然后对文件执行常规标签写入.aux(取自latex.ltx)。这样您就可以再次使用\ref{<label>}进行检索<tag>

请注意,这不支持使用hyperref。为此需要进行修改,但这是可行的。等待它...


...现在hyperref支持:

在此处输入图片描述

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

目前,这两种方法是互斥的。也就是说,如果您需要hyperref支持,则第二种方法有效。如果您不需要支持hyperref,则应该使用前者。

答案2

对实体(此处为术语)进行编号的自然方式是使用计数器。计数器的外观可以从阿拉伯数字更改为大写字母。如果hyperref加载了包,也\autoref可以使用其功能。

我们只需要耍点amsmath小花招。它不允许多个标签,但它接受不同名称下的命令。由于 在 期间hyperref加载并且该包重新定义了,因此使用 延迟了别名的定义。nameref\begin{document}\label\mylabel\label\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}

amsmath这在像这样的软件包的新环境中也有效,gather它使用两遍系统。首先测量方程,然后排版。amsmath只要使用 定义了计数器,软件包就会负责计数器设置\newcounter。或的\label目标设置了两次,但第一次尝试进入被丢弃的测量框。hyperref\refstepcounter

答案3

这是修改后的 MWE(来自问题)以及解决方案!(至少是我最初想到的)。感谢:@Werner 和 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}

相关内容