2.9.4 The Interpretation of Textual Forms
Converting from strings or boxes to expressions.
This takes a string and interprets it as an expression.
In[1]:= ToExpression["2 + 3 + x/y"]
Out[1]=
Here is the box structure corresponding to the textual form of an expression in StandardForm.
In[2]:= ToBoxes[2 + x^2, StandardForm]
Out[2]=
ToExpression interprets this box structure and yields the original expression again.
In[3]:= ToExpression[%]
Out[3]=
In any Mathematica session, Mathematica is always effectively using ToExpression to interpret the textual form of your input as an actual expression to evaluate.
If you use the notebook front end for Mathematica, then the interpretation only takes place when the contents of a cell are sent to the kernel, say for evaluation. This means that within a notebook there is no need for the textual forms you set up to correspond to meaningful Mathematica expressions; this is only necessary if you want to send these forms to the kernel.
The hierarchy of forms for standard Mathematica input.
Here is an expression entered in FullForm.
In[4]:= Plus[1, Power[x, 2]]
Out[4]=
Here is the same expression entered in InputForm.
In[5]:= 1 + x^2
Out[5]=
And here is the expression entered in StandardForm.
In[6]:=
Out[6]=
Built into Mathematica is a collection of standard rules for use by ToExpression in converting textual forms to expressions.
These rules define the grammar of Mathematica. They state, for example, that x + y should be interpreted as Plus[x, y], and that should be interpreted as Power[x, y]. If the input you give is in FullForm, then the rules for interpretation are very straightforward: every expression consists just of a head followed by a sequence of elements enclosed in brackets. The rules for InputForm are slightly more sophisticated: they allow operators such as +, =, and >, and understand the meaning of expressions where these operators appear between operands. StandardForm involves still more sophisticated rules, which allow operators and operands to be arranged not just in a onedimensional sequence, but in a full twodimensional structure.
Mathematica is set up so that FullForm, InputForm and StandardForm form a strict hierarchy: anything you can enter in FullForm will also work in InputForm, and anything you can enter in InputForm will also work in StandardForm.
If you use a notebook front end for Mathematica, then you will typically want to use all the features of StandardForm. If you use a textbased interface, however, then you will typically be able to use only features of InputForm.
Two versions of InputForm.
When you use StandardForm in a Mathematica notebook, you can enter directly twodimensional forms such as . But InputForm allows only onedimensional forms. Nevertheless, even though the actual text you give in InputForm must be onedimensional, it is still possible to make it represent a twodimensional form. Thus, for example, \!\(x\^2\) represents the twodimensional form , and is interpreted by Mathematica as Power[x, 2].
Here is ordinary onedimensional input.
In[7]:= x^2 + 1/y
Out[7]=
Here is input that represents a twodimensional form.
In[8]:= \!\( x\^2 + 1\/y \)
Out[8]=
Even though the input is given differently, the expressions obtained on the last two lines are exactly the same.
In[9]:= % == %%
Out[9]=
If you copy a twodimensional form out of Mathematica, it is normally given in \!\( ... \) form. When you paste this onedimensional form back into a Mathematica notebook, it will automatically "snap" into twodimensional form. If you simply type a \!\( ... \) form into a notebook, you can get it to snap into twodimensional form using the Make 2D menu item.
Importing from other textual forms.
StandardForm and its subsets FullForm and InputForm provide precise ways to represent any Mathematica expression in textual form. And given such a textual form, it is always possible to convert it unambiguously to the expression it represents.
TraditionalForm is an example of a textual form intended primarily for output. It is possible to take any Mathematica expression and display it in TraditionalForm. But TraditionalForm does not have the precision of StandardForm, and as a result there is in general no unambiguous way to go back from a TraditionalForm representation and get the expression it represents.
Nevertheless, ToExpression[input, TraditionalForm] takes text in TraditionalForm and attempts to interpret it as an expression.
This takes a string and interprets it as TraditionalForm input.
In[10]:= ToExpression["f(6)", TraditionalForm]
Out[10]=
In StandardForm the same string would mean a product of terms.
In[11]:= ToExpression["f(6)", StandardForm]
Out[11]=
When TraditionalForm output is generated as the result of a computation, the actual collection of boxes that represent the output typically contains special InterpretationBox and TagBox objects which specify how an expression can be reconstructed from the TraditionalForm output.
The same is true of TraditionalForm that is obtained by explicit conversion from StandardForm. But if you edit TraditionalForm extensively, or enter it from scratch, then Mathematica will have to try to interpret it without the benefit of any additional embedded information.
