I never really understood the point of StringForm
returning a StringForm
rather than a String
, which was always either irrelevant to me or an annoyance (addressed by ToString
). Mathematica
10 introduced TemplateApply
, which seems to offer the same functionality (since it can work directly with a string) but returns a String
. So my question: are there situations where StringForm
remains useful, or should it be abandoned?
1 Answer
Note that Message
returns StringForm
:
Message[mname, e1, e2, …]
is printed asStringForm[mess, e1, e2, …]
wheremess
is the value of the messagemname
. Entries of the form`i`
in the stringmess
are replaced by the correspondingei
.
StringForm
is a container that keeps the supplied arguments what makes it easy to extract them for the purposes of debugging. TemplateApply
is intended for convenient generation of standardized output, not for debugging.
StringForm
can produce results that contain nicely formatted forms of arbitrary mathematica expressions. I dont see how you readily get similar results with string templates. $\endgroup$InsertionFunction->ToString@*StandardForm
... which reminds me, I find the default behavior (provided byTextString
) to be quite an odd choice for the insertion function. $\endgroup$*Form
functions in behaviour: it just looks like a string, but the underlying expression has headStringForm
and preserves its arguments. The same is true forInputForm
,MatrixForm
,FullForm
, etc. There were lots of changes in v10, maybe this philosophy is changing, perhaps to make Mathematica more like other systems or be more compatible with other systems.ToString@StringForm[...]
was common before. The one prominent use of StringForm I know of is in message formatting. $\endgroup$