# \hfill in math 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.

-
Would you please make an example of what you'd like to achieve? –  egreg Nov 20 '12 at 13:07
Is it possible that you are trying to tag the equation and the tag doesn't fit on one line so you are doing it manually? –  percusse Nov 20 '12 at 13:23
No, I'm not tagging. It's just some text which I would like to watch right aligned (and other situations, but at this moment it's just this). But everything would be more flexible if \hfill worked. –  Manuel Nov 20 '12 at 13:28
Based on personal experience with this kind of situation, I believe this may be a XY Problem. When I started using LaTeX last year, I spent quite a bit of time with exactly the same issue with align -- only later did I learn that I did not properly understand how to fully use align and the related alignat (and even tabular/array). So, if you show (in perhaps a different question) exactly what you want to achieve (as opposed to how you want to achieve it) you may get better answers. If I am mistaken, please ignore this comment. –  Peter Grill Nov 20 '12 at 18:57
No, you are right. And I haven't write a good explanation because I don't have it. I just needed that at this moment (and may be won't need it again, that's why my intention is not to solve my problem, but discovering \mathhfill). But I thought What if we could simply, without head ache, use hfill,dotfill, etc. in align exactly as we use in text mode? At this moment the @egreg answer fits me, but my real objective is to have a command \mathhfill and \mathdotfill which work exactly like the normal ones, but in align. –  Manuel Nov 20 '12 at 19:24

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}


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.

-
Cool. Well, that works, but I will wait some time just to see if anybody shows a command more like \hfill. i.e. if you write (foo1) \mathhfill (foo2) it will align (foo1) to the left and (foo2) to the right. If not, I'll accept your answer. May be you could addapt \dotfill to work in align, and then use \phantom{\dotfill} –  Manuel Nov 20 '12 at 13:45
Great!. That's much better. –  Manuel Nov 20 '12 at 19:17
Does this work in align environment instead of align*? In align the compilation fails with Misplaced \omit. –  devil Dec 13 '12 at 5:51
@devil If I run both examples with align instead of align* I get no error. –  egreg Dec 13 '12 at 7:39

The most obvious answer is \hfill as demonstrated by this plain TeX file

$a\hfill b$

\bye


-
Well, I didn't tried that, and it works in , but this is not what I want. I would like this to work in align, gather and similar environments. –  Manuel Nov 20 '12 at 13:22

In align you can use \omit at the start of cells:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
a + b + c &= a + b + c + d + e + f + g + h + i \\
&\omit\hfill foo\\
a&=b\\
\omit text & =c \\
\omit\hfill text&=d
\end{align*}
\end{document}

-
That's also great. But it don't work in every case: i.e. (foo1) \omit\hfill (foo2). Anyway, where can I find info about the \omit command? –  Manuel Nov 20 '12 at 13:58
The \omit must be at the begin of the cell (it tells the underlying \halign that in this cell the template should be ignored and is described e.g. in the texbook). Btw: Enrico's solution uses \omit too, it is only a bit more hidden. –  Ulrike Fischer Nov 20 '12 at 14:09
Note that it's only the \omit that has to be first, the hfill can be anywhere  &\omit$a \hfill b$ for example –  David Carlisle Nov 20 '12 at 14:32
@David Carlisle and egreg Okey. I'm pleased enough. I like this mode of working. I thank you all, because the solution is a mix of all the answer. –  Manuel Nov 20 '12 at 15:27
Note that to be really safe you should only do the omit on aligns second pass not on its first so have a macro \@ifmeasuring\else\omit\fi as in @egreg's solution otherwise you omit this cell on the internal measuring phase (which is safe enough unless the cell ends up being the widest in this column in which case that will not be recorded if you omit it) –  David Carlisle Nov 20 '12 at 15:49