5
$\begingroup$

I've been using Mathematica for a long time, so I have a habit of using some of the older functions even when newer, better ones have been added to replace them.

StringForm is one such function. I have the habit of using it with Print as the Mathematica analog of C's printf when I need debugging output. Today, when I found myself typing Print[StringForm[ ... once again, the thought "why am I not using Row?" occurred to me. Indeed, why not?

Row has been in Mathematica since ver. 6. It looks to me it can do anything that StringForm can do and a lot more. However, WRI has not deprecated StringForm, so I wonder: should Row always be used in place of StringForm? Or is there still some use cases where StringForm is better?

$\endgroup$
6
  • $\begingroup$ I don't know if this constitutes an answer, but I think of Row as a more generic function that allows you to form a row of arbitrary items for visual appearances (e.g. strings and images) and StringForm as a more low level function that's useful in injecting values into a boilerplate message. In writing debugging messages, I find StringForm easier to use because I can read the text in one go, rather than being broken as in Row $\endgroup$
    – rm -rf
    Dec 26, 2012 at 3:40
  • 3
    $\begingroup$ I looked back and found an example where I had used StringForm to format the output of Date[]. But now Date is deprecated and we have DateString... So it's definitely true that StringForm is not nearly as central to formatting as Row is. $\endgroup$
    – Jens
    Dec 26, 2012 at 3:46
  • 1
    $\begingroup$ Row would seem to be preferred if you want to style words or phrases in Italic, Bold, etc., or with varying color or size or font family, for then you can use Style. $\endgroup$
    – murray
    Dec 26, 2012 at 21:48
  • 1
    $\begingroup$ Message uses StringForm, so I think it can't be deprecated. $\endgroup$ Dec 26, 2012 at 22:25
  • 1
    $\begingroup$ StringForm["x = ``", Style[1/3, Red]] yields a String, which looks the same as Row[{"x = ", Style[1/3, Red]}] but different than Row[{"x = ", Style[1/3, Red]}] // ToString. Sometimes you might want a String (to put into string functions, I suppose); sometimes it might suit your style, such as the use rm -rf mentioned. $\endgroup$
    – Michael E2
    Dec 27, 2012 at 20:39

1 Answer 1

4
$\begingroup$

StringForm is being used by Message and therefore difficult to replace. From the documentation of Message:

Message[mname,$e_1$, $e_2$ ,...] is printed as StringForm[mess,$e_1$, $e_2$ ,...] where mess is the value of the message mname. Entries of the form `i` in the string mess are replaced by the corresponding $e_i$.

You can see that here:

Message[NDSolve::ndnco, "XXXXX", "YYYYY" ]

NDSolve::ndnco: The number of constraints (XXXXX) (initial conditions) is not equal to the total differential order of the system plus the number of discrete variables (YYYYY). >>

If we leave away the variable part of the message we get:

Message[NDSolve::ndnco]

StringForm::sfr: Item 1 requested in "The number of constraints (`1`) (initial conditions) is not equal to the total differential order of the system plus the number of discrete variables (`2`). >>" out of range; 0 items available. >>

StringForm::sfr: Item 2 requested in "The number of constraints (`1`) (initial conditions) is not equal to the total differential order of the system plus the number of discrete variables (`2`). >>" out of range; 0 items available. >>

NDSolve::ndnco: The number of constraints (`1`) (initial conditions) is not equal to the total differential order of the system plus the number of discrete variables (`2`). >>

$\endgroup$
1
  • $\begingroup$ Unless... we deprecate Message too and replace it by Speak? $\endgroup$
    – Jens
    Dec 27, 2012 at 19:23

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.