# Expanding an expression in the order they appear?

Say we wish to expand the following:

Expand[(1 -
1/S) (b - \[Beta] \[Tau] S f[
i_] - (\[CurlyPhi] + Subscript[c,
1] + \[Mu]) S + \[Theta] V + \[Eta] R) + (1 -
1/V) (\[CurlyPhi] S - (\[Theta] + Subscript[c,
2] + \[Mu]) V) + (1 -
1/i) (\[Beta] \[Tau] S f[
i_] - (Subscript[\[Alpha], 1] + Subscript[c,
3] + \[Gamma] + \[Mu]) i) + (1 - 1/Q) (Subscript[c, 1] S +
Subscript[c, 2] V +
Subscript[c, 3]
i - (\[Psi] + Subscript[\[Alpha], 2] + \[Mu]) Q) + (1 -
1/R) (\[Psi] Q + \[Gamma] i - (\[Eta] + \[Mu]) R)]


Doing so results in:

b - b/S + \[Gamma] - (i \[Gamma])/R + \[Eta] - (
R \[Eta])/S + \[Theta] - (V \[Theta])/S + 5 \[Mu] - i \[Mu] -
Q \[Mu] - R \[Mu] - S \[Mu] - V \[Mu] + \[CurlyPhi] - (
S \[CurlyPhi])/V + \[Psi] - (Q \[Psi])/R + \[Beta] \[Tau] f[i_] - (
S \[Beta] \[Tau] f[i_])/i + Subscript[c, 1] - (
S Subscript[c, 1])/Q + Subscript[c, 2] - (
V Subscript[c, 2])/Q + Subscript[c, 3] - (
i Subscript[c, 3])/Q + Subscript[\[Alpha], 1] -
i Subscript[\[Alpha], 1] + Subscript[\[Alpha], 2] -
Q Subscript[\[Alpha], 2]


The terms are all "jumbled" so my question is: how would we expand it in order they appear? That is, expand the first bracket $$\big((1-1/s)(...)\big)$$ then the second etc such that the final result is in chronology.

• You can uses cases to expand each term. But the problem is when you add them all together, you will get back the same result as doing Expand on the whole thing. It is just the way Mathematica arranges the expression internally. btw, you should avoid using _ in your variable names f[i_] Feb 2 at 16:39
• @Nasser The issue is that they are connected, some terms will kill each other in different expressions, hence why I wanted them to appear in order!
– Math
Feb 2 at 16:41
• @Nasser They were taken from another answer by Daniel I believe but I will take on board your advice.
– Math
Feb 2 at 16:42
• Plus is Orderless. You can't use Plus as the head, if you wish to maintain a custom order. You can use other things, such as Inactive[Plus] as the head. You could try Inactive[Plus] @@ Expand[List @@ expr] -- maybe you'd like the result, maybe not. Feb 2 at 16:48
• @MichaelE2 Could you put it as an answer so I can view it?
– Math
Feb 2 at 16:56

If you want to have a look at those terms in order of their appearance, try the following: This is your expression:

expr1 = (1 -
1/S) (b - \[Beta] \[Tau] S f[
i] - (\[CurlyPhi] +
Subscript[c, 1] + \[Mu]) S + \[Theta] V + \[Eta] R) + (1 -
1/V) (\[CurlyPhi] S - (\[Theta] +
Subscript[c, 2] + \[Mu]) V) + (1 -
1/i) (\[Beta] \[Tau] S f[
i] - (Subscript[\[Alpha], 1] +
Subscript[c, 3] + \[Gamma] + \[Mu]) i) + (1 -
1/Q) (Subscript[c, 1] S + Subscript[c, 2] V +
Subscript[c,
3] i - (\[Psi] + Subscript[\[Alpha], 2] + \[Mu]) Q) + (1 -
1/R) (\[Psi] Q + \[Gamma] i - (\[Eta] + \[Mu]) R);


Note that in this context, f[i_] looks strange. I corrected, therefore, for f[i]. If this has been done intentionally, correct it back.

Now this will expand, keeping the order of terms

expr2 = Map[HoldForm[Evaluate[Expand[#]]] &, expr1]


yielding the following result (which I show as the image to be better visible):

Now one can see the terms. However, one cannot operate this expression due to the HoldForm operation used inside. To operate, one needs to apply ReleaseHold. However, after this, all terms will be rearranged according to the internal order of Mma.

Not sure what the OP expects, but as requested:

Inactive[Plus] @@ Expand[List @@ expr]


where expr is the expression to be expanded in the question.

I don't have time now to work up a full answer, so I'm making this CW. My intention in my comment under the question was for the OP to test and see if it produced the desired output or not. If not, then I would have just ignored it.