[Tex/LaTex] Left-aligned text inside an equation

equationshorizontal alignment

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.

Best Answer

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: enter image description here