2
$\begingroup$

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).

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – partial81
    Apr 8, 2013 at 8:54
  • $\begingroup$ 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. $\endgroup$
    – mac
    Apr 8, 2013 at 9:24

3 Answers 3

2
$\begingroup$

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}]
$\endgroup$
1
  • $\begingroup$ It seems to work fine in Mathematica 9.0.1! $\endgroup$
    – mac
    Apr 8, 2013 at 12:36
3
$\begingroup$

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

Button["red", FrontEndTokenExecute[InputNotebook[], FontColor, Red]]
$\endgroup$
1
  • $\begingroup$ Works for me too! $\endgroup$
    – mac
    Apr 8, 2013 at 12:35
0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Apr 8, 2013 at 9:20
  • $\begingroup$ @IstvánZachar You're right. Thanks for the correction. I just copied the output of the formated cell using CMD + Shift + E. $\endgroup$
    – dotcs
    Apr 8, 2013 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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