From the menu selection (Input |> CreateButton) you can make buttons with ButtonStyles Paste, Evaluate, EvaluateCell, CopyEvaluate and CopyEvaluateCell. Each of these ButtonStyles are intended to be used when something is selected in an Input or Output cell and they all do something with the selected expression and the input cell.  Making one of these buttons is simple. Suppose for example you want a button that will Simplify the selected expression. To do that you would do the following:

(1)  Type Simplify[■] as I did in the next cell.
       Note the ■ symbol can be entered as  \[SelectionPlaceHolder] or using the alias  :spl:  or using the 'Complete Characters' palette.
(2)  Select "Simplify[■]" in the input cell.
(3)  Make the menu selection (Input |> Create Button |> Evaluate)
(4)  Select the cell bracket for the input cell containing "Simplify[■]".
(5)  Make the menu selection (File |> Generate Palette from Selection).

You will then have a palette containing the button just describe and it will work like those in the Algebraic Manipulation palette.


As far as I am aware you need to deal with lower level boxes if you want to make a palette of several buttons like the one I discuss above.  You can make such a palette by evaluating the next cell and doing the following:
(1)  Select the cell bracket for the cell created when the next cell is evaluated.
(2)  Make the menu selection (File |> Generate Palette from Selection) and the palette will be displayed in it's own notebook.

Cell[BoxData[GridBox[{ {ButtonBox[RowBox[{"Factor", "[", " ... 4;"Evaluate"]} }, RowSpacings0]] , "Input"]//CellPrint

All buttons above above had ButtonStyle "Evaluate".  This and all other built-in ButtonStyles are explained below.

Built-in  ButtonStyles

Evaluating the next cell makes a sample palette containing a button with each of the built-in Button-Styles. Once the resulting palette is displayed, you can move your mouse pointer over one of the buttons and the ButtonStyle for that button will be shown in the bottom of the window. You can verify that each of the buttons works as I describe below.

nb = NotebookCreate[VisibleFalse] ;     NotebookWrite[nb, {Cell[BoxData[GridBox[{{ ... lickSelectFalse, ShowCellBracketFalse, ActiveTrue, VisibleTrue] ;

Clear[x] ;

Paste  ButtonStyle

Evaluate  ButtonStyle

EvaluateCell  ButtonStyle

CopyEvaluate  ButtonStyle

CopyEvaluateCell  ButtonStyle

Make your own ButtonStyle

If you tried the sample button with setting  ButtonStyle→EvaluateCell  you would have noticed that after the button is used the contents of the new output cell are selected.  I would prefer that after the button finishes nothing is selected, and the insertion point is after the new output cell, and I made a new ButtonStyle to do this.  To try this new ButtonStyle the "Prototype for style: EvaluateCell2" cell below should be pasted into your style sheet under "Palatte Styles".  You can open the Style Sheet with menu selection (Format |> Edit Style Sheet ).

I am using Mathematica 4.1 under Windows 98, and the ButtonStyle wouldn't work when I added it to a private StyleSheet. However, the ButtonStyle would work when I pasted the prototype below in the style sheet (Default.nb).  You might have the same problem.

To see the details of the "EvaluateCell2" prototype in the next cell select the cell bracket and make the menu selection (Format |> ShowExpression).

The "EvaluateCell2" prototype above is almost identical to the one for the "EvaluateCell" prototype  The only difference is in the ButtonFunction setting and the difference is colored red below.  The next cell shows the "EvaluateCell"  ButtonFunction setting.

ButtonFunction (FrontEndExecute[{FrontEnd`NotebookApply[FrontEnd`InputNotebook[], #, A ... ook[], All, Cell, 1], FrontEnd`SelectionEvaluateCreateCell[FrontEnd`InputNotebook[], All]}] &)

The next cell shows the ButtonFunction setting I made for the "EvaluateCell2"  prototype.

ButtonFunction (FrontEndExecute[{FrontEnd`NotebookApply[FrontEnd`InputNotebook[], #, A ... k[], All, Cell, 1], FrontEnd`SelectionEvaluateCreateCell[FrontEnd`InputNotebook[], After]}] &)

If you want to learn more about FrontEndExecute, and the  FrontEnd`*  functions see chapter 56 of The Beginner's Guide To Mathematica, by Jerry Glynn and Theodore Gray.  

You will also notice that all of the built-in ButtonStyles do strange things to the selected notebook if nothing is selected.  If you are ambitious you could go into the shared style sheets and change the ButtonFunction for the built-in ButtonStyles to ensure they do nothing if a part of an input expression is not selected.

That covers most of this subject but you might want to also read   

Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page