You can also interfere with the sorting done by TraditionalForm
by wrapping your boxes in TagBox
, InterpretationBox
or TemplateBox
:
MakeBoxes[x, form_] = TagBox[RowBox[{"b", "+", "a"}], #&];
MakeBoxes[x, StandardForm]
MakeBoxes[x, TraditionalForm]
TagBox[RowBox[{"b", "+", "a"}], #1 &]
TagBox[RowBox[{"b", "+", "a"}], #1 &]
(I would actually use a TagSet
here, but for pedagogical reasons I avoided it.) This avoids the need to modify an internal Mathematica symbol, as in the accepted answer (there may also be good examples where the reordering is beneficial, as in multivariate polynomials). Also, as Mr.Wizard points out, the other answers using TagSet
only work when x is at the top-level. For example:
y /: MakeBoxes[y, form_] = RowBox[{"b", "+", "a"}];
MakeBoxes[y, TraditionalForm]
MakeBoxes[{y}, TraditionalForm]
RowBox[{"b", "+", "a"}]
RowBox[{"{", RowBox[{"a", "+", "b"}], "}"}]
With the TagBox
approach, the non-top level example still works:
MakeBoxes[{x}, TraditionalForm]
RowBox[{"{", TagBox[RowBox[{"b", "+", "a"}], #1 &], "}"}]
Block[{TraditionalFormDump`$UseNewTraditionalForm = False},MakeBoxes[x, TraditionalForm]]
returnsRowBox[{"b", "+", "a"}]
. $\endgroup$x
, which I think it's a good idea, returns what you expect $\endgroup$MakeBoxes
but explicitly asDownValues
, may work $\endgroup$