Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  rm -rf Dec 26 '12 at 3:40
3  
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. –  Jens Dec 26 '12 at 3:46
1  
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. –  murray Dec 26 '12 at 21:48
1  
Message uses StringForm, so I think it can't be deprecated. –  Sjoerd C. de Vries Dec 26 '12 at 22:25
1  
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. –  Michael E2 Dec 27 '12 at 20:39
show 1 more comment

1 Answer

up vote 4 down vote accepted

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`). >>

share|improve this answer
    
Unless... we deprecate Message too and replace it by Speak? –  Jens Dec 27 '12 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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