pedro silva
pedro silva

Reputation: 85

Mathematica: how to have text in multiple colors?

I'd like to have a manipulate control, something like {{wAB, 1, "AB"}, 0, 1, Appearance -> "Labeled"}, but would like the A and the B to be different colors, say, Red and Blue.
I can change the overall color with Style["AB",Red], but haven't been able to get the A and the B in different colors. Any help would be appreciated!

Upvotes: 4

Views: 3318

Answers (3)

Mr.Wizard
Mr.Wizard

Reputation: 24336

Although it is safer to do styling with Style you can actually color the characters of the string directly using the Format menu or keyboard shortcuts, and Mathematica will preserve it in the dynamic control:

Mathematica graphics

Upvotes: 3

Nasser
Nasser

Reputation: 13131

you mean like this?

Manipulate[
 wAB,
 {{wAB,1,Row[{Style["A", Red], Style["B", Blue]}]},0,1,Appearance->"Labeled"}
 ]

enter image description here

and if you prefer to define the decoration part separately (which can be useful for larger and more complicated controls) and reference it in controls later on (like declaring a variable, sort of, but it is a macro actually) and reuse it for different controls, then you can use With, like this

Manipulate[wAB,

 Evaluate@With[

   {myStyle = Row[{Style["A", Red], Style["B", Blue]}]},

   {{wAB, 1, myStyle}, 0, 1, Appearance -> "Labeled"}

   ]
 ]

Upvotes: 6

theWalker
theWalker

Reputation: 2020

If vertical position is x, you must print one letter on x, then count width (w) and print next letter with another color on the new position x+=w.

Upvotes: 0

Related Questions