4
$\begingroup$

I was pleasantly surprised to find out that StringTemplate appears to work with styled (formatted) strings, with the style of the slots applied to the inserted expressions:

enter image description here

(The string was formatted using the Format menu.)

Is this usage intended or documented?

$\endgroup$

1 Answer 1

3
$\begingroup$

I think this follows from the representation of styles (and other boxes) in strings.

For example, this:

Mathematica graphics

is really represented like this in a string:

template="a \!\(\*\nStyleBox[\"``\",\nFontColor->RGBColor[1,0,0]]\) and a ``" 

Then either with StringForm or StringTemplate we get

enter image description here

In[10]:= StringTemplate[template]["foo", "bar"]

Out[10]= "a \!\(\*StyleBox[\"foo\",\nFontColor->RGBColor[1, 0, 0]]\) and a bar"

It's just plain substitution.


EDIT: This view also gives ideas on how to break substitution:

enter image description here

It appears that there is no special support for styling, it's all a side effect of how style boxes are encoded in strings.

$\endgroup$
4
  • $\begingroup$ Do you get the same error that I reported? $\endgroup$
    – Mr.Wizard
    Commented Feb 11, 2015 at 21:37
  • 1
    $\begingroup$ @Mr.Wizard If I try to convert it by typing into it then yes, I get the error. Furthermore, the string is corrupted. If I convert it by changing the cell style then everything is fine. $\endgroup$
    – Szabolcs
    Commented Feb 11, 2015 at 21:41
  • 1
    $\begingroup$ @Mr.Wizard Further testing shows that the error is not related to StringTemplate. Just make a styled string, evaluate it, and try typing in the output cell. The cell contents will be corrupted and the error appears. $\endgroup$
    – Szabolcs
    Commented Feb 11, 2015 at 21:42
  • $\begingroup$ Thank you. I should have explored that further before posting. $\endgroup$
    – Mr.Wizard
    Commented Feb 11, 2015 at 21:43

Your Answer

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

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