Background:I want to create a "palette" that contains buttons that evaluate the selected part of the expression and replace the selection with the result, just like what the built-in palette "Algebraic Manipulation" does. I found a way to achieve this feature in the documentation of Button
:
Button[Defer[Expand[\[SelectionPlaceholder]]], None, BaseStyle -> "Evaluate"]
As you know, this creates a button named Expand that Expand
the selected expression and replace it with the result.
(a + b)^5
Select the expression above and click the button, the expression will change to:
a^5 + 5 a^4 b + 10 a^3 b^2 + 10 a^2 b^3 + 5 a b^4 + b^5
I think this feature is very convenient, and I would like to create more stuff with it.e.g: create a button of this style with TableForm
,I tried the following code:
Button[Defer[TableForm[\[SelectionPlaceholder]]], None, BaseStyle -> "Evaluate"]
Well, this is a simple imitation of the example shown above. But it gives me a button with a single SelectionPlaceholder.
Finally,I found that Defer
won't work with TableForm
. When you execute:
Defer[TableForm[{1, 2, 3}]]
It gives me the TableForm
of {1,2,3}
. But when I use the following code:
Tableform = TableForm; Defer[Tableform[{1, 2, 3}]]
it works fine:
Tableform[{1, 2, 3}]
And then I can achieve what I want with this:
Tableform=TableForm;
Button[Defer[Tableform[\[SelectionPlaceholder]]], None, BaseStyle -> "Evaluate"]
Q: Could anyone gives me more details of how these things work?
Bonus: How can I create this kind of "inline execute" palette with more complicated user-defined functions? (I would like to add it to the Palettes Menu, but I have no idea of where to get my user-defined function initialized every time I start the palette.) And is there any other way to create a "inline execute" button?
Button[Defer[TableForm][\[SelectionPlaceholder]], None, BaseStyle -> "Evaluate"]
, right? $\endgroup$$Pre = (Print[HoldForm @ #]; #) &
one can see that MMA converts any kind of expression to a specifiedForm
right after it parses it. When the expression is not aForm
built-in symbol, it remains un-formatted. The problem originates from the way that MMA reads in the input, notDefer
$\endgroup$Button["Name", ToExpression[NotebookRead[SelectedNotebook[]], StandardForm, Function[in, Paste[TableForm[in]], HoldAllComplete]]]
$\endgroup$