Low-Level Input and Output Rules

Low-level functions for converting between expressions and boxes.

(RefLink[MakeBoxes, paclet:ref/MakeBoxes]) generates boxes without evaluating its input:

MakeBoxes[2+2,StandardForm]

(RefLink[MakeExpression, paclet:ref/MakeExpression]) interprets boxes but uses (RefLink[HoldComplete, paclet:ref/HoldComplete]) to prevent the resulting expression from being evaluated:

MakeExpression[%,StandardForm]

Built into the Wolfram Language are a large number of rules for generating output and interpreting input. Particularly in (RefLink[StandardForm, paclet:ref/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 the Wolfram Language 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 the Wolfram Language already has built-in input and output rules.

This outputs using the ⊕ operator:

CirclePlus[u,v,w]

The Wolfram Language understands ⊕ on input:

u ⊕ v ⊕ w//FullForm

In dealing with output, you can make definitions for (RefLink[Format, paclet:ref/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 Wolfram Language input.

If you want to, the Wolfram Language 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 (RefLink[MakeBoxes, paclet:ref/MakeBoxes]) and (RefLink[MakeExpression, paclet:ref/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 (RefLink[StandardForm, paclet:ref/StandardForm]):

gplus/:MakeBoxes[gplus[x_,y_,n_],StandardForm]:=RowBox[{MakeBoxes[x,StandardForm],SubscriptBox["⊕",MakeBoxes[n,StandardForm]],MakeBoxes[y,StandardForm]}]

gplus is now output using a subscripted ⊕:

gplus[a,b,m+n]

The Wolfram Language cannot, however, interpret this as input:

a(Subscript[⊕, m+n])b

This tells the Wolfram Language to interpret a subscripted ⊕ as a specific piece of (RefLink[FullForm, paclet:ref/FullForm]) input:

MakeExpression[RowBox[{x_,SubscriptBox["⊕",n_],y_}],StandardForm]:=MakeExpression[RowBox[{"gplus","[",x,",",y,",",n,"]"}],StandardForm]

Now the subscripted ⊕ is interpreted as a gplus:

a(Subscript[⊕, m+n])b//FullForm

When you give definitions for (RefLink[MakeBoxes, paclet:ref/MakeBoxes]), you can think of this as essentially a lower-level version of giving definitions for (RefLink[Format, paclet:ref/Format]). An important difference is that (RefLink[MakeBoxes, paclet:ref/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 (RefLink[Format, paclet:ref/Format]) is automatically called again on any results obtained by applying it, the same is not true of (RefLink[MakeBoxes, paclet:ref/MakeBoxes]). This means that in giving definitions for (RefLink[MakeBoxes, paclet:ref/MakeBoxes]) you explicitly have to call (RefLink[MakeBoxes, paclet:ref/MakeBoxes]) again on any subexpressions that still need to be formatted.

Operations done on Wolfram Language input.