6

I am following the solution in How to additionally name equations in align environment? to name my own equations. I would now like to align them at the = sign and the text where, but I am not getting it right.

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

Enter image description here

I tried to follow the solution in Aligning equations with text with alignat equations-with-text-with-alignat, but it did not work.

 \begin{flalignat*}{3}
...
 \end{flalignat*}{3}

2 Answers 2

7

I would keep the basic flalign setup, as it makes it straightforward to place the equation descriptors at the far right, immediately next to the equation number. To achieve alignment on the words "where", I suggest you (a) measure the width of the widest element to the right of the = symbols and (b) place the material in the shorter elements in a \mybox "box", as is described in more detail in the code given below.

enter image description here

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

Mico's answer can be automated using functionality from eqparbox. \eqmathbox[<tag>][<align>]{<stuff>} below is an extension to that package's \eqmakebox that sets <stuff> in the widest box possible across similar <tag>s with the optional localised <align>ment (default is centred).

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .