Introduction to the Mathematica Front End: Version 3

©1998 Louis J. D'Andria.

This session covers the essentials of using the Mathematica 3 front end. There will be a goal-oriented, hands-on look at using Version 3 technology, style sheets, and palettes, followed by a brief tour of some features of the new version, including hyphenation, spell checking, and interactive graphics. If time permits, there will also be a discussion of the technology that makes programmable documents possible.

Main Topics

What Is The Front End?

The front end is that part of Mathematica that serves as a word processor and document typesetter.  This is separate from the part of Mathematica that performs calculations, which is known as the kernel.  You typically use the front end as a word processor to create a notebook, which is a portable document that enables you to edit and view not only text and graphics, but also live typeset mathematical expressions.  Those expressions can also be evaluated, or embedded within other text.

Some notebooks contain tables of buttons, which when clicked perform complex actions.  Such notebooks are known as palettes.

In this talk, we will see a wide range of features of the Mathematica front end, including basic creation of notebooks and palettes, customizing their look and feel, embedding live objects within them, and finally looking ahead to features coming in the new version of Mathematica.

Palettes and Typesetting

Using Palettes

For typical editing tasks, one can use the palettes available in the File ⊳ Palettes submenu, such as the Basic Input palette, which opened the first time you launched Mathematica. These can be used for evaluation, or simply formatting.  If you have something selected when you click a button, it will be substituted for the ■ in the button.  The other placeholders, □, can be filled after pressing a button.  Cycle through the □ locations by pressing ⇥.

(∫_\[Placeholder]^\[Placeholder]) ((\[SelectionPlaceholder]^\[Placeholder])/(Sqrt[\[Placeholder]])) \[DifferentialD]\[Placeholder]

Some buttons cause an evaluation to begin, such as those in the Algebraic Manipulation palette.

Sin[α + 2 β]

Typesetting Without Palettes

For common editing tasks, there are alternatives to using the palettes.  For example, you can enter the following integral in one of a few ways: using the palette, using the Edit ⊳ Expression Input submenu, or using the keyboard.

(∫_0^α) ((E^x)/(Sqrt[2])) 
        \[DifferentialD]x

To do this, it is necessary that you know not only how to create the typesetting structure, but also how to input the special characters (∫, α, e, and d).  You can look this information up in the back of the Mathematica book, or by pointing to the character in the CompleteCharacters palette.  We'll discuss other methods later.

Every character in the Mathematica font set has a long name associated with it.  To type this character, you type '\[' followed by the long name, followed by a closing ']'.  As this last character is typed, the long name automatically collapses into the character.  Try this with the character with long name Integral.

Many characters also have a short name, which is typed by pressing the escape key, then the short name, and then the escape key again.  Try this with EscintEsc.

Creating Palettes

You can create an array of buttons using the Input ⊳ Create Table/Matrix/Palette menu item.  You can insert the ■ character by typing \[SelectionPlaceholder ] or EscsplEsc.  Other placeholders are input by typing \[Placeholder ] or EscplEsc.

({{Plot[\[SelectionPlaceholder], {x, 0, 2  π}]; }, {Plot[\[SelectionPlaceholder], {x, 0, 10  π}]; }, {Plot[\[SelectionPlaceholder], {x, 0, \[Placeholder]  π}]; 
            }})

By default, such buttons will be "Paste" style buttons.  If you want a button to evaluate instead, highlight the button and choose Input ⊳ Edit Button, and select "Evaluate" as the button's ButtonStyle.

When you are ready to turn a cell into a palette, select the cell and choose File ⊳ Generate Palette From Selection.

Sin[x]

To edit a palette, click in the palette's title bar to bring it to the front, and then select File ⊳ Generate Notebook from Palette to create a notebook.  Make any desired changes, and then select File ⊳ Generate Palette From Selection to use or save the edited palette.

Links to Web Pages

To create a button that links to a web page, use the Input ⊳ Create Hyperlink dialog box, and type the URL into the field provided.  For instance, here is a web page that describes the use of Mathematica and AppleScript.

Go to Wolfram's home page

Cell Transformations

