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.

I use Mathematica 9.0.1 and I want change the color of a selection in a text cell using a button. But when I use this code it changes the color of the entire cell.

Row[Button[Graphics[{#, Rectangle[]}, ImageSize -> 20], 
SetOptions[NotebookSelection[InputNotebook[]], FontColor -> #], Appearance -> None] & /@ 
   {ColorData["HTML"]["RoyalBlue"], ColorData["HTML"]["OliveDrab"],
    RGBColor[0.419608, 0, 0.733333], Black}]

What do I do wrong? It used to work in Mathematica 8 and 9.0.0. I want it to work exactly like the Text Color command in the Writing Assistant palette (or the Format >> Text Color menu command).

share|improve this question
    
Where are the words in your example which should be colored? Perhaps you add a picture to your question and a picture of the desired output - this would be helpful, at least for me. –  partial81 Apr 8 '13 at 8:54
    
To be specific i would like to create a button which will color any kind of text.Not the entire cell but only the part which i choose with the mouse. –  mac Apr 8 '13 at 9:24
add comment

3 Answers

up vote 2 down vote accepted

CurrentValue seems to be needed, then it works:

    Row[Button[Graphics[{#,Rectangle[]},ImageSize->20],
    CurrentValue[NotebookSelection[InputNotebook[]],FontColor]=#,
    Appearance->None]&/@{ColorData["HTML"]["RoyalBlue"],
    ColorData["HTML"]["OliveDrab"],RGBColor[0.419608,0,0.733333],Black}]
share|improve this answer
    
It seems to work fine in Mathematica 9.0.1! –  mac Apr 8 '13 at 12:36
add comment

I don't have v9 to test, but perhaps this?

Button["red", FrontEndTokenExecute[InputNotebook[], FontColor, Red]]
share|improve this answer
    
+1, works perfectly in v9. –  István Zachar Apr 8 '13 at 10:50
    
Works for me too! –  mac Apr 8 '13 at 12:35
add comment

The following

Cell[TextData[{"This is some example text in ", 
StyleBox["blue", FontColor->RGBColor[0, 0, 1]]}], 
"Text",CellChangeTimes->{{3.57440047356804*^9, 3.574400478269321*^9}}]

will result in

Example Text with some part in blue

Check out the Writing Assistant Palette. There you can find a lot of formatting options.

share|improve this answer
    
Pardon me but if I paste this literally, it won't produce the desired cell output. One has to wrap it in CellPrint. Also, CellChangeTimes is unncessary. –  István Zachar Apr 8 '13 at 9:20
    
@IstvánZachar You're right. Thanks for the correction. I just copied the output of the formated cell using CMD + Shift + E. –  chromate Apr 8 '13 at 9:26
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.