Add a SortOrder
property to determine how you want symbols to be sorted, and then use the sort_product
algorithm:
{\mu,\nu}::Indices(vector).
{a,b,a{#},b{#}}::SortOrder.
{a^{\mu},b^{\mu}}::NonCommuting.
ex:=a b a^{\mu} a b_{\mu} b+a b b^{\mu} b a_{\mu} a;
sort_product(_);
which produces $$aabb a^{\mu} b_{\mu} + aabb b^{\mu} a_{\mu} .$$ If you want to do it, instead, with factor_out(_, $a,b$)
, then you will need a call to distribute(_)
afterwards to get rid of the brackets again.