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

2.8.10 Representing Textual Forms by Boxes

All textual forms in Mathematica are ultimately represented in terms of nested collections of boxes. Typically the elements of these boxes correspond to objects that are to be placed at definite relative positions in two dimensions.

  • Here are the boxes corresponding to the expression a+b.
  • In[1]:= ToBoxes[a + b]


  • DisplayForm shows how these boxes would be displayed.
  • In[2]:= DisplayForm[%]


    Showing the displayed form of boxes.

  • This displays three strings in a row.
  • In[3]:= RowBox[{"a", "+", "b"}] // DisplayForm


  • This displays one string as a subscript of another.
  • In[4]:= SubscriptBox["a", "i"] // DisplayForm


  • This puts two subscript boxes in a row.
  • In[5]:= RowBox[{SubscriptBox["a", "1"], SubscriptBox["b", "2"]}] //


    Some basic box types.

  • This nests a fraction inside a radical.
  • In[6]:= RadicalBox[FractionBox[x, y], n] // DisplayForm


  • This puts a superscript on a subscripted object.
  • In[7]:= SuperscriptBox[SubscriptBox[a, b], c] // DisplayForm


  • This puts both a subscript and a superscript on the same object.
  • In[8]:= SubsuperscriptBox[a, b, c] // DisplayForm


    Inserting frames and grid lines.

  • This shows a fraction with a frame drawn around it.
  • In[9]:= FrameBox[FractionBox["x", "y"]] // DisplayForm


  • This puts lines between rows and columns of an array.
  • In[10]:= GridBox[Table[i+j, {i, 3}, {j, 3}], RowLines->True,
    ColumnLines->True] // DisplayForm


  • And this also puts a frame around the outside.
  • In[11]:= FrameBox[%] // DisplayForm


    Modifying the appearance of boxes.

    StyleBox takes the same options as StyleForm. The difference is that StyleForm acts as a "wrapper" for any expression, while StyleBox represents underlying box structure.

  • This shows the string "name" in italics.
  • In[12]:= StyleBox["name", FontSlant->"Italic"] // DisplayForm


  • This shows "name" in the style used for section headings in your current notebook.
  • In[13]:= StyleBox["name", "Section"] // DisplayForm


  • This uses section heading style, but with characters shown in gray.
  • In[14]:= StyleBox["name", "Section", FontColor->GrayLevel[0.5]] // DisplayForm


    If you use a notebook front end for Mathematica, then you will be able to change the style and appearance of what you see on the screen directly by using menu items. Internally, however, these changes will still be recorded by the insertion of appropriate StyleBox objects.

    Controlling the interpretation of boxes.

  • This prints as

    with a superscript.
  • In[15]:= SuperscriptBox["x", "2"] // DisplayForm


  • It is normally interpreted as a power.
  • In[16]:= ToExpression[%] // InputForm


  • This again prints as

    with a superscript.
  • In[17]:= InterpretationBox[SuperscriptBox["x", "2"],
    vec[x, 2]] // DisplayForm


  • But now it is interpreted as vec[x,2], following the specification given in the InterpretationBox.
  • In[18]:= ToExpression[%] // InputForm


    If you edit the boxes given in an InterpretationBox, then there is no guarantee that the interpretation specified by the interpretation box will still be correct. As a result, Mathematica provides various options that allow you to control the selection and editing of InterpretationBox objects.

    Options for InterpretationBox and related boxes.

    TagBox objects are used to store information that will not be displayed but which can nevertheless be used by the rules that interpret boxes. Typically the tag in TagBox[boxes,tag] is a symbol which gives the head of the expression corresponding to boxes. If you edit only the arguments of this expression then there is a good chance that the interpretation specified by the TagBox will still be appropriate. As a result, Editable->True is the default setting for a TagBox.
    The rules that Mathematica uses for interpreting boxes are in general set up to ignore details of formatting, such as those defined by StyleBox objects. Thus, unless StripWrapperBoxes->False, a red x, for example, will normally not be distinguished from an ordinary black x.

  • A red x is usually treated as identical to an ordinary one.
  • In[19]:= ToExpression[
    StyleBox[x, FontColor->RGBColor[1,0,0]]] == x


    Setting up active elements.

    In a Mathematica notebook it is possible to set up elements which perform an action whenever you click on them. These elements are represented internally by ButtonBox objects. When you create an expression containing a ButtonBox, you will be able to edit the contents of the ButtonBox directly so long as the Active option is False for the cell containing the expression. As soon as you set Active->True, the ButtonBox will perform its action whenever you click on it.
    Section 2.10.6 discusses how to set up actions for ButtonBox objects.