removed
With more variable you have to decide how to deal with mixed terms x1*x2
Sorry, I had to clarify this issue. The second-order terms specified in the command should be collected as usual:
Collect[..., x1^2, x2^2, x1*x2, ...]
A similar problem was discussed in the thread "How to Get a Taylor Series for Multiple Variables?".
I don't know if this is straightforward enough
Collect[a*x + b*x + c*x^2 + d*x^2, x] /. coeff_*x :> Expand[coeff*x]
Thank you, enough straightforward. Can the code be extended for expression with several variables (i.e., x1^2, x2^2, ..., xn^2)?