No, you are not interpreting this correctly, at least not entirely.
MakeBoxes
is always applied recursively, unless the formatting rule for an outer expression decideds hijack the process for an inner expression. If this didn't happen, how could {Red, Disk[]}
display as a list of a red swatch and the word Disk[]
, but Graphics[{Red,Disk[]}]
give you an actual red disk?
This is basically what you are running into here. In fact, your comment above about Times[a, Power[z,-1]]
reducing to Divide[a,z]
is essentially completely reversed. A symbolic fraction is a product of one or more Power
expressions:
In[124]:= a/b //FullForm
Out[124]//FullForm= Times[a,Power[b,-1]]
In[125]:= a /( c b) //FullForm
Out[125]//FullForm= Times[a,Power[b,-1],Power[c,-1]]
For this reason, the formatting rule of Times
explictly ignores the formatting rules of Power
while it's formatting. Your second rule works because it is circurmvents Times
formatting, and in fact it would work just as well for your purposes (and be faster) if you wrote it like this:
MakeBoxes[Times[a___, Power[z, n_Integer]], TraditionalForm] := ToBoxes[a Superscript[z, n], TraditionalForm];
Note that you risk possibly reordering terms in the product this way, but if you always want the z^-1 at the end you're probably OK. Of course, you also need the first rule for when z^-1 appears on its own, or inside a different function which doesn't hijack the recursion (which should be just about all functions).
About MakeBoxes
more generally: MakeBoxes
is a kernel function. The order of evaluation is
- The FE sends boxes to the kernel
- All steps described in "The Main Loop", culminating in complete evaluation of the input and applying $PrePrint.
- The kernel calls MakeBoxes on the result, and transmits the boxes to the FE to be inserted into the notebok.
The FE never "applies" any function, except in certain simple cases inside of Dynamic expressions and controls. If it doubt, it will transmit things to the kernel make sure they are computed properly, and the make use of the result.
MapAll
, i.e., withTraditionalForm//@ (a z^(-1))
formating reaches thePower
s that lie deeper. For example:TraditionalForm //@ ( (a z^(-1) + w^(y^(-2)))^(-3) + x^(-5))
$\endgroup$ – kglr Jul 14 '17 at 18:29