2
$\begingroup$

I'm interested in displaying a string composed from sub-strings of different colors. Mathematica allows one to create a list comprising sub-strings of different colors, for example

{Style["Blue", Blue], Style["Red", Red]}

I would like to create a string looking like

enter image description here

StringJoin doesn't enable to tackle the problem

$\endgroup$
3
  • $\begingroup$ Do you need a String or do you need to make the output look like what you show? If the latter, use Row on the list. $\endgroup$ – Szabolcs Dec 15 '19 at 10:44
  • $\begingroup$ It is enough to make an desired output. $\endgroup$ – Konstantin Dec 15 '19 at 10:58
  • $\begingroup$ Many related questions: (7008), (7732), (10990), (29817), (45132), (73822), (100371) $\endgroup$ – Mr.Wizard Dec 15 '19 at 13:53
3
$\begingroup$

For display purposes you can use Row (as suggested by Szabolcs in comments).

Alternatively, you can use StringRiffle or StringTemplate after wrapping styled strings with ToString[#, StandardForm]:

StringRiffle[ ToString[#, StandardForm] & /@ {Style["Red", Red, 24], 
    Style["Blue", Blue, 16]},""]

enter image description here

StringTemplate["````"] @@
 (ToString[#, StandardForm] & /@ {Style["Red", Red, 24], Style["Blue", Blue, 16]})

enter image description here

To get a string object you can use

str = StringJoin[
  ToString[#, StandardForm] & /@ {Style["Red", Red, 24], Style["Blue", Blue, 16]}]

enter image description here

Head[str]

String

$\endgroup$

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