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.12 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.
  • In[1]:= ToString[SuperscriptBox["x", "2"], InputForm]


  • This creates the SuperscriptBox.
  • In[2]:= \( x \^ 2 \)


  • ToExpression interprets the SuperscriptBox as a power.
  • In[3]:= 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 SuperscriptBox.
  • In[4]:= \( x \^ 2 \)


  • This corresponds to the power that the SuperscriptBox represents.
  • In[5]:= \!\( x \^ 2 \)


  • The expression generated here is a power.
  • In[6]:= FullForm[ \!\( x \^ 2 \) ]


    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 automatically generate a \!\( ... \) form. 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. \!\( ... \) forms entered directly from the keyboard can be displayed in two-dimensional form using the Make 2D item in the Edit menu.

    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.
  • In[7]:= "\( x \^ 2 \)"


  • The \! tells Mathematica that this string contains boxes.
  • In[8]:= "\!\( x \^ 2 \)"


  • You can mix boxes with ordinary text.
  • In[9]:= "box 1: \!\(x\^2\); box 2: \!\(y\^3\)"


    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.
  • In[10]:= \( x \/ (y + z) \) // DisplayForm


  • Without the parentheses, the grouping would be different.
  • In[11]:= \( x \/ y + z \) // DisplayForm


  • \( and \) specify grouping, but are not displayed as explicit parentheses.
  • In[12]:= \( x \/ \(y + z\) \) // DisplayForm


  • The inner \( and \) lead to the construction of a RowBox.
  • In[13]:= \( x \/ \(y + z\) \)


    When you type aa+bb as input to Mathematica, 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 RowBox has aa, + and bb broken into separate strings.
  • In[14]:= \( aa+bb \) // FullForm


  • The spaces around the + are by default discarded.
  • In[15]:= \( aa + bb \) // FullForm


  • Backslash-space inserts a literal space.
  • In[16]:= \( aa + bb \) // FullForm


  • Here two nested RowBox objects are formed.
  • In[17]:= \( aa+bb/cc \) // FullForm


  • The same box structure is formed even when the string given does not correspond to a complete Mathematica expression.
  • In[18]:= \( 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 Mathematica input, prefaced by \*.

  • This constructs a GridBox.
  • In[19]:= \( \*GridBox[{{"a", "b"}, {"c", "d"}}] \) // DisplayForm


  • This constructs a StyleBox.
  • In[20]:= \( \*StyleBox["text", FontWeight->Bold] \) // 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 \( ... \).
  • In[21]:= \( x + \*GridBox[{{"a", "b"}, {\(c \^ 2\), \(d \/
    \*GridBox[{{"x","y"},{"x","y"}}] \)}}] \) // DisplayForm


    In the notebook front end, you can typically use * or 8 to get a dialog box in which you can enter raw boxesjust as you do after \*.

    Controlling the way input is interpreted.

  • In a StandardForm cell, this will be interpreted in StandardForm, yielding a product.
  • In[22]:= \!\( c(1+x) \)


  • The backslash backquote sequence tells Mathematica to interpret this in TraditionalForm.
  • In[23]:= \!\(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.