This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Textual Input and Output  /

2.8.17 Advanced Topic: Low-Level Input and Output Rules


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 generalization 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]:=

    Syntax::sntxi: Incomplete expression; more input is needed.

    Syntax::sntxf: "\!\(StandardForm\`\(\(a \_\(m+n\) b\)\)\" cannot be followed by ")".

  • This tells Mathematica to interpret a subscripted as a specific piece of FullForm input.
  • In[7]:= MakeExpression[RowBox[{x_, SubscriptBox[
    "\[CirclePlus]", n_], y_}], StandardForm] :=
    MakeExpression[RowBox[
    {"gplus", "[", x, ",", y, ",", n, "]"}], StandardForm]

  • Now the subscripted is interpreted as a gplus.
  • In[8]:=

    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.


    Operations done on Mathematica input.