I'm a strong proponent of unit tests and as such I'm using the MUnit framework from Wolfram Workbench on a regular basis. Many of the codes I'm working on also use TraditionalForm formatting in order to make objects look nicer in the Mathematica notebooks. Some of the formatting,, rules are pretty complicated which means that it would be nice to have unit tests for them as well.

However, I don't see a simple way to implement unit tests to check the TraditionalForm output of particular expression. For example, let us look at


Now I would like to write two tests like (pseudocode):

Representation[MyObject[1,1]] === SuperscriptBox[\[Alpha],1]
Representation[MyObject[1,2]] === SuperscriptBox[\[Alpha],2]

The best thing I was able to come up with is

TraditionalForm[MyObject[1,1]] === TraditionalForm[SuperscriptBox[\[Alpha],1]//DisplayForm]
TraditionalForm[MyObject[1,2]] === TraditionalForm[SuperscriptBox[\[Alpha],2]//DisplayForm]

Which however still doesn't return True. When I run Mathematica kernel without the frontend I see that both




evaluate to

//TraditionalForm= α^1

but for some reason Mathematica doesn't consider them to be equal.

Is there some handy function to output the TraditionalForm of MyObject[1,1] and MyObject[1,2] in terms of SuperscriptBox and \[Alpha]?

Or may be there is some standard way to write unit tests for the TraditionalForm representation that I'm completely missing?


1 Answer 1


TraditionalForm and DisplayForm are just "wrappers". If you look at FullForm of expressions compared in question, you'll see that although they look the same, internally they are represented differently:

MyObject /: MakeBoxes[MyObject[x_, y_], TraditionalForm] :=
    If[x === y, SuperscriptBox["α", 1], SuperscriptBox["α", 2]]

TraditionalForm[MyObject[1, 1]]
TraditionalForm[MyObject[1, 1]] // FullForm
    TraditionalForm[MyObject[1, 1]]

TraditionalForm[SuperscriptBox["α", 1] // DisplayForm]
TraditionalForm[SuperscriptBox["α", 1] // DisplayForm] // FullForm
    TraditionalForm[DisplayForm[SuperscriptBox["\[Alpha]", 1]]]

If you want to compare box representation of expressions, you should explicitly convert them to boxes.

MakeBoxes[MyObject[1, 1], TraditionalForm] === SuperscriptBox["α", 1]
(* True *)

ToBoxes[MyObject[1, 1], TraditionalForm] === FormBox[SuperscriptBox["α", 1], TraditionalForm]
(* True *)
  • $\begingroup$ Thanks, this actually solves my problem! $\endgroup$
    – vsht
    Dec 5, 2014 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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