TransWikia.com

flalign in algorithm

TeX - LaTeX Asked on July 17, 2021

This question is a bit related to the answer provided here:
How can I use an align environment flush left?

I use algorithm2e and mathtools to write an algorithm with an equation inside.
I want it to be on the left side. Why I have to add additional & (space after it added for clarity) at the beginning of a line when without algorithm environment I didn’t have to?

MWE:

documentclass{article}
usepackage{mathtools}
usepackage[ruled,vlined]{algorithm2e}

begin{document}

  begin{algorithm}
    begin{flalign*}
    &     mu_beta leftarrow frac{1}{m} sum_{i=1}^{m}{x_1}&
    end{flalign*}
  end{algorithm}

end{document}

One Answer

This has nothing to do with algorithm2e so I'll leave that out. Basically align and its siblings are in fact arrays of successive rl columns. flalign then stretches the intercolumn space between these rl pairs to fill the page width. This works of course only if you have at least two such pairs, meaning that you need at least two &: if you have only one & then you just jumped from the r to the l column, but you still have what amsmath calls "an equation column" (which as I said is realy a pair of columns), and this will be still centered.

documentclass{article}

usepackage{mathtools}
usepackage[showframe,textwidth=8cm]{geometry}

begin{document}

begin{flalign*}
R & L &            R & L 
right & left & right & left
end{flalign*}
%
begin{flalign*}
fooooo & 
baz    &
end{flalign*}
%
begin{flalign*}
& fooooo 
& baz
end{flalign*}
%
begin{flalign*}
& fooooo & 
& baz    & 
end{flalign*}
%
begin{flalign*}
fooooo &&
baz    &&
end{flalign*}

end{document}

enter image description here

Note that in the last two examples it is in principle enough to put the second & in only one row, as that is enough to trigger the spreading mechanism.

Correct answer by campa on July 17, 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