# How to simplify and rearrange a mathematical expression with quantities that have subscripts

I'm trying to simplify and rearrange the mathematical expression

$$\text{d\xi } \left(k_1+k_2\right)+\text{dt} \left(k_1 \left(l_1-x_1\right)\right)+\text{dt} \left(k_2 \left(x_2-l_2\right)\right)$$,

to obtain the following

$$\left(k_1+k_2\right) \text{d\xi }+\left(k_1 \left(l_1-x_1\right)+k_2 \left(-l_2+x_2\right)\right) \text{dt}$$

Is there a way to achieve this result in Mathematica?

• Do you only need to print? Because with a rule that includes Holdform it can be achieved. Commented Jul 16 at 21:01
• Yes, up to now I just require to print it. Commented Jul 16 at 21:05
• Do you really need the differentials to be at the end? Manipulating the order of factors in Mathematica is always painful ... If you just need to collect the terms, use Collect[expr, {dξ, dt}]. Commented Jul 18 at 8:31
• Yes, thanks so much. It is just a matter of aesthetics... Commented Jul 18 at 17:07

If you just need to print that result, you can do the following:

expr = dξ (Subscript[k, 1] + Subscript[k, 2]) +
dt*Subscript[k, 1] (Subscript[l, 1] - Subscript[x, 1]) +
dt*Subscript[k, 2] (Subscript[x, 2] - Subscript[l, 2]);

fun = Function[a,
If[Head[a] === Times, List @@ a /. {s1_, s2_} :> HoldForm[s2*s1], a], Listable];

Total@fun@(List @@ Collect[expr, {dt, dξ}, FullSimplify])// TeXForm


$$\left(k_1+k_2\right) \text{d\xi }+\left(k_1 \left(l_1-x_1\right)+k_2 \left(-l_2+x_2\right)\right) \text{dt}$$

Generalized version to cover complicated cases

To order in the form coefficient*differential, define PrintTradOrder as follows:

Format[PrintTradOrder[a_, b_]] := Row[{a, Style["*", White, FontSize -> 5], b}];


The last step is to use PrintTradOrder to organize the expressions of interest term by term:

NegGroupHold[expr_] :=
If[AllTrue[Level[expr, {1}], InternalSyntacticNegativeQ@# &] &&
a_ :> RuleCondition@(-a), {1}], expr];

sNeg = InternalSyntacticNegativeQ@# &;

OrganizeTerms[expr_, vars_?VectorQ] /; {expr} === vars := expr;

OrganizeTerms[expr_, vars_?VectorQ] /;
VectorQ[Coefficient[expr, vars], NumericQ] := expr;

OrganizeTerms[expr_, vars_?VectorQ] :=
Module[{collexpr, polvars, filtervars, coeffs, coeffvars, terms,
snegative},
collexpr = Collect[expr, vars, FullSimplify];
polvars =
Cases[Cases[collexpr, x_ /; StringContainsQ[ToString[x], "d"]],
x_ /; StringContainsQ[ToString[x],
filtervars =
DeleteCases[polvars, Alternatives @@ Complement[polvars, vars]];
coeffs = NegGroupHold /@ (Coefficient[collexpr, #] & /@ filtervars);
terms =
If[NumericQ[#1], Times @@ {#1, #2},
PrintTraditionalOrder[Times[#1, ""], #2]] & @@@ coeffvars;
snegative = sNeg /@ coeffs;
If[(snegative === {False, True} || snegative === {True, True}) &&
Length@coeffs > 1&& Head[coeffs[[2]]] === Times,
Row[{#1, "", #2}] & @@ terms, Total@terms]
];


Testing OrganizeTerms:

OrganizeTerms[expr, {dξ, dt}] // TeXForm


$$\text{} \left(k_1+k_2\right)*\text{d\xi }+\text{} \left(k_1 \left(l_1-x_1\right)+k_2 \left(x_2-l_2\right)\right)*\text{dt}$$

An image of the output:

I've run multiple tests to ensure it handles the most complicated cases well and so far it's working fine.

• Once again, thank you so much for your help! It works! Commented Jul 16 at 21:45