By defaut, when you start typing, Mathematica creates a StandardForm input cell.  This is appropriate for entering expressions for evaluation or display, and can contain typeset information.  TraditionalForm is also suitable for typesetting, and adds such features as italicizing single-letter variables, and using Times instead of Courier to give a more textbook look.

You can use the Cell ⊳ Convert To submenu to typeset a cell in a different way, or as a graphical image.  This uses rules built into the kernel, which has special mathematical knowledge such as formatting of integrals.

Integrate[E^x/Sqrt[2], {x, 0, a}]

If you merely want to change which options are used to display the selected cell, use the Cell ⊳ Display As submenu.  This does no interpretation, and does not use the kernel.

To change cell in one text-based style to another, use the Format ⊳ Style menu.

Inline Cells

To change the type of cell that is created when you start typing, you can use the Cell ⊳ Default Input FormatType submenu.  InputForm is tantamount to input cells from Mathematica 2.2, which cannot handle typeset information.

Text cells also cannot display typeset expressions themselves, however you can easily create - usually by typing or clicking a palette button - typeset expressions within text cells by using inline cells.  If you are in a text cell and type something that would normally form a typeset structure, an inline cell is automatically inserted and the structure is created.  You can also type Ctrl[9] to explicitly begin an inline cell from the keyboard, and Ctrl[0] or Ctrl[␣] to exit the inline cell.

There is typically a rose-colored background present so you know when you are actively editing within an inline cell.  You can use StandardForm or TraditionalForm for your inline cell format by choosing one from the Cell ⊳ Default Inline FormatType submenu.

