MakeBoxes vs Format
- To: mathgroup at smc.vnet.net
- Subject: [mg60073] MakeBoxes vs Format
- From: ted.ersek at tqci.net
- Date: Thu, 1 Sep 2005 02:13:09 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
We can use the following to change the way things
like (x/y) are displayed in output.
In[1]:=
(** MakeBoxes definition **)
MakeBoxes[Times[num_,den_^-1], form_]:=
RowBox[{MakeBoxes[num, form],
FractionBox[1,MakeBoxes[den,form]]
}]
The following shows the boxes that are then displayed after
evaluating (x/y).
In[2]:=
ToBoxes[x/y]
Out[2]=
RowBox[{x,FractionBox[1,y]}]
-------------------
The MakeBoxes definition above is stored in FormatValues[MakeBoxes]. The
next line removes the above definition.
In[3]:=
FormatValues[MakeBoxes]=.
--------------------
The next line does almost the same thing as the MakeBoxes definition
above, but here I use a Format definition instead. This gives us TWO
format definitions stored in FormatValues[Times]. Isn't that strange.
In[4]:=
Unprotect[Times];
(** Format definition **)
Format[Times[num_,den_^-1]]:=
DisplayForm[RowBox[{num,FractionBox[1,den]}]]
In[6]:=
ToBoxes[x/y]
Out[6]=
TagBox[RowBox[{x,FractionBox[1,y]}],DisplayForm]
-------------------
Mathematica checks for Format definitions before it uses MakeBoxes
definitions.
Format has no Hold attributes, but MakeBoxes has the HoldAllComplete
attribute. So we often have to use MakeBoxes as a pure function on the
right side of a MakeBoxes definition.
Example: MakeBoxes[#,StandardForm]& @@{-n}
Most or all boxes (SubscriptBox, GridBox, FractionBox, ...) require
Strings or other boxes. So unless you know the argument of a box is a
String you have to call MakeBoxes on that argument.
---------------
QUSETIONS:
Is it possible to make a Format definition that makes the same output
boxes as the MakeBoxes definition above? Not as far as I can tell.
What's up with the two rules we get in FormatValues[Times] after
evaluating the above Format definition?
When and why should we use (Format definitions) vs (MakeBoxes definition).
Are there any other pros and cons between use of MakeBoxes and Format?
-----------
Ted Ersek :)