Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

That is, while the following code works fine:

omega = "text";
omega<>omega

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

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

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?

share|improve this question
    
Related: mathematica.stackexchange.com/q/7732/121 –  Mr.Wizard Sep 23 '12 at 22:01
add comment

3 Answers 3

up vote 24 down vote accepted

Use Row to join them:

Omega = "text";
joined = Row[{Style[Omega, Lighter[Blue, .1]], Style[Omega, Darker[LightBlue, .1]]}];
Print[joined]
share|improve this answer
add comment

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

share|improve this answer
4  
Nice. Also Row@items ~ToString~ StandardForm though the format is not as clean. –  Mr.Wizard Sep 23 '12 at 22:11
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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