3

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

Konstantin
  • 906
  • 5
  • 8

1 Answers1

4

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

kglr
  • 394,356
  • 18
  • 477
  • 896