# How can I arrange linear equation with "Collect" in Mathematica?

I was asked a question about the order of linear equations and got this code(@halirutan ) to arrange the equation :

    plus /: MakeBoxes[plus[args__], form_] :=
With[{niceFormatting =
RowBox[Riffle[MakeBoxes[#, form] & /@ {args}, "+"]]},
InterpretationBox[niceFormatting, Plus[args]]]


where

Subscript[R, 2] := Subscript[a, 2] t + Subscript[b, 2];
Subscript[R, 3] := Subscript[a, 3] t + Subscript[b, 3];

poly = Expand[Subscript[R, 2] Subscript[R, 3]]



The order of the equation shall be $$a_2 a_3 t^2+ a_3 b_2t+a_2 b_3t+b_2 b_3$$

I need to add a restrcuturing (Collect) in the code

In my attempts I suggested it be so

poly = Collect[Expand[Subscript[R, 2] Subscript[R, 3]],t]


$$a_2 a_3 t^2+b_2 b_3+t \left(a_3 b_2+a_2 b_3\right)$$

but,It does not result in the desired result. I want him to be in this configuration $$a_2 a_3 t^2+ \left(a_2 b_3+a_3 b_2\right)t+b_2 b_3$$

To arrange linear equations in general. As an example Do you have any help or suggestions ?

• @halirutan can you help me Mar 25, 2017 at 9:13
• You should understand how this code works, the code prevents TraditionalForm from rearranging terms. However, the expression you got from Collect is already of that form, so this code will do nothing. Mar 25, 2017 at 9:53

You can use function Exponent to get the power of t and sort it. With some small modifications, you can obtain this.

expr = Collect[Expand[Subscript[R, 2] Subscript[R, 3]], t];
(expr /. Plus -> plus // Reverse@*SortBy[Exponent[#, t] &]) // TraditionalForm


$$a_2 a_3 t^2+t\left(a_3 b_2+a_2 b_3\right)+b_2 b_3$$ If you are not satisfied with the position of t, I will try to modify it.

## Update

This strange ordering is caused by the usage of Subscript. I have made a more general function, that prints out the form as if no subscript is used

normalOrder[expr_] :=
Reverse@HoldForm[
Evaluate[
expr /. Subscript[a_, b_] :>
ToString[a] <> "$" <> ToString[b]]] /. a_?(StringQ@# && StringMatchQ[#, __ ~~ "$" ~~ __] &) :>
RuleCondition[
Subscript @@ ToExpression /@ StringSplit[ToString[a], "$"]]  The result is as follows: normalOrder[poly]//TraditionalForm  $$b_2 b_3+a_3 b_2 t+a_2 b_3 t+a_2 a_3 t^2$$ normalOrder[Collect[poly, t]]//TraditionalForm  $$b_2 b_3+\left(a_3 b_2+a_2 b_3\right) t+a_2 a_3 t^2$$ ## Update2 If you must have terms sorted by the power of$t$in reverse order, normalOrderReverse[expr_] := Module[{foo}, HoldForm[Evaluate[ expr /. Subscript[a_, b_] :> ToString[a] <> "$" <> ToString[b]]] /.
Verbatim[Plus][a___] :> RuleCondition[Reverse@foo[a]] /.
foo -> Plus /.
a_?(StringQ@# && StringMatchQ[#, __ ~~ "$" ~~ __] &) :> RuleCondition[ Subscript @@ ToExpression /@ StringSplit[ToString[a], "$"]]]


Now

normalOrderReverse[poly] // TraditionalForm


$$a_2 a_3 t^2+a_2 b_3 t+a_3 b_2 t+b_2 b_3$$

normalOrderReverse[Collect[poly, t]] // TraditionalForm


$$a_2 a_3 t^2+\left(a_3 b_2+a_2 b_3\right) t+b_2 b_3$$

Actually that Reverse can be replaced to use other sorting functions.

• thanks for your efforts . But I am arranging the equation according to the logic of mathematics .... It must have a variable t that holds the upper powers at first ,where $a_i,b_i$ are constant ,$i=2,3,...$ Mar 25, 2017 at 10:40
• Try this: SetOptions[PolynomialForm, TraditionalOrder -> True]; Collect[poly, t] // PolynomialForm Mar 25, 2017 at 10:58
• @J.M. Ahh, I don't know such a form exists... Mar 25, 2017 at 11:00
• @J.M. You should post that as an answer, so others can get to the right solution easily. Otherwise they could be confused by my spaghetti code... Mar 25, 2017 at 11:10
• I'd prefer that you append this to your answer... :) Mar 25, 2017 at 11:23

SetOptions[PolynomialForm, TraditionalOrder -> True]; Collect[poly, t] // PolynomialForm