Does anyone know whether it is possible to combine\join two styled strings?

That is, while the following code works fine:

omega = "text";

when I try to join my omegas into one string but having different colors like this


mma returns this error:

StringJoin::string: String expected at position 1

It's clear to me that the objects I'm trying to join have head Style not String, but may be there is a way to produce a string that has its parts painted in different colors?


Use Row to join them:

Omega = "text";
joined = Row[{Style[Omega, Lighter[Blue, .1]], Style[Omega, Darker[LightBlue, .1]]}];
| improve this answer | |
  • $\begingroup$ Though this is a quick fix for most of the cases, whenever the result is expected to be a String, I prefer WReach's solution. Not to mention that Row breaks lines differently than how the FE prints strings. $\endgroup$ – István Zachar May 13 '19 at 18:07
  • $\begingroup$ Hi István, I agree. Since I read WReach's solution I have found it very useful. $\endgroup$ – Chris Degnen May 13 '19 at 18:32

Given two styled items:

omega = "text"

items = { Style[omega,Lighter[Blue,.1]], Style[omega,Darker[LightBlue,.1]] }

We can produce a single string with both stylings if we convert each item into a StandardForm string prior to joining them:

Apply[StringJoin, ToString[#, StandardForm] & /@ items]

The following screenshot shows the results:

styled concatenation session

| improve this answer | |
  • 5
    $\begingroup$ Nice. Also Row@items ~ToString~ StandardForm though the format is not as clean. $\endgroup$ – Mr.Wizard Sep 23 '12 at 22:11

If you evaluate or print the following string you get what you want:

"\!\(\*StyleBox[\"text\",FontColor->RGBColor[1, 0, 0]]\) \!\(\*StyleBox[\"text\",FontColor->RGBColor[0, 0, 1]]\)"

Mathematica graphics

This is a single string and it contains color information for different substrings.

Do[Print@"\!\(\*StyleBox[\"text\",FontColor->RGBColor[1, 0, 0]]\) \!\(\*StyleBox[\"text\",FontColor->RGBColor[0, 0, 1]]\)", {5}]

Mathematica graphics

| improve this answer | |

Following the details from the Mathematica documentation page for Style especially the examples in the section Options:

Style[omega, Lighter[Blue, .1]] Style[omega, Darker[LightBlue, .1]]

output for the two Style boxes in one input separated by a single blanc

This follows the methodology offered in Options>SingleLetterItalics: Style[T, SingleLetterItalics -> False] + Sin[x y] // TraditionalForm all three output boxes are present each after eachother: sin(x y)+T

sin(x y)



in one output.

The gray x is allowed too.

This works in Print[Style[omega, Lighter[Blue, .1]] Style[omega, Darker[LightBlue, .1]]] output

To make Yourself an impression compare:


to Column[Options[Style]]

Options[Style] // Length

(190) Options[Row] // Length (15)

One Style for one expression with many, up to 190 options.

Mathematica has too the built-in StyleBox. That is in need for the built-in DisplayForm to be shown as Style in the output line. And for this there is the built-in AdjustmentBox. This built-in together with RowBox allow wide range formattings of the combination of Style for strings displayed in the output line. The authors of the Mathematica documentation cut the length of the input already at this depth of nesting.

   AdjustmentBox[Style["x", RGBColor[0.8 - b, 0.2 - b, 0.5 - b]], 
    BoxBaselineShift -> b], {b, -.2, .2, .2}]] // DisplayForm

enter image description here

Have fun with layout and color.

| improve this answer | |
**Bold and underline a String and return String
  If you like, you can mix and match any style combo by
  simply adding or subtracting lines the following Module**

toEm[str_  ] := Module[{s1,s2},
    s1 = ToString[Style[str, Bold] , StandardForm];
    s2 = ToString[Style[s1, Underlined] , StandardForm]
| improve this answer | |
  • 1
    $\begingroup$ The problem was to join two strings, each of which has been styled with Style[], not to apply two styles to one string. See @WReach's answer, for instance. -- BTW, you can combine your two steps into one: ToString[Style[str, Bold, Underlined], StandardForm] $\endgroup$ – Michael E2 Sep 7 at 14:49

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