MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What is the difference Between MakeBoxes and ToBoxes

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106754] Re: [mg106684] What is the difference Between MakeBoxes and ToBoxes
  • From: "David Park" <djmpark at comcast.net>
  • Date: Fri, 22 Jan 2010 05:41:55 -0500 (EST)
  • References: <713167.1264068057720.JavaMail.root@n11>

George,

MakeBoxes is very useful if you want to have specially formatted output
display but use standard Mathematica input. Standard input is often more
convenient than a special input (using the Notation package or
MakeExpression) because you don't have to bring up a special box structure
or tab around when entering information.

Here is a simple example taken from one of my Presentations package essays.
MatrixExp has no special formatting.

MatrixExp[\[Theta] J]  
% /. J -> ( {
     {0, -1},
     {1, 0}
    } ) // MatrixForm  

Perhaps we would want MatrixExp to format as an exponential when it doesn't
evaluate. We can do it with the following MakeBoxes definition:

MakeBoxes[MatrixExp[x_], form : StandardForm | TraditionalForm] := 
 InterpretationBox[#1, #2] & @@
      {SuperscriptBox["\[ExponentialE]", MakeBoxes[x, form]], 
   MatrixExp[x]}  

Now evaluate the same statements above. (It is the first statement that is
formatted.) Notice the structure of the rhs of the MakeBoxes definition.
InterpretationBox has the Attributes HoldAll, but we can circumvent this by
making it a pure function that we apply to a List. The first item in the
list is the formatted structure and the second item is the internal
representations. The list could be replaced by a Module that did some
calculations to determine the display structure. Notice also that the rhs
uses MakeBoxes on x because we don't know if it might have its own
formatting definitions.

InterpretationBox also has an option SyntaxForm that can be used to
determine the precedence grouping of the formatted expression, and hence
whether Mathematica adds parentheses in various cases. However, this is not
fully integrated into Mathematica, and in my experience causes FrontEnd
crashes. That is a flaw in Mathematica that I don't like, but usually you
can get by without SyntaxForm.

Instead of writing the displayed form entirely with low level box
structures, you might be able to write all or part of it with high level
Mathematica expressions and then use ToBoxes to generate the box structures.
It depends on whether the format you want fits into regular Mathematica
formatting, or is just too special. Here we could write the MakeBoxes
definition above as:

MakeBoxes[MatrixExp[x_], form : StandardForm | TraditionalForm] := 
 InterpretationBox[#1, #2] & @@
      {ToBoxes[Superscript["\[ExponentialE]", x], form], MatrixExp[x]}  

We also have the high level Interpretation statement and I've used this
successfully in some cases, but it is not as versatile as InterpretationBox.
There is also the Format statement.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: George [mailto:gtatishvili at gmail.com] 

May you please advise me what is the difference between "ToBoxes" and
"MakeBoxes"?  I read in Mathematica manual and unerstood that the only
differerence is that "MakeBoxes" generates boxes without evaluation of
input...So is that all the difference?

I saw also some examples where on lhs is "MakeBoxes" and on rhs is
"ToBoxes" when I just changed "MakeBoxes" into "ToBoxes" (and vice
versa) the Mathematica gave me some errors.... So could anybody
explain me with a simple practical example with explanation?

Thank you very much
George




  • Prev by Date: Re: Function and object naming conventions
  • Next by Date: Re: Text Output
  • Previous by thread: Re: What is the difference Between MakeBoxes and ToBoxes
  • Next by thread: Function and object naming conventions