## 2.9.17 Advanced Topic: Low-Level Input and Output Rules

 MakeBoxes[expr, form] construct boxes to represent expr in the specified form MakeExpression[boxes, form] construct an expression corresponding to boxes

Low-level functions for converting between expressions and boxes.
MakeBoxes generates boxes without evaluating its input.
 In[1]:=  MakeBoxes[2 + 2, StandardForm]
 Out[1]=
MakeExpression interprets boxes but uses HoldComplete to prevent the resulting expression from being evaluated.
 In[2]:=  MakeExpression[%, StandardForm]
 Out[2]=

Built into Mathematica are a large number of rules for generating output and interpreting input. Particularly in StandardForm, these rules are carefully set up to be consistent, and to allow input and output to be used interchangeably.

It is fairly rare that you will need to modify these rules. The main reason is that Mathematica already has built-in rules for the input and output of many operators to which it does not itself assign specific meanings.

Thus, if you want to add, for example, a generalized form of addition, you can usually just use an operator like for which Mathematica already has built-in input and output rules.

This outputs using the operator.
 In[3]:=  CirclePlus[u, v, w]
 Out[3]=
Mathematica understands on input.
 In[4]:=  u v w // FullForm
 Out[4]//FullForm=

In dealing with output, you can make definitions for Format[expr] to change the way that a particular expression will be formatted. You should realize, however, that as soon as you do this, there is no guarantee that the output form of your expression will be interpreted correctly if it is given as Mathematica input.

If you want to, Mathematica allows you to redefine the basic rules that it uses for the input and output of all expressions. You can do this by making definitions for MakeBoxes and MakeExpression. You should realize, however, that unless you make such definitions with great care, you are likely to end up with inconsistent results.

This defines how gplus objects should be output in StandardForm.
 In[5]:=  gplus /: MakeBoxes[gplus[x_, y_, n_], StandardForm] := RowBox[ {MakeBoxes[x, StandardForm], SubscriptBox["\[CirclePlus]", MakeBoxes[n, StandardForm]], MakeBoxes[y, StandardForm]} ]
gplus is now output using a subscripted .
 In[6]:=  gplus[a, b, m+n]
 Out[6]=
Mathematica cannot however interpret this as input.
 In[7]:=  aXMLElement[img, {src -> http://documents.wolfram.com/MathematicaCharacters/CirclePlus.gif, width -> 10, height -> 19, align -> absmiddle, alt -> CirclePlus}, {}] . XMLElement[sub, {}, {m+n}]b

This tells Mathematica to interpret a subscripted as a specific piece of FullForm input.
 In[8]:=  MakeExpression[RowBox[{x_, SubscriptBox[ "\[CirclePlus]", n_], y_}], StandardForm] := MakeExpression[RowBox[ {"gplus", "[", x, ",", y, ",", n, "]"}], StandardForm]
Now the subscripted is interpreted as a gplus.
 In[9]:=  aXMLElement[img, {src -> http://documents.wolfram.com/MathematicaCharacters/CirclePlus.gif, width -> 10, height -> 19, align -> absmiddle, alt -> CirclePlus}, {}] . XMLElement[sub, {}, {m+n}]b//FullForm
 Out[8]//FullForm=

When you give definitions for MakeBoxes, you can think of this as essentially a lower-level version of giving definitions for Format. An important difference is that MakeBoxes does not evaluate its argument, so you can define rules for formatting expressions without being concerned about how these expressions would evaluate.

In addition, while Format is automatically called again on any results obtained by applying it, the same is not true of MakeBoxes. This means that in giving definitions for MakeBoxes you explicitly have to call MakeBoxes again on any subexpressions that still need to be formatted.

 • Break input into tokens. • Strip spacing characters. • Construct boxes using built-in operator precedences. • Strip StyleBox and other boxes not intended for interpretation. • Apply rules defined for MakeExpression.

Operations done on Mathematica input.

THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.