MATHEMATICA TUTORIAL

# String Representation of Boxes

Mathematica provides a compact way of representing boxes in terms of strings. This is particularly convenient when you want to import or export specifications of boxes as ordinary text.

This generates an InputForm string that represents the SuperscriptBox.
 Out[1]=
This creates the SuperscriptBox.
 Out[2]=
ToExpression interprets the SuperscriptBox as a power.
 Out[3]//FullForm=

It is important to distinguish between forms that represent just raw boxes, and forms that represent the meaning of the boxes.

This corresponds to a raw SuperscriptBox.
 Out[4]=
This corresponds to the power that the SuperscriptBox represents.
 Out[5]=
The expression generated here is a power.
 Out[6]//FullForm=
 \(input\) raw boxes \!\(input\) the meaning of the boxes

Distinguishing raw boxes from the expressions they represent.

If you copy the contents of a StandardForm cell into another program, such as a text editor, Mathematica will generate a form where necessary. This is done so that if you subsequently paste the form back into Mathematica, the original contents of the StandardForm cell will automatically be re-created. Without the , only the raw boxes corresponding to these contents would be obtained.

With default settings for options, forms pasted into Mathematica notebooks are automatically displayed in two-dimensional form.

 "\(input\)" a raw character string "\!\(input\)" a string containing boxes

Embedding two-dimensional box structures in strings.

Mathematica will usually treat a form that appears within a string just like any other sequence of characters. But by inserting a you can tell Mathematica instead to treat this form like the boxes it represents. In this way you can therefore embed box structures within ordinary character strings.

Mathematica treats this as an ordinary character string.
 Out[7]=
The tells Mathematica that this string contains boxes.
 Out[8]=
You can mix boxes with ordinary text.
 Out[9]=
 \(box1,box2,...\) RowBox[box1,box2,...] box1\^box2 SuperscriptBox[box1,box2] box1\_box2 SubscriptBox[box1,box2] box1\_box2\%box3 SubsuperscriptBox[box1,box2,box3] box1\&box2 OverscriptBox[box1,box2] box1\+box2 UnderscriptBox[box1,box2] box1\+box2\%box3 UnderoverscriptBox[box1,box2,box3] box1\/box2 FractionBox[box1,box2] \@box SqrtBox[box] \@box1\%box2 RadicalBox[box1,box2] form\` box FormBox[box,form] \*input construct boxes from input

Input forms for boxes.

Mathematica requires that any input forms you give for boxes be enclosed within and . But within these outermost and you can use additional and to specify grouping.

Here ordinary parentheses are used to indicate grouping.
 Out[10]//DisplayForm=
Without the parentheses, the grouping would be different.
 Out[11]//DisplayForm=
and specify grouping, but are not displayed as explicit parentheses.
 Out[12]//DisplayForm=
The inner and lead to the construction of a RowBox.
 Out[13]=

When you type as input to Mathematica, the first thing that happens is that , , and are recognized as being separate "tokens". The same separation into tokens is done when boxes are constructed from input enclosed in . However, inside the boxes each token is given as a string, rather than in its raw form.

The RowBox has , , and broken into separate strings.
 Out[14]//FullForm=
Spaces around the are by default discarded.
 Out[15]//FullForm=
Here two nested RowBox objects are formed.
 Out[16]//FullForm=
The same box structure is formed even when the string given does not correspond to a complete Mathematica expression.
 Out[17]//FullForm=

Within sequences, you can set up certain kinds of boxes by using backslash notations such as and . But for other kinds of boxes, you need to give ordinary Mathematica input, prefaced by .

This constructs a GridBox.
 Out[18]//DisplayForm=
This constructs a StyleBox.
 Out[19]//DisplayForm=

in effect acts like an escape: it allows you to enter ordinary Mathematica syntax even within a sequence. Note that the input you give after a can itself in turn contain sequences.

You can alternate nested and . Explicit quotes are needed outside of .
 Out[20]//DisplayForm=
 \!\(input\) interpret input in the current form \!\(form\`input\) interpret input using the specified form

Controlling the way input is interpreted.

In a StandardForm cell, this will be interpreted in StandardForm, yielding a product.
 Out[21]=
The backslash backquote sequence tells Mathematica to interpret this in TraditionalForm.
 Out[22]=

When you copy the contents of a cell from a notebook into a program such as a text editor, no explicit backslash backquote sequence is usually included. But if you expect to paste what you get back into a cell of a different type from the one it came from, then you will typically need to include a backslash backquote sequence in order to ensure that everything is interpreted correctly.

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »