2.9.6 String-Oriented Output Formats
| "text" | a string containing arbitrary text |
Text strings. | The quotes are not included in standard Mathematica output form. | |
In[1]:=
"This is a string."
|
Out[1]=
|
|
| In input form, the quotes are included. | |
Out[2]//InputForm=
|
|
You can put any kind of text into a Mathematica string. This includes non-English characters, as well as newlines and other control information. Section 2.8 discusses in more detail how strings work.
StringForm["cccc``cccc", , , ... ]
| | output a string in which successive `` are replaced by successive |
StringForm["cccc`i`cccc", , , ... ]
| | output a string in which each `i` is replaced by the corresponding |
Using format strings. In many situations, you may want to generate output using a string as a "template", but "splicing" in various Mathematica expressions. You can do this using StringForm. | This generates output with each successive `` replaced by an expression. | |
In[3]:=
StringForm["x = ``, y = ``", 3, (1 + u)^2]
|
Out[3]=
|
|
| You can use numbers to pick out expressions in any order. | |
In[4]:=
StringForm["{`1`, `2`, `1`}", a, b]
|
Out[4]=
|
|
The string in StringForm acts somewhat like a "format directive" in the formatted output statements of languages such as C and Fortran. You can determine how the expressions in StringForm will be formatted by wrapping them with standard output format functions. | You can specify how the expressions in StringForm are formatted using standard output format functions. | |
In[5]:=
StringForm["The `` of `` is ``.", TeXForm, a/b, TeXForm[a/b]]
|
Out[5]=
|
|
You should realize that StringForm is only an output format. It does not evaluate in any way. You can use the function ToString to create an ordinary string from a StringForm object. | StringForm generates formatted output in standard Mathematica output form. | |
In[6]:=
StringForm["Q: `` -> ``", a, b]
|
Out[6]=
|
|
| In input form, you can see the actual StringForm object. | |
Out[7]//InputForm=
|
|
| This creates an ordinary string from the StringForm object. | |
In[8]:=
InputForm[ToString[%]]
|
Out[8]//InputForm=
|
|
StringForm allows you to specify a "template string", then fill in various expressions. Sometimes all you want to do is to concatenate together the output forms for a sequence of expressions. You can do this using SequenceForm.
SequenceForm[ , , ... ] | give the output forms of the concatenated together |
Output of sequences of expressions. | SequenceForm prints as a sequence of expressions concatenated together. | |
In[9]:=
SequenceForm["[x = ", 56, "]"]
|
Out[9]=
|
|
ColumnForm[{ , , ... }] | a left-aligned column of objects | | ColumnForm[list, h, v] | a column with horizontal alignment h (Left, Center or Right), and vertical alignment v (Below, Center or Above) |
Output of columns of expressions. | This arranges the two expressions in a column. | |
In[10]:=
ColumnForm[{a + b, x^2}]
|
Out[10]=
|
|
| HoldForm[expr] | give the output form of expr, with expr maintained unevaluated |
Output of unevaluated expressions. Using text strings and functions like StringForm, you can generate pieces of output that do not necessarily correspond to valid Mathematica expressions. Sometimes, however, you want to generate output that corresponds to a valid Mathematica expression, but only so long as the expression is not evaluated. The function HoldForm maintains its argument unevaluated, but allows it to be formatted in the standard Mathematica output form. | HoldForm maintains 1 + 1 unevaluated. | |
Out[11]=
|
|
| The HoldForm prevents the actual assignment from being done. | |
Out[12]=
|
|
| If it was not for the HoldForm, the power would be evaluated. | |
Out[13]=
|
|
|