String Representation of Boxes

The Wolfram Language 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 creates a (RefLink[SuperscriptBox, paclet:ref/SuperscriptBox]):

\(x\^2\)

(RefLink[ToExpression, paclet:ref/ToExpression]) interprets the (RefLink[SuperscriptBox, paclet:ref/SuperscriptBox]) as a power:

ToExpression[%]//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 (RefLink[SuperscriptBox, paclet:ref/SuperscriptBox]):

\(x\^2\)

This corresponds to the power that the (RefLink[SuperscriptBox, paclet:ref/SuperscriptBox]) represents:

\!\(x\^2\)

The expression generated here is a power:

FullForm[\!\(x\^2\)]

Distinguishing raw boxes from the expressions they represent.

If you copy the contents of a (RefLink[StandardForm, paclet:ref/StandardForm]) cell into another program, such as a text editor, the Wolfram System will generate a ∖!∖(…∖) form where necessary. This is done so that if you subsequently paste the form back into the Wolfram System, the original contents of the (RefLink[StandardForm, paclet:ref/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 Wolfram System notebooks are automatically displayed in two-dimensional form.

Embedding two-dimensional box structures in strings.

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

The Wolfram Language treats this as an ordinary character string:

"\( x \^ 2 \)"

The !∖ tells the Wolfram Language that this string contains boxes:

"\!\( x \^ 2 \)"

You can mix boxes with ordinary text:

"box 1: \!\(x\^2\); box 2: \!\(y\^3\)"

String representations of boxes.

The Wolfram Language 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:

\(x\/(y+z)\)//DisplayForm

Without the parentheses, the grouping would be different:

\(x\/y+z\)//DisplayForm

∖( and ∖) specify grouping, but are not displayed as explicit parentheses:

\(x\/\(y+z\)\)//DisplayForm

The inner ∖( and ∖) lead to the construction of a (RefLink[RowBox, paclet:ref/RowBox]):

\(x\/\(y+z\)\)

When you type aa+bb as input to the Wolfram System, the first thing that happens is that aa, +, and bb 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 (RefLink[RowBox, paclet:ref/RowBox]) has aa, +, and bb broken into separate strings:

\(aa+bb\)//FullForm

Spaces around the + are by default discarded:

\(aa + bb\)//FullForm

Here two nested (RefLink[RowBox, paclet:ref/RowBox]) objects are formed:

\(aa+bb/cc\)//FullForm

The same box structure is formed even when the string given does not correspond to a complete Wolfram Language expression:

\(aa+bb/\)//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 Wolfram System input, prefaced by ∖*.

This constructs a (RefLink[GridBox, paclet:ref/GridBox]):

\(\*GridBox[{{"a","b"},{"c","d"}}]\)//DisplayForm

This constructs a (RefLink[StyleBox, paclet:ref/StyleBox]):

\(\*StyleBox["text",FontWeight->"Bold"]\)//DisplayForm

∖* in effect acts like an escape: it allows you to enter ordinary Wolfram Language 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 ∖(…∖):

\(x+\*GridBox[{{"a","b"},{\(c\^2\),\(d\/\*GridBox[{{"x","y"},{"x","y"}}]\)}}]\)//DisplayForm

Controlling the way input is interpreted.

In a (RefLink[StandardForm, paclet:ref/StandardForm]) cell, this will be interpreted in (RefLink[StandardForm, paclet:ref/StandardForm]), yielding a product:

\!\(c(1+x)\)

The backslash backquote sequence tells the Wolfram Language to interpret this in (RefLink[TraditionalForm, paclet:ref/TraditionalForm]):

\!\(TraditionalForm\`c(1+x)\)

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.