Introduction to Toolbars

Mathematica's unique symbolic architecture makes it easy to add toolbars with any possible appearance and action to a Mathematica notebook.

Ruler

The ruler is a toolbar used to set the text margins of selected cells and the indentation of cell names and keywords. The notebook ruler can be added to a notebook by selecting Show Ruler in the Window menu or programmatically by using the notebook option WindowToolbars.

WindowToolbars->"RulerBar"notebook option value to show the margin settings ruler
Window Show Rulermenu item toggling the ruler display along the top of a notebook

Show ruler.

For example, the ruler can be shown in the current notebook by evaluating SetOptions[EvaluationNotebook[], WindowToolbars->"RulerBar"].

1.gif

Docked Cells

Docked cells are cells that are displayed as a toolbar or banner at the top of the notebook and treated as deployed.

DockedCells->cellsnotebook option that gives a list of cells that are displayed as "docked" at the top of the notebook

Docked cells.

While any type of cells can be included in cells, it is recommended that only those with the cell style be used. The style ensures that the cell behaves like a toolbar.

One way to implement a docked cell in the current notebook is to use SetOptions[EvaluationNotebook[], DockedCells->cells].

Here is a basic example of a docked cell containing a string.

This next example contains the mathematical expression in TraditionalForm.

Control objects such as action menus, popup menus, buttons, input fields, etc. can also be used in a docked cell. Create the toolbar contents and then wrap ToBoxes around it to generate the front end box structure automatically. Then place the ToBoxes expression into the BoxData of a Cell. For example, the following input creates an action menu toolbar that places a cell with the chosen style at the insertion point.

In[2]:=
Click for copyable input

If the spacing between the toolbar elements is static, some elements in a docked cell may become hidden at certain window widths. This can be solved by scaling the item sizes in Grid. The following example includes a label and ButtonBar.

In[3]:=
Click for copyable input

Multiple docked cells can be included in a notebook.

Extended Examples

Word Processing Toolbar

This example includes buttons for setting text alignments and spacings, and opening the font dialog, print dialog, and the Documentation Center. In this example, each of the toolbar elements is generated separately and then converted into boxes using ToBoxes in the CreateWindow statement at the end.

Here is a row of buttons controlling the spacing and alignment of the notebook text.
In[6]:=
Click for copyable input
Out[6]=
This button opens the Fonts dialog to set formatting options for text selections.
In[35]:=
Click for copyable input
Out[35]=
This button opens the system print dialog to print the notebook.
In[36]:=
Click for copyable input
Out[36]=
This button opens the Documentation Center.
In[37]:=
Click for copyable input
Out[37]=
Here are the labels for the buttons in the toolbar.
In[38]:=
Click for copyable input
Out[38]=
Here is the completed toolbar in a new notebook.

Decorative Toolbar

Images can also be used as docked cells. Below, a 1399×24 pixel image is being used as a decorative docked cell similar to those seen in many stylesheets.

In[1]:=
Click for copyable input

Defining Toolbars in a Stylesheet

A docked cell can also be defined in a stylesheet instead of in an individual notebook. For example, notebooks with the NaturalColor stylesheet all have the following docked cell at the top of each notebook.

12.gif

The DockedCells option value for stylesheets is stored in the notebook style definitions cell within a stylesheet notebook. To add a docked cell, start by opening a stylesheet notebook. If the cell does not already exist in the stylesheet, create a new cell with an expression of the form Cell[StyleData["Notebook"]] and place it near the top of the notebook.

13.gif

Add the docked cell(s) cells to the stylesheet by updating the notebook style definitions cell expression to resemble the following form: Cell[StyleData["Notebook"], DockedCells->{cells}]. For example, print the cell from the previous example.

In[2]:=
Click for copyable input

Unformat the printed cell and copy the cell expression into the notebook style definitions cell as the DockedCells option value.

15.gif

Finally, choose Cell ► Show Expression to reformat the notebook style definition cell.

16.gif

Any new notebook with this stylesheet will contain the docked cell.

17.gif

New to Mathematica? Find your learning path »
Have a question? Ask support »