# How to rearrange the terms in polynomials to the power of x?

Here is my code

Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y]

I got

6 - 3 x^2 == 2 n^2 + 4 n x

But I wanna it to be be this
$$3 x^2 + 4 n~x + 2 n^2 -6=0$$ I tried this code

Collect[6 - 3 x^2 == 2 n^2 + 4 n x, x]

Nothing change with this code. What's wrong with it?

Based on the OP's comments, the central issue is not how to move all terms to the left side of the equation, which can be done in many ways. For instance,

-Subtract @@ Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y] == 0
(* -6 + 2 n^2 + 4 n x + 3 x^2 == 0*)


Instead, the issue is, how to keep Mathematica from displaying the result in canonical order, as above. This was addressed sometime ago here, but without an integer term. A simple approach is to generalize the earlier solution as follows:

newreorderSymbols[expr_, symbols_List] := With[{s = symbols, i = First@Cases[expr, _Integer]},
HoldForm[Evaluate[(expr /. i -> z /. Thread[s -> Sort@s])]] /. Thread[Sort@s -> s] /. z -> i]

-Subtract @@ Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y];
newreorderSymbols[%, {x, n}] == 0
(* 3 x^2 + 4 x n + 2 n^2 - 6 == 0*)


but, it contains x n instead of n x. The precise desired result instead can be obtained from

-Subtract @@ Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y];
(HoldForm[Evaluate[Reverse[List @@ %]]] /. List -> Plus) == 0
(* 3 x^2 + 4 n x + 2 n^2 - 6 == 0*)


It is important to remember that, in either case, HoldForm modifies how the answer is displayed, and not the underlying answer itself.

Here's a form that forces polynomials to use your order for a given variable:

polyForm /: MakeBoxes[polyForm[e_, x_], form_] := With[{n = Hold[e] /. p_Plus :> xForm[p, x]},
Replace[n, Hold[s_] :> MakeBoxes[s, form]]
]

xForm /: MakeBoxes[xForm[poly_, x_], form_] := With[{s = Series[poly, {x, Infinity, 0}]},
Replace[
MakeBoxes[s, form],
InterpretationBox[
RowBox[a_],
__
] :> RowBox[a[[;;-3]]]
]
]


Using one of the other answers to create the desired equation, e.g.:

eqn = -Subtract @@ Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y] == 0


-6 + 2 n^2 + 4 n x + 3 x^2 == 0

Then, using polyForm:

polyForm[eqn, x]


3 x^2 + 4 n x + (-6 + 2 n^2) == 0

Another possibility is to use TraditionalForm:

TraditionalForm[eqn, ParameterVariables->{n}]


3 x^2+4 n x+2 n^2-6==0

You can use SubtractSides:

SubtractSides @ Eliminate[y == x + n && x^2/6 + y^2/3 == 1, y]


6 - 2 n^2 - 4 n x - 3 x^2 == 0

• But I want this sequence $$3 x^2 + 4 n~x + 2 n^2 -6=0$$ – kile May 29 '19 at 11:19

If the result is only to be used for display purposes then

TraditionalForm[6-3 x^2==2 n^2+4 n x/.left_==right_->right-left==0, ParameterVariables->{n}]


seems to display the desired

3 x^2 + 4 n x + 2 n^2 - 6 == 0


without needing MakeBoxes and Hold and all that brings to a new user.

But a new user almost certainly needs to be cautioned if the next thing they expect to do is use these results in further calculations.

Try (workaround)

0==(6 - 3 x^2 == 2 n^2 + 4 n x /. Equal -> Subtract)
(*0 == 6 - 2 n^2 - 4 n x - 3 x^2*)

• I want this order $$3 x^2 + 4 n~x + 2 n^2 -6=0$$ – kile May 29 '19 at 11:18