[Tex/LaTex] \hfill in math mode

horizontal alignmentmath-mode

Is there any easy way of declaring a new command which emulates \hfill in math mode. I don't want flalign or something similar. I want to use the command in the same way (and with the same freedom) I use \hfill.

Is that possible?

i.e. this is an example:

\begin{align*}
    a + b + c &= a + b + c + d + e + f + g + h + i \\
    & \mathhfill \text{(foo)} 
\end{align*}

where I want (foo) to be right aligned (under + h + i and here

\begin{align*}
    a + b + c + d + e + f + g + h + i &= a + b + c \\
    \text{(foo)} \mathhfill &
\end{align*}

I want the text aligned on the left (just under the a + b +. But I don't want flalign or other solutions. Because there are lots of situations (this is a minimal example). And I would like to see a solution which work just as \hfill.

EDIT: well, as Carlisle said, it works in $a \hfill b$, but I would like to see it working in align, i.e.

EDIT2: The answers solved my needs. But it would be great if somebody would give a command which works exactly like \hfill or i.e. \dotfill in align.

Best Answer

You can do it, but not with \hfill, because of how align works. Here's a way:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\pushright}[1]{\ifmeasuring@#1\else\omit\hfill$\displaystyle#1$\fi\ignorespaces}
\newcommand{\pushleft}[1]{\ifmeasuring@#1\else\omit$\displaystyle#1$\hfill\fi\ignorespaces}
\makeatother

\begin{document}
\begin{align*}
    a + b + c &= a + b + c + d + e + f + g + h + i \\
    & \pushright{\text{(foo)}}
\end{align*}


\begin{align*}
    a + b + c + d + e + f + g + h + i &= a + b + c \\
    \pushleft{\text{(foo)}} &
\end{align*}

\end{document}

enter image description here

A slightly different implementation allows you to use \hfill where you want:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\specialcell}[1]{\ifmeasuring@#1\else\omit$\displaystyle#1$\ignorespaces\fi}
\makeatother

\begin{document}
\begin{align*}
    a + b + c &= a + b + c + d + e + f + g + h + i \\
    & \specialcell{\hfill\text{(foo)}}
\end{align*}


\begin{align*}
    a + b + c + d + e + f + g + h + i &= a + b + c \\
    \specialcell{\text{(foo)}\hfill} \\
    \specialcell{\hfill\text{(foo)}\hfill}
\end{align*}

\end{document}

I'd recommend against using the simple \omit.

enter image description here

Related Question