Representing Textual Forms by Boxes

All textual and graphical forms in the Wolfram System 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:

ToBoxes[a+b]

(RefLink[DisplayForm, paclet:ref/DisplayForm]) shows how these boxes would be displayed:

DisplayForm[%]

Showing the displayed form of boxes.

This displays three strings in a row:

RowBox[{"a","+","b"}]//DisplayForm

This displays one string as a subscript of another:

SubscriptBox["a","i"]//DisplayForm

This puts two subscript boxes in a row:

RowBox[{SubscriptBox["a","1"],SubscriptBox["b","2"]}]//DisplayForm

Some basic box types.

This nests a fraction inside a radical:

RadicalBox[FractionBox[x,y],n]//DisplayForm

This puts a superscript on a subscripted object:

SuperscriptBox[SubscriptBox[a,b],c]//DisplayForm

This puts both a subscript and a superscript on the same object:

SubsuperscriptBox[a,b,c]//DisplayForm

Inserting frames and grid lines.

This shows a fraction with a frame drawn around it:

FrameBox[FractionBox["x","y"]]//DisplayForm

This puts lines between rows and columns of an array:

GridBox[Table[i+j,{i,3},{j,3}],RowLines->True,ColumnLines->True]//DisplayForm

And this also puts a frame around the outside:

FrameBox[%]//DisplayForm

Modifying the appearance of boxes.

(RefLink[StyleBox, paclet:ref/StyleBox]) takes the same options as (RefLink[Style, paclet:ref/Style]). The difference is that (RefLink[Style, paclet:ref/Style]) is a high-level function that applies to an expression to determine how it will be displayed, while (RefLink[StyleBox, paclet:ref/StyleBox]) is the corresponding low-level function in the underlying box structure.

This shows the string "name" in italics:

StyleBox["name",FontSlant->"Italic"]//DisplayForm

This shows "name" in the style used for section headings in your current notebook:

StyleBox["name","Section"]//DisplayForm

This uses section heading style, but with characters shown in gray:

StyleBox["name","Section",FontColor->GrayLevel[0.5]]//DisplayForm

If you use a notebook front end for the Wolfram System, 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 (RefLink[StyleBox, paclet:ref/StyleBox]) objects.

Controlling the interpretation of boxes.

This prints as x with a superscript:

SuperscriptBox["x","2"]//DisplayForm

It is normally interpreted as a power:

ToExpression[%]//InputForm

This again prints as x with a superscript:

InterpretationBox[SuperscriptBox["x","2"],vec[x,2]]//DisplayForm

But now it is interpreted as vec[x,2], following the specification given in the (RefLink[InterpretationBox, paclet:ref/InterpretationBox]):

ToExpression[%]//InputForm

If you edit the boxes given in an (RefLink[InterpretationBox, paclet:ref/InterpretationBox]), then there is no guarantee that the interpretation specified by the interpretation box will still be correct. As a result, the Wolfram Language provides various options that allow you to control the selection and editing of (RefLink[InterpretationBox, paclet:ref/InterpretationBox]) objects.

Options for (RefLink[InterpretationBox, paclet:ref/InterpretationBox]) and related boxes.

(RefLink[TagBox, paclet:ref/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 (RefLink[TagBox, paclet:ref/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 (RefLink[TagBox, paclet:ref/TagBox]) will still be appropriate. As a result, (RefLink[Editable, paclet:ref/Editable])->(RefLink[True, paclet:ref/True]) is effectively the default setting for a (RefLink[TagBox, paclet:ref/TagBox]).

The rules that the Wolfram System uses for interpreting boxes are in general set up to ignore details of formatting, such as those defined by (RefLink[StyleBox, paclet:ref/StyleBox]) objects. Thus, unless (RefLink[StripWrapperBoxes, paclet:ref/StripWrapperBoxes])->(RefLink[False, paclet:ref/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:

ToExpression[StyleBox[x,FontColor->RGBColor[1,0,0]]]==x