66

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.

4
  • 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
    Commented Nov 20, 2012 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
    Commented Nov 20, 2012 at 13:28
  • 1
    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. Commented Nov 20, 2012 at 18:57
  • 1
    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
    Commented Nov 20, 2012 at 19:24

5 Answers 5

51

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

7
  • 1
    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
    Commented Nov 20, 2012 at 13:45
  • 1
    Great!. That's much better.
    – Manuel
    Commented Nov 20, 2012 at 19:17
  • 7
    Does this work in align environment instead of align*? In align the compilation fails with Misplaced \omit.
    – devil
    Commented Dec 13, 2012 at 5:51
  • 1
    @devil If I run both examples with align instead of align* I get no error.
    – egreg
    Commented Dec 13, 2012 at 7:39
  • Funny, this seems a bit like re-inventing the wheel for the aligned environment. Indeed, the commands shoveleft and shoveright basically do the same in the multlined environment. See tex.stackexchange.com/questions/58529/… That being said, it works great.
    – mranvick
    Commented Aug 16, 2023 at 7:18
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}
7
  • 2
    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
    Commented Nov 20, 2012 at 13:58
  • 2
    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. Commented Nov 20, 2012 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 Commented Nov 20, 2012 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
    Commented Nov 20, 2012 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) Commented Nov 20, 2012 at 15:49
6

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

$a\hfill b$

\bye

enter image description here

1
  • 13
    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
    Commented Nov 20, 2012 at 13:22
6

Another way (the only one that worked for me) is to use \hskip. You will have to adjust it manually, though.

\begin{align}
     & x + y, &  \text{(Foo)} \hskip0.7\textwidth 
\end{align}
2
  • 1
    Why not \hspace or \hspace*?
    – Werner
    Commented Mar 10, 2016 at 17:08
  • 4
    works fine as well. Syntax: hspace{X pt/em/cm} . But it just gives you the same result -- you have to adjust it manually to get the \hfill effect.
    – Ufos
    Commented Mar 11, 2016 at 9:33
4

I couldn't for the life of me get @egreg's solution to work for me: always got misplaced \omit, no matter what I tried. I ended up finding a little bit of a hacky solution, but it works for me. It uses some code found here. Here's the MWE:

\documentclass{article}

\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\currentsidemargin}{%
  \ifodd\value{page}%
    \oddsidemargin%
  \else%
    \evensidemargin%
  \fi%
}

\newlength{\whatsleft}

\newcommand{\measureremainder}[1]{%
\begin{tikzpicture}[overlay,remember picture]
    % Helper nodes
    \path (current page.north west) ++(\hoffset, -\voffset)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, minimum height=\paperheight]
        (pagearea) {};


    \path (pagearea.north west) ++(1in+\currentsidemargin,-1in-\topmargin-\headheight-\headsep)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\textwidth, minimum height=\textheight]
        (textarea) {};

    % Measure distance to right text border
    \path let \p0 = (0,0), \p1 = (textarea.east) in
        [/utils/exec={\pgfmathsetlength#1{\x1-\x0}\global#1=#1}];

\end{tikzpicture}%
}

\newcommand\rightalignedtext[1]{\measureremainder{\whatsleft}\begin{minipage}[t]{\whatsleft}\hfill#1\hspace*{1em}\end{minipage}}

\begin{document}

\begin{equation*}
    \begin{aligned}
        a + b + c &= a + b + c + d + e + f + g + h + i \rightalignedtext{(foo)}
    \end{aligned}
\end{equation*}

\end{document}
1
  • 1
    Thanks for that! I couldn't get egreg's solution to work either. Commented Oct 9, 2023 at 0:58

You must log in to answer this question.

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