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]]]


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]]

TraditionalForm[poly /. Plus :> plus]

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 enter image description here Do you have any help or suggestions ?

  • $\begingroup$ @halirutan can you help me $\endgroup$ Mar 25, 2017 at 9:13
  • $\begingroup$ 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. $\endgroup$
    – vapor
    Mar 25, 2017 at 9:53

2 Answers 2


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.


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_] := 
     expr /. Subscript[a_, b_] :> 
       ToString[a] <> "$" <> ToString[b]]] /. 
  a_?(StringQ@# && StringMatchQ[#, __ ~~ "$" ~~ __] &) :> 
    Subscript @@ ToExpression /@ StringSplit[ToString[a], "$"]]

The result is as follows:


$$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$$


If you must have terms sorted by the power of $t$ in reverse order,

normalOrderReverse[expr_] := 
       expr /. Subscript[a_, b_] :> 
         ToString[a] <> "$" <> ToString[b]]] /. 
     Verbatim[Plus][a___] :> RuleCondition[Reverse@foo[a]] /. 
    foo -> Plus /. 
   a_?(StringQ@# && StringMatchQ[#, __ ~~ "$" ~~ __] &) :> 
     Subscript @@ ToExpression /@ StringSplit[ToString[a], "$"]]]


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.

  • $\begingroup$ 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,...$ $\endgroup$ Mar 25, 2017 at 10:40
  • 1
    $\begingroup$ Try this: SetOptions[PolynomialForm, TraditionalOrder -> True]; Collect[poly, t] // PolynomialForm $\endgroup$ Mar 25, 2017 at 10:58
  • 1
    $\begingroup$ @J.M. Ahh, I don't know such a form exists... $\endgroup$
    – vapor
    Mar 25, 2017 at 11:00
  • 1
    $\begingroup$ @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... $\endgroup$
    – vapor
    Mar 25, 2017 at 11:10
  • 1
    $\begingroup$ I'd prefer that you append this to your answer... :) $\endgroup$ Mar 25, 2017 at 11:23

since it is J.M.'s answer.

SetOptions[PolynomialForm, TraditionalOrder -> True]; Collect[poly, t] // PolynomialForm
  • $\begingroup$ Since this is J.M.'s comment, it should be acknowledged in the answer. $\endgroup$
    – bbgodfrey
    Mar 25, 2017 at 12:00
  • $\begingroup$ @bbgodfrey Sorry just my intention was that the best answer $\endgroup$ Mar 25, 2017 at 12:02
  • $\begingroup$ @Emadkareem You should make this community wiki since it is J.M.'s answer. $\endgroup$
    – vapor
    Mar 25, 2017 at 12:09
  • $\begingroup$ @happyfish How to do it. Do you delete the answer? I am a beginner in mathematica.stackexchange. $\endgroup$ Mar 25, 2017 at 12:13
  • $\begingroup$ @Emadkareem simply edit your question, and check the "community wiki" box in this position $\endgroup$
    – vapor
    Mar 25, 2017 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.