Here is a sample text cell which contains a few typeset expressions within inline cells, such as \(TraditionalForm
      \`∫\_1\%∞\( 1\/j\^2\) dj\) and (({{1, 1}, {1, 0}})).

Notebooks Are Expressions

Notebook

Notebooks are Mathematica expressions with head Notebook.  The first argument is a list of Cell expressions, and other arguments are options to Notebook.  Notebook expressions can be stored in files, manipulated by the kernel, or transferred as text via email or ftp.  Open this notebook in a text editor, for example.

Cell

The easiest way to see a cell expression is to select a cell's bracket, and use the Format ⊳ Show Expression menu command.  This menu command is very much like the kernel FullForm, in that it shows you everything that would otherwise be hidden about the cell.

Note that not only text cells, but typeset and graphics cells are merely Mathematica expressions.  These expressions are saved in a notebook file as ASCII text, which allows the files to be send via email or ftp to anyone using Mathematica on any platform.  More on this later.

(∫_0^α) ((E^x)/(Sqrt[2])) 
        \[DifferentialD]x

Options

All customizable aspects of a notebook window or cell are customized by setting options.  Usually, you set options by selecting some menu items, such as for the following cell.

This cell has large cyan text and a gray background because of the options in the underlying Cell expression.

Besides the menus, more options and settings are available in the Option Inspector.

Style Sheets and Style Environments

Style Sheets

Style sheets allow a modular, flexible, and powerful way to affect the display of a notebook or set of notebooks.  There are a dozen built-in style sheets to choose from, or you can add your own.

Built-in

The currently installed style sheets are listed in the Format ⊳ Style Sheet menu.  Changing your notebook to use one of these is as easy as selecting one from this menu.

You can install a style sheet so that it appears in this menu by placing the style sheet notebook in one of the directories among those in the options setting for StyleSheetPath.  The main location is SystemFiles/FrontEnd/StyleSheets.

Style sheets typically define the styles in the default style sheet, including styles like Title, Section, and Text.  In addition, some style sheets define new styles such as styles for automatic numbering or cells that should be automatically hidden when the notebook is printed.

Private vs. Shared

A shared style sheet is a separate notebook file that is read in whenever some notebook references it.  This reference is actually an option setting for the Notebook expression, StyleDefinitions -> "Default.nb".

A private style sheet should be used if the style sheet will only ever be used with a single notebook, or if you want the convenience of having the notebook and the style sheet combined into a single file.  The way you import a style sheet to make it private is to select Format ⊳ Edit Style Sheet….  When you have imported a private style sheet, the Notebook expression corresponding to that style sheet is stored as the setting for the option StyleDefinitions.

NotebookPut[
      Notebook[{Cell["🐺", "myStyle"]}, 
        StyleDefinitions -> 
          Notebook[
            {Cell[StyleData["myStyle"], FontSize -> 288, 
                TextAlignment -> Center, 
                FontColor -> RGBColor[ .7,  .38,  .27]]}]]];

Creating and Editing Style Sheets

To edit a notebook's style sheet, select Format ⊳ Edit Style Sheet.  You will be asked if you want to edit the shared style sheet, or import a private copy for editing.  Once the style sheet is open, select the relevant cells and change their definitions by using the menus or the option inspector.

To create new styles, begin with a copy of an existing, similar style definition, and change its name via Format ⊳ Show Expression.

If you edit a shared style sheet, the changes will affect all the notebooks that use that style sheet.  If you edit a private style sheet into a notebook, the changes only affect that notebook.

To create a new shared style sheet, it is easiest to begin with a copy of one of the existing style sheets.  These can be found in the Mathematica layout, at SystemFiles/FrontEnd/StyleSheets.

For more information on creating and editing style sheets, see the online documentation.

Setting Your Default Style Sheet

Set something other than Default.nb by setting the option DefaultStyleDefinitions in the Option Inspector.

Style Environments

Mathematica solves the problem of needing different resolutions for printouts and for screen viewing by defining style environments.  These environments are ways to tweak the basic style definitions to be optimized for various media.  Typical enviroments include Working, Presentation, and Printout.  The Format menu contains two submenus, Screen Style Environment and Printing Style Environment, that allow you to specify defaults for viewing the notebook on screen or on a printout.

For more information on style environments, see the online documentation.

Adding and Removing Options

What Is an Option?

The customizable aspects of the front end, from colored text to defining remote kernels to your default style sheet, are all described by options.  A searchable list of these options can be seen by selecting Format ⊳ Option Inspector.

These options are identical to kernel options in syntax - they come after the other arguments to the expression, and each is in the form of a rule,  optionName -> optionSetting.  There are options that affect the display of text, cells, notebooks, or even menu items.

Selections and Cells

Many menu items merely set an option's value — e.g., the Format ⊳ Text Color submenu sets the FontColor option.  You can use values other than those in the menus by using the Option Inspector.  You can even edit the cell expression directly by using the Format ⊳ Show Expression menu item.

COLOR ME

In the Option Inspector, an indicator to the left of the option's name (generally a '+' or a '●') shows that the option has an explicit setting for the current selection or notebook.  Viewing the Option Inspector as Text lists only those options that have the indicator in front of them for the current selection, cell, notebook, or what have you.  You can specify a new value by editing the text in the option inspector and then pressing ⏎.

You can clear an option's setting using the Option Inspector by clicking on the indicator.  This will cause the indicator to disappear, and the option setting to be removed from the expression.  You can also use the Format ⊳ Remove Options menu to remove all options in your current selection.

Notebooks

In the Option Inspector window, you can select Notebook from the "Show option values for" popup.  This causes the notebook options to be activated.  Any changes you make will now affect the foremost notebook.  This allows you to specify more values for Background or Magnification than are found in the menu.

Front End

By selecting Global in the Option Inspector window, you will gain access to the options that affect the general use of the front end.  This includes where the front end looks for style sheets, what if any language kits to use, what remote kernels have been defined, and changes to the default setting of dialog boxes.  You will typically not need to set these options by using the Option Inspector; rather, they will be set automatically when you interact with dialog boxes such as Kernel ⊳ Kernel Configuration Options or Find ⊳ Find.

Options that affect global behavior of the front end are automatically saved in the front end's init.m file, which lives in $PreferencesDirectory.  Note that this has nothing to do with kernel init.m files.

Inheritance and Scope

The front end uses option inheritance to determine what options values apply to your text, cells, and notebooks.  For example, if your notebook has a background color (via the option Background), all the cells will inherit that background color except for cells that have a Background setting themselves.  The same thing goes for text within a cell, which inherits first from the cell it is in, and then for the notebook containing it.

NotebookPut[
      Notebook[{Cell[
            TextData[
              {"This cell is ",  
                StyleBox["red",  
                  Background -> RGBColor[0, 0, 1]]}], "Title",  
            Background -> RGBColor[1, 0, 0]], 
          Cell["This cell isn't", "Title"]}, 
        Background -> RGBColor[1, 1, 0]]];

Here is a partial hierarchy of inheritance in the front end, which includes more levels than the listing given in the popup menu in the Option Inspector.  If an option is set for some level in the following column, the lower levels inherit that setting unless it is explicitly overridden.

({{Global}, {Notebook options}, {Style Environment}, {Cell Style}, {Cell Style in Environment}, {Cell options}, {Selection}})

Some options like Background can be set at any of these levels.  Other options, like KernelConfigurationOptions, can only be reasonably used at a few levels.

How'd They Do That?

Just as you can use options to do many things, you can also tell how other people have done something by studying the option settings they use.  For example, can you figure out how to enter the character ‡?  Or how to edit the following button to open a notebook of a different color?

Open a small yellow notebook

Graphics and Sound

You can perform various operations on graphics and sound cells which have been generated by the kernel or by importing them from another program.  You can move, rescale, and resize graphics, play sounds, extract coordinates from them, or animate a sequence of them.

To extract coordinates from the following graphic, command-click on the graphic, and then copy the coordinates and paste them anywhere you like.

Plot[Sin[x], {x, 0, 2  Pi}];

To animate a sequence of graphics or sounds, double-click on one of them.  Alternately, you can select disjoint graphics cells and then choose Cell ⊳ Animate Selected Graphics.

CellTags and Counters

Using CellTags

Mathematica gives us the ability to jump quickly to any cell in a notebook with the Find ⊳ Cell Tags menu item, which lists those cells that have been tagged or bookmarked by the user.

To add a tag to a cell, use the Find ⊳ Add/Remove Cell Tags menu item.  Any string can be used as a tag, and you can have multiple tags for the same cell or the same tag for multiple cells.

Once you have the tags set up the way you want them, the text of all the tags in your notebook will appear in the Find ⊳ Cell Tags submenu.  Selecting an item from this menu will highlight the cell bracket of all cells that are tagged with the string you selected.

Cell tags are used internally by the front end in other ways, such as in creating relative counters.

Automatic Numbering

New to 3.0 is the ability to create automatic reference numbers for figures, equations, or other objects.  By using Input ⊳ Create Automatic Numbering Object, you can insert an object that will display a count of how many cells of a particular style exist up to that point from the top of the notebook.  For example, this is the th text cell in this notebook.  That counter was created by choosing the specified menu item, and then picking Text in the resulting dialog.  The number will change automatically as text cells are added or deleted.

You can also create a truly live reference to a cell that updates whenever it needs to.  To do this, you must first add a cell tag to the cell that you wish to reference with the counter.  Then, in the Create Automatic Numbering Object dialog, specify the cell tag and the style for the counter.  Using this method, I can refer to the previous cell as text cell .

To create compound counters, use multiple counters separated by whatever character you like.  For instance, the <section>.<subsection> that this cell is in is §..

Exporting to .html / .gif / .tex / .eps / .m / .ma / … files

Save As Special and SaveSelectionAs

It is important to be able to share data with others in a variety of formats.  Mathematica supports exporting to many standard file formats, including HTML and TEX.

You can save a notebook as TEX or HTML by selecting File ⊳ Save As Special ⊳ TeX or HTML.  This is also where you would choose to save the notebook as a version 2.2 Mathematica notebook (.ma).  Notice that you can open 2.2 notebooks merely by choosing File ⊳ Open and selecting your conversion settings in the resulting dialog.

Other formats, including EPS and QuickTime, are available in the Edit ⊳ Save Selection As submenu.  But some image formats such as GIF and JPEG are missing.  Where are they?

Display and Export

Some more export options are available via the Display function in the kernel.  See the Mathematica book for more information on Display and the formats it supports.

In the next version, Import/Export will allow Mathematica to not only be able to write files such as GIF and JPEG, but also be able to read them, so you can import your favorite graphic files from the web into your new notebooks or packages.

What's the difference between a notebook (.nb) file and a package (.m) file?

What does File ⊳ Save As Special ⊳ Package Format do?  A package file is a .m file that is meant to be read into the kernel using Get or '<<'.  If you have a notebook that contains some cells that you want in a package file, you should first make those cells initialization cells by choosing it from the Cell ⊳ CellProperties submenu.  Now you have a couple choices:

1. Save the notebook using the usual File ⊳ Save or File ⊳ Save As.  When you do so, Mathematica will ask you if you want to also create a pacakge from the initialization cells.  If you say yes, a file ending with ".m" will be created alongside your notebook file.  The .m file contains all the code from the initialization cells in the notebook.  Whenever you update and save the notebook, this package file will be recreated and updated to match.

Note that if you said no in this dialog, you will never be asked again for that notebook.  If you want to be asked again, reset the notebook option AutoGeneratedPackage from False to Manual, or click on the '+' or '●' next to that option to remove its setting.

2. Save the notebook using File ⊳ Save As Special ⊳ Package Format or Edit ⊳ Save Selection As ⊳ Package Format.  This also saves all the initialization cells in a text format suitable for kernel evaluation, but is a one-time file save, as opposed to the continuous updating that is done in the first option above.

Cross-Platform Compatibility

Compatilibity Issues

In versions prior to 3.0, the front end looked very different depending on what platform you were using.  Starting with version 3.0, the front end's code base was centralized, so much of the inner workings of the front end are identical on all platforms.  That means not only that the front end's look and feel is mostly the same across all platforms, but also notebook files are completely portable.  These ASCII files can be transferred as text either through email or ftp.  Provided they have been transferred in ASCII mode, there are no end-of-line problems with transferring Mathematica notebooks between platforms.  You can easily create a notebook using the Windows front end, email it to yourself at the lab, and then open, edit, or print the notebook from the Linux front end.

Finding Help

Why the Beep?

When the front end or kernel encounters an error, or when you try to do something that can't be done, Mathematica will beep.  The reason for a particular beep can be determined by selecting Help ⊳ Why The Beep?  For instance, try to animate this cell.

When the beep is due to a kernel message being generated, that message is generally more informative than the message presented in the Why The Beep? dialog.

Online Books

The Mathematica Book and the Mathematica 3.0 Standard Add-On Packages guide are two books that can be found in the help browser.  Other books can be added to the browser by placing the appropriate files that make up the book into a special location in the Mathematica layout.  This will typically be done by running the book's installer, or following the instructions that came with the book.

For information on creating online books, contact Wolfram Research, Inc.

Master Index

Here you can find references to all sections of the help browser, so looking up graphics for example will find references in the function listing, and in both the books listed above, as well as any other books that have an index entry for that word.  Looking up function names or areas of functionality are good ways to find answers to specific programming questions.

Rebuilding the Help Index — How and Why

The front end stores or caches category and index information in its preferences files so that this information does not need to be regenerated every time you select something in the help browser.  When you place a new book in the Mathematica layout, you need to inform the front end that it should update its cached browser information.  You can do this by selecting Help ⊳ Rebuild the Help Index.  Even if you never place a book in the Mathematica layout, you may still need to use this menu item if you, for example, move the Mathematica installation or rename your hard drive.

Note that if you delete your preferences files, the front end will automatically rebuild the help index the first time you open the help browser thereafter.

Future Features

Colored Brackets

The new option UnmatchedBracketStyle determines whether unmatched brackets are displayed with a custom color, which reverts to the default color when the matching character or characters are typed.

Plot[Sin[x], {x, 0, 2  π

Hyphenation

The Hyphenation option specifies whether to hyphenate text.  Related options allow the user finer control over when to hyphenate, and what character to use when a word is broken between lines.

Spell Checker

A very useful new ability enables us to spell check notebook documents.  There are SpellingDictionaries and LanguageCategory options, which are used to specify the differences between typical input and exposition.

The Incredible Shrinking Blob™

In order to help the eye follow when Mathematica automatically line breaks input, the CursorTracker feature was added.  This displays a small oval very briefly, which is meant to focus the eye on the new location of the input cursor.  This can be turned off by setting ShowCursorTracker to False.

Inline Cells Revisited

In 3.0, you could use typeset cells inline in text cells.  In the next version, you will be able to use text cells inline in typeset cells, and graphics cells inline in both typeset and text cells.  This will allow for more natural and flexible layout of notebook elements.

Import/Export

Status unknown

Interactive Graphics

Status unknown

Advanced Topics

Tokens, Commands, and Packets

Tokens

There are many simple commands that you can give the front end.  They are in the form of a string or token.  Here is the syntax for sending these tokens.

FrontEndExecute[FrontEndToken["tokenString"]]

For tokens with a parameter, or tokens that apply to a particular notebook object, use this syntax

FrontEndExecute[FrontEndToken[nbObject, "tokenString", <"tokenParameter">]]

Sample tokens: New, Open, Undo, Fraction, Paste — After, CopySpecial — CellExpression.

Commands

Notebook programming commands give control for manipulating documents using a high level syntax.  These commands are typically sent to either kernel or the front end, depending on the complexity of the task at hand.

Sample commands: NotebookCreate, NotebookSave, SelectionMove, SetSelectedNotebook

Packets

In addition to tokens and commands, you can communicate directly with the front end by sending it MathLink packets.  FrontEndExecute and FrontEndToken are two such packets.  Most commands have tokens of the same name that you can use.  However, there are some packets that do not have associated commands, and so the only way to use those features is to send the packets directly.  Here is the syntax for sending one such packet from a kernel.

LinkWrite[$ParentLink, 
      FrontEnd`HelpBrowserLookup["RefGuide", "N"]]

