Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When I use RowBox for MakeBoxes the order of the elements is changed for TraditionalForm, if there is a plus (+) in the list of elements.

MakeBoxes[x, form_] = RowBox[{"b", "+", "a"}];
MakeBoxes[x, StandardForm]
MakeBoxes[x, TraditionalForm]
(* Out[2] = RowBox[{"b", "+", "a"}]
   Out[3] = RowBox[{"a", "+", "b"}] *)

Sometimes Mathematica really makes me wonder. Does this only happen when there is a plus in RowBox? Is there any way to fix this?

share|improve this question
3  
FWIW, Block[{TraditionalFormDump`$UseNewTraditionalForm = False},MakeBoxes[x, TraditionalForm]] returns RowBox[{"b", "+", "a"}]. –  Leonid Shifrin Oct 7 '13 at 17:01
2  
FWIW^2, associating the definition to x, which I think it's a good idea, returns what you expect –  Rojo Oct 7 '13 at 17:04
    
@Leonid, you are using variables The Google does not know about, therefor they do not exist. :-) But you are right. Where can you read about $UseNewTraditionalForm? –  Friedrich Oct 7 '13 at 17:27
1  
@Mr.Wizard I haven't tested, but probably attaching it to TraditionalForm, or even to MakeBoxes but explicitly as DownValues, may work –  Rojo Oct 7 '13 at 22:04
1  
@Rojo thanks for correcting me –  Mr.Wizard Oct 8 '13 at 1:51
show 4 more comments

2 Answers

up vote 5 down vote accepted

When I see this right, then the evil function is TraditionalFormDump`ordplus. This seems to change the order. The arguments can be extracted from a Trace

TraditionalForm[a + b]; (* Dummy call *)
TraditionalFormDump`ordplus[{{"+", "b"}, {"+", "a"}}, {}]

(* {2,1} *)

If we change this to give a sorted list, then your arguments are not reordered

ClearAll[TraditionalFormDump`ordplus]
TraditionalFormDump`ordplus[l1_, _] := Range[Length[l1]]

MakeBoxes[x, form_] = RowBox[{"b", "+", "a"}];
MakeBoxes[x, StandardForm]
MakeBoxes[x, TraditionalForm]

(* 
  RowBox[{"b", "+", "a"}]
  RowBox[{"b", "+", "a"}]
*)
share|improve this answer
    
Impressive! How did you find out about TraditionalFormDump`ordplus? A trace does not give me this function. Do you have any idea why this does not happen when I associate the definition to x as Rojo mentioned? –  Friedrich Oct 7 '13 at 18:07
1  
@Friedrich I believe it does not happen because definitions associated with x are tried before those associated with TraditionalForm (just like with UpValues), and this special bothersome definition is attached to TraditionalForm's FormatValues. Values associated with MakeBoxes (such as your definitions) are tried last. –  Rojo Oct 7 '13 at 20:41
1  
@Friedrich You have to use TraceInternal->True as option to Trace. –  halirutan Oct 7 '13 at 21:20
add comment

halirutan gave a very nice explanation why this only happens in TraditionalForm and not in StandardForm. For completeness I will add the workaround that Rojo mentioned: Associate the definition to x and not to MakeBoxes using UpSet:

MakeBoxes[x, form_] ^= RowBox[{"b", "+", "a"}];

Association with TraditionalForm also works:

Unprotect[TraditionalForm];
TraditionalForm /: MakeBoxes[x, TraditionalForm] = RowBox[{"b", "+", "a"}];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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