If I write
x = -(1 - 2 G M/r) \[DoubleStruckD]t^2 + (1 -
2 G M/r)^-1 \[DoubleStruckD]r^2 + r^2 \[DoubleStruckD]θ^2 +
r^2 Sin[θ]^2 \[DoubleStruckD]ϕ^2;
x
I see
When I write x
(or perhaps x//SpecialDisplay
), I want to see
that is, I want expressions containing dt to come first, and then expressions containing dr, and then expressions containing dθ, and then expressions containing dϕ. I know I can write this as
MyPlus[var_][v___] :=
With[{has = Select[{v}, Not[FreeQ[#, var]] &],
nothas = Select[{v}, FreeQ[#, var] &]},
If[Length[has] > 0 && Length[nothas] > 0,
Row[{Plus @@ has, " + ", Plus @@ nothas}],
Plus[v]
]
];
Pull[x_, var_] := x /. Plus -> MyPlus[var]
SpecialDisplay[x_] :=
Fold[Pull[#1, #2] &,
x, {\[DoubleStruckD]t, \[DoubleStruckD]r, \[DoubleStruckD]θ, \
\[DoubleStruckD]ϕ}]
and I'm wondering if there's a simpler way to do it by, e.g., changing the canonical ordering of variables, or replacing variables with ones with the correct ordering and then freezing the order and then putting them back, or something.
SortBy[List @@ x, {FreeQ[#, \[DoubleStruckD]t] & ,FreeQ[#, \[DoubleStruckD]r]&,FreeQ[#,\[DoubleStruckD]\[Theta]]& ,FreeQ[#,\[DoubleStruckD]\[Phi]]&}] /. List -> Inactive[Plus]
? $\endgroup$