2.8.4 The Interpretation of Textual Forms
This takes a string and interprets it as an expression.
Converting from strings or boxes to expressions.
In:= ToExpression["2 + 3 + x/y"]
Here is the box structure corresponding to the textual form of an expression in StandardForm.
In:= ToBoxes[2 + x^2, StandardForm]
ToExpression interprets this box structure and yields the original expression again.
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.
Here is an expression entered in FullForm.
The hierarchy of forms for standard Mathematica input.
In:= Plus[1, Power[x, 2]]
Here is the same expression entered in InputForm.
In:= 1 + x^2
And here is the expression entered in StandardForm.
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 one-dimensional sequence, but in a full two-dimensional 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 text-based 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 two-dimensional forms such as . But InputForm allows only one-dimensional forms. Nevertheless, even though the actual text you give in InputForm must be one-dimensional, it is still possible to make it represent a two-dimensional form. Thus, for example, \!\(x\^2\) represents the two-dimensional form , and is interpreted by Mathematica as Power[x,
Here is ordinary one-dimensional input.
In:= x^2 + 1/y
Here is input that represents a two-dimensional form.
In:= \!\( x\^2 + 1\/y \)
Even though the input is given differently, the expressions obtained on the last two lines are exactly the same.
In:= % == %%
If you copy a two-dimensional form out of Mathematica, it is normally given in \!\( ... \) form. When you paste this one-dimensional form back into a Mathematica notebook, it will automatically "snap" into two-dimensional form. If you simply type a \!\( ... \) form into a notebook, you can get it to snap into two-dimensional form using the
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.
This takes a string and interprets it as TraditionalForm input.
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.
In:= ToExpression["f(6)", TraditionalForm]
In StandardForm the same string would mean a product of terms.
In:= ToExpression["f(6)", StandardForm]
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.