Assume the following very complex palette:

  PasteButton[FontColor -> RGBColor[1, 0, 0]]

Its purpose is to insert a FontColor rule. It works like expected in a notebook but I plan to use it inside raw cell expressions. Please try the following two examples. Paste the following code into a notebook

 Cell[Cell["abc", "Text"]]

You get a text cell saying "abc". Now go with the cursor anywhere between the "abc" and press Ctrl+Shift+E (this is Cell->Show Expression) to see the underlying cell data. Go directly in front of the last closing bracket, make a comma and press the palette-button. You should get

 FormBox["abc", Text]],FontColor->RGBColor[1,0,0]]

Pressing Ctrl+Shift+E again and the text is red. Works like a charm. Now try the following cell with the same approach:


Here I get (Linux 64, Mma 8.0.4)

  RowBox[{"FontColor", "->", RowBox[{"RGBColor", "[", 
  RowBox[{"1", ",", "0", ",", "0"}], "]"}]}]

which is not what I expected and which does not work.

Question: Can anyone explain me what's going on here? Is the PasteButton aware of the context?

Background: The background is, that I have created a palette which I want to use to adjust style-sheet definitions. That was, when I stumbled over this behaviour.

Fix: One possible way to fix this is to use NotebookWrite directly instead of the PasteButton. Disadvantage of this approach is, that the expression can no longer be inserted into a normal notebook, because there you have to insert cell-expression.

  • $\begingroup$ It's very weird. Bug? It doesn't like StyleData cells it tries to paste it as a box expressions $\endgroup$ – Rojo May 30 '12 at 1:16
  • $\begingroup$ Maybe the magic word is "effectively" here: You could use the option BaseStyle->"Paste" and a normal Button and you get the same weird behavior. The doc of "Paste" says "effectively use NotebookApply upon the current selection using the button label as the applied data". $\endgroup$ – halirutan May 30 '12 at 1:28
  • 1
    $\begingroup$ @halirutan You could try Button["paste FontColor", Paste[Defer[FontColor -> RGBColor[1, 0, 0]]]]. $\endgroup$ – Heike May 30 '12 at 7:51
  • $\begingroup$ @Heike This seems to work like I had hoped. Do you have an idea, why and when (even with the Defer) the translation from -> to \[Rule] happens? It seems that wrapping InputForm around it always keeps the -> form. $\endgroup$ – halirutan May 30 '12 at 12:21
  • 1
    $\begingroup$ Bug. Report it to tech support. $\endgroup$ – John Fultz Jun 2 '12 at 20:21

I think this is a bug in the pasting which is attempting to be smart about what to paste, since in the case of pasting into a cell you actually write the box expressions, while when you are working with the cell expression you want the input-form inserted. In any case, this seems to work in both cases:

 Button["Font Color", 
    NotebookWrite[SelectedNotebook[], ToBoxes[FontColor -> RGBColor[1,0,0]]]

Playing around with this I saw no problems with using it in a normal notebook. If it does cause problems with your other code please post code describing the issues.

| improve this answer | |
  • $\begingroup$ Yes, this is similar to what I had as work-around too. $\endgroup$ – halirutan Jun 7 '12 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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