8
$\begingroup$

I wonder why PasteButton["Plot", Plot[\[SelectionPlaceholder], {x, 0, 10}]] applied to x^2 --- highlighted --- doesn't work.

$\endgroup$

2 Answers 2

7
$\begingroup$

In case you want to plot in place you can use something like:

Button["Plot", 
   NotebookWrite[ 
      InputNotebook[],  
      ToBoxes @ Plot[ 
         Evaluate @ ToExpression @ CurrentValue @ "SelectionData", 
         {x, 0, 10}
      ]
   ]
]

You may want to add Method -> "Queued" for more complicated plot to avoid timeout.

And if you want x from the selection to be scoped properly, e.g. to give a plot even if x already has a value, then wrap the procedure inside the Button with Block[{x},...].

enter image description here

$\endgroup$
4
  • $\begingroup$ On my computer I have the message Could not parse RowBox[{^,2}] as input. $\endgroup$ Apr 7, 2016 at 8:35
  • $\begingroup$ Sorry you are right! Have you a solution for the extension of the question ? $\endgroup$ Apr 7, 2016 at 9:40
  • $\begingroup$ To do the same thing without immediate evaluation in such a way to be abble to make modification to the plot $\endgroup$ Apr 7, 2016 at 11:40
  • $\begingroup$ @cyrille.piatecki isn't Algohi's answer allowing you that? $\endgroup$
    – Kuba
    Apr 7, 2016 at 11:48
7
$\begingroup$

According to the documentation, "PasteButton, evaluates its arguments in an ordinary way, so that expr is immediately evaluated"

It is like you evaluate Plot[\[SelectionPlaceholder], {x, 0, 10}] which will give empty plot.

This could be one solution:

PasteButton["Plot", Defer[Plot[\[SelectionPlaceholder], {x, 0, 10}]]]

$\endgroup$
1
  • $\begingroup$ Perfect. And How to do tha same without evaluation --- to be able to change marginaly some parameters ? $\endgroup$ Apr 7, 2016 at 8:23

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.