ToString¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
ToString[expr]
gives the printed form of expr (as OutputForm) as a String; strings inside expr appear without quotes, ToString["s"] is "s".
ToString[expr, form]
uses the specified output form.
Notes
Supported forms: OutputForm (default, linear), InputForm, FullForm, TeXForm.Examples (16)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (5)¶
In[1]:= ToString[x^2 + y^3]
Out[1]= "x^2 + y^3"
In[2]:= ToString[x^2 + y^3, FullForm]
Out[2]= "Plus[Power[x, 2], Power[y, 3]]"
In[3]:= ToString[x^2 + y^3, TeXForm]
Out[3]= "x^{2}+y^{3}"
In[4]:= ToString[{"a", 1}]
Out[4]= "{a, 1}"
In[5]:= ToString["a\"b", InputForm]
Out[5]= "\"a\\\"b\""
Applications (11)¶
The default is OutputForm
A string is its own OutputForm: no added quotes
Strings inside an expression print as their characters
Under InputForm, quotes and backslashes are escaped
The internal tree
TeX for typesetting
Rule-delayed stays distinct from rule
A generated constant becomes a subscript
The decimal digits of 2^100
The InputForm text reads back
An unknown form stays unevaluated
Implementation notes¶
Algorithm. builtin_tostring (src/core.c) renders its argument to a string through the shared printer in src/print.c. It takes 1ā2 arguments, and the second must be a form symbol. As in Mathematica, the default form is OutputForm (since v0.455; before that it was InputForm). A string argument comes back as itself, with no render and no quotes. Anything else goes through expr_to_string_outputform, which prints strings anywhere in the tree as their characters (ToString[{"a", 1}] is "{a, 1}"). The other forms select a printer:
InputFormandStandardFormuseexpr_to_string, which keeps string quotes and escapes"and\, so the result reads back.FullFormusesexpr_to_string_fullform.TeXFormwraps the argument inTeXForm[...]and prints that. This setsC[k]asc_kand infix heads as\to,:\to,\land, ā¦
Data structures. The printer writes into a heap buffer, a tmpfile() round trip on _WIN32. The buffer is copied into a fresh EXPR_STRING. Precedence-aware parenthesisation, Rational/Complex printing and the infix StringExpression/Pattern/Repeated forms ("a"~~x_, x:p, p..) all come from print_standard.
Complexity / limits. Linear in the size of the printed form. Mathilda's OutputForm is one-dimensional (ToString[1/2] is "1/2"), whereas Mathematica's is a 2-D layout. An unknown form such as ToString[x, FooForm] leaves the call unevaluated, so a typo shows at the call site. Protected.
Protected.- An unsupported form leaves the call unevaluated (e.g.
ToString[x, FooForm]returnsToString[x, FooForm]), so a typo is visible at the call site rather than silently downgraded. TeXFormrenders the generated constantC[k](fromDSolve,Reduce,Integrate) as the subscriptedc_k, matching Mathematica:ToString[C[1], TeXForm]is"c_1", andToString[C[10], TeXForm]is"c_{10}"(single-character subscripts bare, longer ones braced). The same holds for the notebook LaTeX renderer.TeXFormsets the infix heads infix:Ruleas\to,RuleDelayedas:\to,Setas=,SetDelayedas:=,And/Oras\land/\lor, the relations as=,\neq,<,>,\leq,\geq,SameQ/UnsameQas\equiv/\not\equiv,Notas\neg, and a chainedInequalityas the chain (1<x<2).RuleDelayedis deliberately distinguished fromRuleā one glyph for both would makea -> banda :> bindistinguishable once typeset, the same reasonSetDelayedis:=. The notebook LaTeX renderer agrees, and additionally brackets a nested operator that binds looser than the one holding it ((a || b) && c, but nota && b || c).
Attributes: Protected.
References¶
See also: String, InputForm, FullForm, TeXForm, StandardForm, DSolve, Reduce, Integrate
- Source:
src/core.c - Specification:
docs/spec/builtins/expression-information.md - Tests:
tests/test_assoc_forms.c - Tests:
tests/test_assoc_ops.c - Tests:
tests/test_blas.c - Tests:
tests/test_compile_assoc.c
Notes & additional examples¶
Notes¶
- Since v0.455
ToString[expr]usesOutputFormby default, as Mathematica does, so strings come out without quotes:ToString["hello"]is"hello". It used to be"\"hello\"". - The supported forms are
OutputForm(the default),InputForm,FullFormandTeXForm.StandardFormis accepted as an alias ofInputForm.InputFormkeeps string quotes and escapes"and\, so the result can be read back withToExpression. - Mathilda's
OutputFormis one-dimensional (ToString[1/2]is"1/2"), whereas Mathematica's is a 2-D layout. - Under
TeXFormthe generated constantsC[k]render asc_k(single characters bare, longer subscripts braced), andRule/RuleDelayedrender as\to/:\to. - An unknown form leaves the call unevaluated, so a typo shows at the call site.
Protected. - See also:
ToExpression,InputForm,FullForm,TeXForm,StringJoin.