Sample packets: FrontEnd`FileBrowse, FrontEnd`HelpBrowserLookup, FrontEnd`Value

Styles

The button styles that are available in the Input ⊳ Create Button submenu are defined in the current notebook's style sheet.  By viewing these styles, you can see how the functions are defined in terms of lower level front end commands.  You can even edit these styles, or create your own custom style that does a particular set of commands.  For example, you might want a button that pastes the selection into a new notebook window, and then evaluates it.

CellTags, the Help Browser, and You

Notebooks and Documentation

Once you have documentation for your package or the files from your book completed, you are ready to add them to the Mathematica help browser.  Doing this is as easy as creating the categories that you want to appear in the browser window, and telling the front end what portions of the notebook to display when someone clicks one of the categories.

For the following to work, all your notebooks and the BrowserCategories.m (and optionally the BrowserIndex.m) files should be placed in AddOns/Applications/yourAppPack/Documentation/English/.

CellTags

CellTags are used in conjunction with the help system to specify what cells should be displayed when an item is selected in the browser, and possibly where to scroll to in the resulting display.

BrowserCategories.m

In order to have your notebooks display in the help browser, they will need to have a BrowserCategories.m file to accompany them.  This is a text file containing a Mathematica expression that indicates what text will display in the list boxes within the browser window, and which notebooks or portions of notebooks are associated with each item.

For simple layouts, the expression in your BrowserCategories.m will have two types of exprssion heads: BrowserCategory and Item.  BrowserCategory takes three arguments

BrowserCategory["Category\!\(\*
StyleBox[\(\\ \),
"Text",
FontWeight->"Plain",
FontSlant->"Italic"]\)Name", None, list]

The third argument can be a list containing BrowserCategory expressions or Item expressions.  An Item which links to a specific notebook usually has the following syntax.

Item["item\!\(\*
StyleBox[\(\\ \),
"Text",
FontWeight->"Plain",
FontSlant->"Italic"]\)name", "file\!\(\*
StyleBox[\(\\ \),
"Text",
FontWeight->"Plain",
FontSlant->"Italic"]\)name", options]

You can also cause a horizonal line to appear in the listing, separating previous items from ones that follow, by using a delimiter item.

Item[Delimiter]

The most important Item option is CopyTag.  If CopyTag is set to a string str, then selecting that item will cause only those cells from the specified notebook that have str among their cell tags to display.  If CopyTag is set to None, then the entire notebook is displayed, regardless of tags.  When there is no CopyTag option given, its setting defaults to "item name".

BrowserIndex.nb

The BrowserIndex.nb file is what enables Mathematica to splice your index together with all the other indices, and present it as part of the master index in the help browser.  In order to do this, the BrowserIndex.nb contains cells that refer both to the notebook files and the BrowserCategories.m file.

For more information on the syntax used in BrowserIndex.nb files, contact Wolfram Research, Inc.

Menus Are Expressions Too

MenuSetup.tr

Format

Editing

Other Keyboard Shortcuts

KeyEventTranslations.tr

Format

Editing

This cell is tagged and used to get the final page count in the headers