TransWikia.com

hfill in math mode

TeX - LaTeX Asked on February 5, 2021

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.

5 Answers

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

$ahfill b$

bye

enter image description here

Answered by David Carlisle on February 5, 2021

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@#1elseomithfill$displaystyle#1$fiignorespaces}
newcommand{pushleft}[1]{ifmeasuring@#1elseomit$displaystyle#1$hfillfiignorespaces}
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@#1elseomit$displaystyle#1$ignorespacesfi}
makeatother

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


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

end{document}

I'd recommend against using the simple omit.

enter image description here

Answered by egreg on February 5, 2021

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         
              &omithfill foo        
             a&=b        
   omit text & =c 
   omithfill text&=d
end{align*}
end{document}

Answered by Ulrike Fischer on February 5, 2021

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.7textwidth 
end{align}

Answered by Ufos on February 5, 2021

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}{%
  ifoddvalue{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}%
}

newcommandrightalignedtext[1]{measureremainder{whatsleft}begin{minipage}[t]{whatsleft}hfill#1hspace*{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}

Answered by Jake Ireland on February 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP