# How to preserve the order of terms in the output

Is there a way in which mathematica can preserve the order of operations while it evaluates an expression?, for instance: In my short example

w = f[r,Θ] ;
lapla12 = -(r/(2 μ)) (1/r D[w/r r, {r, 2}] - 1/r^2 (D[w/r, {Θ, 2}] + Cot[Θ] D[w /r, {Θ, 1}])) // ExpandAll // TraditionalForm


The output that mathematica gave me is

However, I know the real order is

In my real problem, I have more than 100 terms and is quite difficult to me sort all terms to check the results.

EDITED: For instance the first 35 terms in my real expression appears in this order and i would like that they appears in the correct order

I'm using the code from Jens for derivatives formatting purposes.

Here is the code i'm using to obtain the above output.

ClearAll["Global*"]
SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Condensed"]$PrePrint = # /. {Csc[z_] :> 1/Defer@Sin[z],
Sec[z_] :> 1/Defer@Cos[z],
Cot[z_] :> Defer@Cos[z]/Defer@Sin[z]} &;

Subscript[w, 1] = f[Subscript[r, 1], Subscript[r, 2], α, β, γ, Subscript[Θ, 12]];

Set[J, -(D[Subscript[w, 1], {β, 2}] + 1/Sin[β]^2 (D[Subscript[w, 1], {α, 2}] +
D[Subscript[w, 1], {γ, 2}]) + Cot[β] D[Subscript[w, 1], β] -
2 Cot[β]/ Sin[β] D[Subscript[w, 1], α, γ])] // TraditionalForm ;

Set[Subscript[a, 1], 2 D[Subscript[w, 1], {β, 2}] + D[Subscript[w, 1], {γ, 2}] + J]

Set[Subscript[a, 2],  2 Cot[β]/ Sin[β] D[Subscript[w, 1], {α, 1}] - (1 + 2 Cot[β]^2)
D[Subscript[w, 1], {γ, 1}] - 2/Sin[β] D[Subscript[w, 1], β, α] + 2 Cot[β]
D[Subscript[w, 1], β, γ]] // TraditionalForm ;

Set[Subscript[f, 1], 1/(2 Sin[Subscript[Θ, 12]]^2) (-J + Cos[Subscript[Θ, 12]]
(Sin[2 γ] Subscript[a, 2] - Cos[2 γ] Subscript[a, 1]) + Sin[Subscript[Θ, 12]]
(Sin[2 γ] Subscript[a, 1] + Cos[2 γ] Subscript[a, 2])) - D[Subscript[w, 1],
Subscript[Θ, 12], γ] - 1/2 Cot[Subscript[Θ, 12]] D[Subscript[w, 1], {γ, 1}] +
(1/4 - 1/(2 Sin[Subscript[Θ, 12]]^2)) D[Subscript[w, 1], {γ, 2}]
] // ExpandAll // TraditionalForm ;

Set[lapla1, -(1/(2 Subscript[μ, 1])) (1/Subscript[r, 1] D[Subscript[w, 1]
Subscript[r, 1], {Subscript[r, 1], 2}] - 1/Subscript[r, 1]^2
(D[Subscript[w,  1], {Subscript[Θ, 12], 2}] + Cot[Subscript[Θ, 12]]
D[Subscript[w, 1], {Subscript[Θ, 12], 1}]) - Subscript[f, 1]/Subscript[r, 1]^2)


EDITED 2 In this case the correct order is ruled by the expression for lapla1.

where terms in red (radial terms) go first, then terms in purple (internal angular terms), and finally the terms in blue (coupling between the orientational angular terms and the internar angular terms). Blue terms are ruled by the expression for $f_1$ from left to right.

• You problem is very similar to the one answered in this post. Mar 31, 2014 at 10:10
• Mar 31, 2014 at 11:41
• @Artes Actually my problem is not related with the traditional outputs for derivatives, as you can see above i added a picture of my real problem. Mar 31, 2014 at 12:49
• Are you checking the terms by hand? Mar 31, 2014 at 12:54
• @MichaelE2 Yes, I need to do it, this will be the input to other program, and i need to introduce it in the correct order. Mar 31, 2014 at 12:57

This is a quick possibility:

myExpand[expr_] := Module[{tmp},
Unprotect[Plus];
ClearAttributes[Plus, Orderless];
tmp = HoldForm @@ {Expand[expr] /. a_Plus :> Reverse[a]};
SetAttributes[Plus, Orderless];
tmp];

w = f[r, Θ];
lapla12 = -(r/(2 μ)) (1/r D[w/r r, {r, 2}] - 1/r^2 (D[w/r, {Θ, 2}] +
Cot[Θ] D[w/r, {Θ, 1}])) // myExpand

• I really like the trick +1 :), but I'm not sure if OP too, in the long expr there are cases with (a + b) D[f, x]` which are separated so it will be though to achieve what OP needs. Not your fault of course, it's the question that is vague.
– Kuba
Mar 31, 2014 at 21:35