我正在关注的解决方案如何在对齐环境中额外命名方程式?命名我自己的方程式。我现在想将它们与符号=
和文本对齐where
,但我做错了。
\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{amsmath}
\begin{document}
\begin{flalign}
\phantom{(\mathrm{elf})\ (2)} && % balance out width of descriptor
\alpha_i^t &= asd \quad\text{where}\quad i \in [1,...,n]
&&
(\mathrm{elf})\label{eq:attentionhead1}\\
\phantom{(\mathrm{elf})\ (2)} && % balance out width of descriptor
\alpha_i^t &= g\Bigl(\frac{u}{v}\Bigr) \quad\text{where}\quad i \in [1,...,n]
&&
(\mathrm{clf})\label{eq:attentionhead2}
%\label{eq:attentionhead1}
\end{flalign}
\ref{eq:attentionhead1} \ref{eq:attentionhead2}
\end{document}
我尝试按照以下解决方案进行操作使用 alignat 将方程式与文本对齐带文本和对齐的方程式,但没有效果。
\begin{flalignat*}{3}
...
\end{flalignat*}{3}
答案1
我会保留基本flalign
设置,因为它可以直接将方程描述符放在最右侧,紧挨着方程编号。为了在单词“哪里”上实现对齐,我建议您 (a) 测量符号右侧最宽元素的宽度,=
(b) 将材料放在\mybox
“盒子”中较短元素中,如下面给出的代码中更详细地描述的那样。
\documentclass{article}
\usepackage{tabularx,booktabs,graphicx}
\usepackage{amsmath}
\newlength\mylen
% measure width of widest element to the right of the '=' symbols
\settowidth\mylen{$\displaystyle g\Bigl(\frac{u}{v}\Bigr)$}
% define a utility macro:
\newcommand\mybox[1]{\parbox{\mylen}{$\displaystyle #1$}}
\begin{document}
\begin{flalign}
\phantom{(\mathrm{elf})\ (2)} % balance out width of descriptor
&&
\alpha_i^t &= \mybox{asd}
\quad\text{where $i\in[1,\dots,n]$}
&&
(\mathrm{elf}) % equation descriptor
\label{eq:attentionhead1} \\
% balancing need not be repeated in second row
&&
\alpha_i^t &= g\Bigl(\frac{u}{v}\Bigr)
\quad\text{where $i\in[1,\dots,n]$}
&&
(\mathrm{clf}) % equation descriptor
\label{eq:attentionhead2}
\end{flalign}
Cross-references to equations \eqref{eq:attentionhead1}
and \eqref{eq:attentionhead2}.
\end{document}
答案2
Mico 的答案可以使用以下功能自动完成eqparbox
。\eqmathbox[<tag>][<align>]{<stuff>}
下面是该包的扩展\eqmakebox
,它<stuff>
在类似的 s 中设置最宽的框,<tag>
并带有可选的本地化<align>
ment(默认为c
entred)。
\documentclass{article}
\usepackage{tabularx,booktabs,graphicx}
\usepackage{amsmath,eqparbox}
% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
\IfValueTF{#1}
{\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
{\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
\mathpalette\eqmathbox@{#3}
}
\makeatother
\begin{document}
\begin{flalign}
\phantom{(\mathrm{elf})\ \eqref{eq:attentionhead1}} % balance out width of descriptor
&&
\alpha_i^t &= \eqmathbox[asd][l]{asd}
\quad\text{where $i\in[1,\dots,n]$}
&&
(\mathrm{elf}) % equation descriptor
\label{eq:attentionhead1} \\
% balancing need not be repeated in second row
&&
\alpha_i^t &= \eqmathbox[asd]{g\Bigl(\frac{u}{v}\Bigr)}
\quad\text{where $i\in[1,\dots,n]$}
&&
(\mathrm{clf}) % equation descriptor
\label{eq:attentionhead2}
\end{flalign}
Cross-references to equations \eqref{eq:attentionhead1}
and \eqref{eq:attentionhead2}.
\end{document}