7
$\begingroup$

The picture I load is my target:

enter image description here

This is my code in mathematica

    StringTemplate["``Ratio is ``,``Ratio is ``,``Ratio is ``,"][
 Sequence @@ 
  Flatten@Transpose[{{Style["red", Red, Bold], 
      Style["green", Green, Bold], Style["blue", Blue, Bold]}, {0.1, 
      0.2, 0.3}}]]

But the result is

enter image description here

What's problem in my code?Can Anybody give some advice?

$\endgroup$
6
  • 4
    $\begingroup$ Try the option InsertionFunction -> (ToString[#, TraditionalForm] &) $\endgroup$
    – Greg Hurst
    Dec 1, 2015 at 18:03
  • 2
    $\begingroup$ If the expression need not be a string, you could use Row. $\endgroup$
    – Greg Hurst
    Dec 1, 2015 at 18:05
  • $\begingroup$ See here stackoverflow.com/questions/8956915/… $\endgroup$ Dec 1, 2015 at 18:37
  • 1
    $\begingroup$ @ChipHurst that should be an answer. Perhaps more compact if you use InsertionFunction -> (ToString[Style@@#, TraditionalForm] &), then you can omit the Style from each list item. $\endgroup$
    – george2079
    Dec 1, 2015 at 20:05
  • $\begingroup$ @Chip Hurst Thanks very much. $\endgroup$
    – yode
    Dec 2, 2015 at 2:22

3 Answers 3

5
$\begingroup$
Row@MapThread[
  Row[{Style[#1, ToExpression@#1, Bold], " Ratio is ", #2, #3}] &,
  {{"Red", "Green", "Blue"}, {0.1, 0.2, 0.3}, {", ", ", ", ""}}]

enter image description here

$\endgroup$
3
$\begingroup$
TemplateApply[
 StringTemplate["`` Ratio is ``, `` Ratio is ``, `` Ratio is ``"], 
 Flatten[Transpose[{ToString[#, StandardForm] & /@ {Style["Red", Red, 
       Bold], Style["Green", Green, Bold], 
      Style["Blue", Blue, Bold]}, {0.1, 0.2, 0.3}}]]]

enter image description here

$\endgroup$
1
$\begingroup$

As per How to join Styled strings

str = StringJoin[
  ToString[Style["red ", Red], StandardForm],
  "Ratio is 0.1, ",
  ToString[Style["green ", Green], StandardForm],
  "Ratio is 0.2, ",
  ToString[Style["blue ", Blue], StandardForm],
  "Ratio is 0.3"]

enter image description here

StringQ[str]

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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