# Left-aligned text inside an equation

Inside an equation-like environment, I'd like to have some text that is aligned to the left margin of the text block, something like \intertext, but in the same line as the equation

Something like this:

\usepackage{amsmath,mathtools}
\newcommand*{\midmathskip}{\hskip0.5\displaywidth\hskip-0.5\totwidth@}
\newcommand*{\lefttext}[1]{\llap{\rlap{#1}\midmathskip}}

\begin{align}
\lefttext{and similarly}
\hphantom{nn-nn}
\mathllap{hwnn} &= H+hw. \\
\lefttext{Thus}
nn-nn &= 1.
\end{align}


of course, the \hphantom and \mathllap are just hacks to make it look right in this case, but I'm looking for something that would work in a gather environment too. flalign could help, for it alters the alignment of the equations (they are not in the center of the text block any more), and it doesn't help with gather anyway.

As a bonus, it would also be nice if it worked with aligned and alignedat.

-
Welcome to TeX.SX! (I think we know each other from comp.text.tex.) One of my guidelines is that if something is difficult to obtain with TeX/LaTeX, then it's probably bad typography. This is the case, in my opinion. –  egreg Jun 15 '12 at 16:13
@egreg Yes, I asked the same question in comp.text.tex. Maybe you are right and it's bad typography, but it's used in at least one book‌​, which I'm trying to reproduce, and I don't think it looks bad. –  Jellby Jun 15 '12 at 16:34
Sorry, but it does look bad to me. I can see page 128; that lonely "Put" had better be in a line of its own and my feeling is that equation 56 is never referenced, so it should be simply in line or it should be a parenthetical addition to equation 55. –  egreg Jun 15 '12 at 16:44
@egreg Well, yes, I agree on the "Put", and some other cases look weird too, but equation 56 is referenced in pages 129 and 130 at least (and all potentially useful equations should have a number, anyway, although they not always do in this book). –  Jellby Jun 15 '12 at 16:51
Possible Duplicate: AMS align: Left aligned text/math plus multicolumn align. –  Peter Grill Jun 15 '12 at 17:04
show 1 more comment

This is a variant, which works with math-environments which understand \tag. Together with the class-option leqno it doesn't work thou. I learned this approach from Philipp Stephani. It works the same way as amsmaths intertext, but puts the text on the same line as the equation.

\documentclass[letterpaper,11pt]{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\makeatletter
\newif\if@gather@prefix
\preto\place@tag@gather{%
\if@gather@prefix\iftagsleft@
\kern-\gdisplaywidth@
\rlap{\gather@prefix}%
\kern\gdisplaywidth@
\fi\fi
}
\appto\place@tag@gather{%
\if@gather@prefix\iftagsleft@\else
\kern-\displaywidth
\rlap{\gather@prefix}%
\kern\displaywidth
\fi\fi
\global\@gather@prefixfalse
}
\preto\place@tag{%
\if@gather@prefix\iftagsleft@
\kern-\gdisplaywidth@
\rlap{\gather@prefix}%
\kern\displaywidth@
\fi\fi
}
\appto\place@tag{%
\if@gather@prefix\iftagsleft@\else
\kern-\displaywidth
\rlap{\gather@prefix}%
\kern\displaywidth
\fi\fi
\global\@gather@prefixfalse
}
\newcommand*{\beforetext}[1]{%
\ifmeasuring@\else
\gdef\gather@prefix{#1}%
\global\@gather@prefixtrue
\fi
}
\makeatother

\begin{document}
\begin{align}
\label{eq:1}
\beforetext{and similary} hwnn &= H+hw\\
\beforetext{Thus} nn-nn &=1.
\end{align}
\end{document}


Output:

-
This solution would be easier to use, but it seems the \beforetext is disabled along with the numbering when \notag or the starred environments are used. And worse, the text is saved and used in a following equation. –  Jellby Jun 16 '12 at 7:39
It was easy to fix, see my answer below. –  Jellby Jun 17 '12 at 12:29

Here's what you referred to for page 136:

\documentclass{article}
\usepackage{mathtool,lipsum}
\newcommand{\lefttext}[1]{\makebox[0pt][l]{#1}}
\begin{document}
\lipsum*[1]
\begin{aligned}[b] \hbar\omega\eta\bar{\eta} &= (2m)^{-1}(p+im\omega q)(p-im\omega q)\\ &=(2m)^{-1}[p^{2}+m^{2}\omega^{2}q^{2}+im\omega(qp-pq)]\\ &=H-\yfrac{1}{2}\hbar\omega \end{aligned}
\vspace{\glueexpr-\baselineskip-\belowdisplayskip-\jot\relax}
\begin{flalign}
&\lefttext{and similarly}&  \hbar\omega\bar{\eta}\eta &= H+\tfrac{1}{2}\hbar\omega. &&\\
&\lefttext{Thus}& \bar{\eta}\eta-\eta\bar{\eta} &= 1.
\end{flalign}
\lipsum*[2]
\begin{flalign}
&\lefttext{Here}&\mathclap{a long equation} &&\\
&\lefttext{whence}&\mathclap{short} &&
\end{flalign}
\end{document}


We have to play a bit with the vertical spacing because two different environments must go one after another. Expect some manual intervention to fix cases like this (the space between equations 1 and 2 is not really precise).

The important aspect for you is the flalign environment. We provide two mock groups at start and end, with zero width, so the middle group will be effectively centered.

I've added a strategy for emulating gather:

\begin{flalign}
&\lefttext{Here}&\mathclap{a long equation} &&\\
&\lefttext{whence}&\mathclap{short} &&
\end{flalign}


Load mathtools for \mathclap. It will be up to you avoiding overlaps.

-
Of course, a zero-width box fixes the alignment issue of flalign, but still there's the problem with gather (and the vertical spacing is a nuisance, as you say). Thanks! –  Jellby Jun 15 '12 at 17:27
@Jellby I've added how to emulate gather. For the vertical spacing, there's little to do other than hoping that consecutive different environments don't appear often. There's no real clue for aligning both those above in only one environment. However, it seems that taking away also \jot fixes the issue. –  egreg Jun 15 '12 at 17:37
Nice! My LaTeX skills are a bit rusty... Any chance something could be done for aligned and alignedat? –  Jellby Jun 15 '12 at 18:01
@Jellby I don't think so. Only flalign does what you want. However, you might give a look at the IEEEeqnarray environment provided by the IEEE classes. My support of bad typography ends here. ;-) –  egreg Jun 15 '12 at 18:08
And since align and friends can be nested inside gather (see here), the vertical spacing issue can be fixed that way too. –  Jellby Jun 20 '12 at 17:09

Just patching \math@cr@@@align I could make Peter Breitfeld's solution to work as I needed. Now it works with align, gather, with and without equation numbers, and with some tricks even with aligned! It doesn't work with equation, but that can be replaced with gather.

I had to copy the definition from amsmath.sty, just to include the \if@gather@prefix\tag@true\fi, which must go after the \omit (I don't know if there's an easier way to patch it).

\documentclass[a5paper]{article}
\usepackage{amsmath,etoolbox}

\makeatletter
\newif\if@gather@prefix
\preto\place@tag@gather{%
\if@gather@prefix\iftagsleft@
\kern-\gdisplaywidth@
\rlap{\gather@prefix}%
\kern\gdisplaywidth@
\fi\fi
}
\appto\place@tag@gather{%
\if@gather@prefix\iftagsleft@\else
\kern-\displaywidth
\rlap{\gather@prefix}%
\kern\displaywidth
\fi\fi
\global\@gather@prefixfalse
}
\preto\place@tag{%
\if@gather@prefix\iftagsleft@
\kern-\gdisplaywidth@
\rlap{\gather@prefix}%
\kern\displaywidth@
\fi\fi
}
\appto\place@tag{%
\if@gather@prefix\iftagsleft@\else
\kern-\displaywidth
\rlap{\gather@prefix}%
\kern\displaywidth
\fi\fi
\global\@gather@prefixfalse
}
\def\math@cr@@@align{%
\ifst@rred\nonumber\fi
\if@eqnsw \global\tag@true \fi
\omit
\kern-\alignsep@
\if@gather@prefix\tag@true\fi
\iftag@
\setboxz@h{\@lign\strut@{\make@display@tag}}%
\place@tag
\fi
\ifst@rred\else\global\@eqnswtrue\fi
\global\lineht@\z@
\cr
}
\newcommand*{\beforetext}[1]{%
\ifmeasuring@\else
\gdef\gather@prefix{#1}%
\global\@gather@prefixtrue
\fi
}
\makeatother

\begin{document}

align
\begin{align}
\beforetext{and similary} hwnn &= H+hw\\
\beforetext{Thus} nn-nn &=1.
\end{align}

align*
\begin{align*}
\beforetext{and similary} hwnn &= H+hw\\
\beforetext{Thus} nn-nn &=1.
\end{align*}

gather (\textbackslash notag on the first line)
\begin{gather}
\beforetext{and similary} hwnn = H+hw \notag \\
\beforetext{Thus} nn-nn =1.
\end{gather}

aligned
\begin{gather}
\beforetext{\displaystyle\begin{aligned}&\!\text{and similarly}\\&\!\text{Thus}\end{aligned}}
\begin{aligned}
hwnn &= H+hw\\
nn-nn &=1.
\end{aligned}
\end{gather}

\end{document}


-
This is a really nice improvement of my solution (+1). I found the the starred version needs a tag even an empty one like \tag*{\null} to work, but couldn't find how to apply it automatically. –  Peter Breitfeld Jun 17 '12 at 13:18