5
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ 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$
    – george2079
    Aug 30, 2016 at 19:37
  • 1
    $\begingroup$ @george2079 InsertionFunction->ToString@*StandardForm ... which reminds me, I find the default behavior (provided by TextString) to be quite an odd choice for the insertion function. $\endgroup$
    – Alan
    Aug 31, 2016 at 10:54
  • 3
    $\begingroup$ I suspect that this will be down to opinion and personal philosophy. It follows other *Form functions in behaviour: it just looks like a string, but the underlying expression has head StringForm and preserves its arguments. The same is true for InputForm, 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$
    – Szabolcs
    Sep 13, 2016 at 12:32

1 Answer 1

4
$\begingroup$

Note that Message returns StringForm:

Message[mname, e1, e2, …] is printed as StringForm[mess, e1, e2, …] where mess is the value of the message mname. Entries of the form `i` in the string mess are replaced by the corresponding ei.

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.