This notebook is part of a draft of a set of elementary Mathematica tutorials, as presented at the 1999 Mathematica Developer Conference, October 20-23, 1999. This draft is provided here to encourage suggestions about these tutorials. You can send suggestions about these tutorials to Wolfram Research Technical Support at the address support@wolfram.com.

Only the first nine parts of the Basic Tutorial are included in this draft.

Mathematica  Introductory Tutorial (Part 6 of 10)

6 Typeset Editing

In this part of the Basic Tutorial you will learn about:

• Typesetting input and output
• Entering typset input: palettes
• Entering typset input: keyboard
• Creating palettes
• Underlying cell and box structures
• Including typeset expressions in graphics

Typesetting input and output

The Mathematica typeset editor is both an editor for typeset documents and an interface to the Mathematica kernel. You can use the typesetting capabilities of Mathematica to prepare typeset documents and to enter input and display output using the same notations that you might find in technical publications.

By default, Mathematica uses typeset formatting for all output. For example, if you enter and evaluate Sqrt[3], the result will be formatted using the (Sqrt[□]) notation of traditional mathematics.

Sqrt[3]

You can also enter your input using typeset notations. For example, you could enter (Sqrt[3]) rather than Sqrt[3]. One way to enter (Sqrt[3]) is to press Ctrl[@] (press the @ key while holding down the control key) and then type 3. You can then evaluate that input just as you would evaluate any other input.

(Sqrt[3])

Mathematica can also display a variety of characters commonly used in mathematics and engineering, such as greek characters, which you can use in your input.  Just as with built-in functions, some characters like π have special meaning when used in input.

Sin[θ + 2  π]

Entering typeset input: palettes

For every typesetting construct available in Mathematica, there are a variety of ways to create it in your notebook.  You can use menus to enter typeset expressions, but you will usually use the keyboard or palettes instead.  Palette input is discussed here; keyboard input is discussed in the next section.

When Mathematica first launches, it opens a palette window displaying commonly used typesetting forms and characters called the BasicInput palette.  If it's not already open, you can open this palette by selecting BasicInput from the Palettes submenu of the File menu.

Palette windows contain buttons which perform some action when you click them. Clicking on the buttons in the BasicInput palette causes the typeset form to paste into your notebook at the location of your cursor.  For example, if you click the sqare root button in that palette, (Sqrt[■]), you will get a square root symbol in your notebook.

(Sqrt[\[Placeholder]])

The □ and ■ squares in this palette designate places where you can insert your own data.  The outlined square, □, is called a placeholder.  Use the tab key to move from one placeholder to the next in a typeset expression. Typing when a placeholder is selected will replace the placeholder with what you type.

The black square, ■, is called a selection placeholder.  If you have some input selected when you click a button, your selected input will be filled in where the selection placeholder is.  This lets you apply typesetting to part or all of your existing input. If there is no selection placeholder, your data will be replaced by the button's data.

For example, you can click the fraction button,

(\[Placeholder]/\[Placeholder])

fill in the placeholders,

(1/Sin)[θ]

select the fraction, and click the square root button to create something like this.

(Sqrt[(1/Sin)[θ]])

For more information on using palettes, see the Getting Started document for your plaform: Windows, Macintosh, or X.

The BasicInput palette provides you with the most commonly used typeset forms: superscripts, subscripts, fractions, roots, matrices, and common characters.  Other built-in palettes (BasicTypesetting, InternationalCharacters, CompleteCharacters), give more extensive libraries of preformatted typeset forms.  In the next section, we look at how to create arbitrary typeset forms from the keyboard.

Entering typeset input: keyboard

For most typeset forms and special characters, we've just seen how to use palettes to obtain them in your notebook. If you use the same forms over and over, it may be more efficient to use the keyboard instead of clicking on a palette. There are keyboard shortcuts for all of the common typesetting forms, and all special characters can be entered from the keyboard as well.

Keyboard shortcuts for typesetting are almost always formed by holding down the Ctrl key and pressing one other key.  For example, to create a square root (or radical), you can type Ctrl[@] or Ctrl[2].  Just as with clicking the palette button, this will provide a placeholder into which to type your data.

The keyboard shortcuts for some typeset forms are easy to remember.  For example, fractions are typed using Ctrl[/] (instead of ⇧[/] for the one dimensional version), and superscripts are typed using Ctrl[6] or Ctrl[^] (instead of just ^, which is typically entered as ⇧[6]).  Here is a grid of the most common typeset forms and their keyboard equivalents.

({{Radical, (Sqrt[□]), \(Ctrl[2]\ or\ Ctrl[@]\)}, {Fraction, (□/□), \(Ctrl[/]\)}, {Superscript, (□^□), \(Ctrl[6]\ or\ Ctrl[^]\)}, {Subscript, (Subscript[□, □]), \(\(\ \)\(Ctrl[ - ]\)\)}, {Above, (Overscript[□, □]), Ctrl[7]}, {Below, (Underscript[□, □]), \(Ctrl[ + ]\)}, {Add Row, (({{□}, {□}})), Ctrl⁃⏎}, {Add Column, (({{□, □}})), Ctrl[, ]}, {Opposite Position, \(\@□\%□, □\_□\%□, □\+□\%□\), Ctrl[5]}})

For more assistance with keyboard shortcuts for typeset expressions, see the help files for the Expression Input submenu of the Edit menu, or the 2D Expression Input information.

To type a special character such as π into you notebook, you usually will type the two characters '\[' followed by the character's name, and then a closing ']'.  As you type the closing bracket, the string of characters you typed will be automatically replaced by the corresponding special character.  You can see all the special characters and their names in The Mathematica Book.  For example, typing \ [Pi] results in a π character, while \ [GothicCapitalZ] yields ℨ.

Many special characters have aliases you can use to type them more quickly, but they are a little more difficult to remember.  To type a character using its alias, press Esc (the escape key), type the alias, and then press Esc again to complete the character.  For example, π can also be typed as EscpEsc, and ℨ as EscgoZEsc.  Again, see The Mathematica Book to learn character aliases.  If you are familiar with the names given to these characters in HTML, TEX, or SGML, you may find that the name is an alias to the character in Mathematica.

Creating palettes

You may find it useful to create palettes that contain typeset forms you use most often.  In order to do this you should first create an array of empty buttons. Select Create Table/Matrix/Palette in the Input menu.  Click the radio button next to Palette, and choose the dimensions for your array of buttons.  Then click OK; the array will paste into your notebook.

({{\[Placeholder], \[Placeholder], \[Placeholder]}, {\[Placeholder], \[Placeholder], \[Placeholder]}})

Now you should fill in the buttons with the typeset forms you wish to have on your palette.  If you want to include places to fill in your data, or places where data will be inserted automatically, use □ or ■ respectively as discussed in a previous section.  You can type these characters using their long names—\ [Placeholder] and \ [SelectionPlaceholder]—or their aliases— EscplEsc and EscsplEsc.

({{(\[SelectionPlaceholder]/\[Placeholder]), ((((\[SelectionPlaceholder]/\[Placeholder])))^3), ((((\[SelectionPlaceholder]/\[Placeholder])))^5)}, {(Sqrt[(\[SelectionPlaceholder]/\[Placeholder])]), (((\[SelectionPlaceholder]/\[Placeholder]))^(1/3)), (((\[SelectionPlaceholder]/\[Placeholder]))^(1/5))}})

To put this array of buttons into its own palette window, select the cell and choose Generate Palette from Selection from the File menu.  Clicking the buttons in the palette will not work. To save the palette, click the close box and then save it as you would any other notebook.

Underlying cell and box structures

Mathematica saves all the information in a notebook as plain text. Just as every special character has a textual form (\ [name]), every typeset structure also has a textual form.  In fact, everything you see in the notebook window is stored as text in the notebook file. This text is in the format of a Mathematica expression.  A notebook is really a Mathematica expression with head Notebook, and each cell is a subexpression with head Cell; the notebook interface formats each cell according to its corresponding expression.

If you want to know how to type a particular special character used in some cell, or what typesetting structure is used in a cell, or why a cell doesn't format the way you think it should, it may help to be able to see the underlying Cell expression.  You can toggle between the formatted version of the cell and the raw Cell expression by selecting the cell and choosing Show Expression from the Format menu.  (This is analogous to using FullForm to analyse output.)  For instance, using this technique on the following cell, you can discover how to type the character θ, and what typesetting structures correspond to a fraction and a radical.

(Sqrt[(1/Sin)[θ]])

You can also edit the Cell expression. When you toggle back to the formatted version, your changes will be reflected in the display.  For example, select the above cell and choose 14 point from the Size submenu of the Format menu. This changes the display and so it must have changed something about the Cell expression.  Using Show Expression, you see that selecting a new point size simply adds an option to the cell, namely FontSize->14.  You can change this number manually and toggle back to the formatted cell to view your changes.

Within a cell, the subexpression representing a piece of typesetting has a name that usually ends in "Box", so they are sometimes collective known as typesetting boxes.  Typesetting boxes have options too.  For example, if you look at the cell expression underlying a two-dimensional table such as

({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})

you will see this is stored as

GridBox[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]

Some options for GridBox control whether to draw a frame around the outside of the grid (GridFrame), whether to draw lines between the rows (RowLines) or columns (ColumnLines), and how to align the content of each column (ColumnAlignments).  You can alter these options by interacting with the option inspector, or you can directly alter the cell expression to contain

GridBox[{{1, 2, 3}, {4, 5, 6}, {7, 8, 
          9}}, 
GridFrame -> 2, RowLines -> 1]

The display should change accordingly.

You can browse the options available in the various typesetting boxes in the option inspector documentation.

Exercises

Entering a typeset expression

Enter the equation z == Sqrt[x^2 + y^2] using (Sqrt[□]) notation for the square root and superscripts for the exponents.  The result should look like z == (Sqrt[(x^2) + (y^2)]).

Editing a typeset expression

Enter an expression like the one below except with the numerator and the denominator exchanged.  You can enter the new expression either by entering the entire expression or by constructing a template for the new expression and copying the numerator and denominator from this expression into that template.

(Sqrt[((θ + (Sqrt[1 - 2  β]))/(θ + (Sqrt[1 + β])))])

Adding entries to a matrix

Add one row and one column to each matrix in this matrix dot product.  Use 1 for the diagonal elements in both matrices, use x for the off-diagonal elements in the first matrix, and use β for the off-diagonal elements in the second matrix.  Evaluate the matrix dot product and display the result in MatrixForm.

(({{1, x}, {x, 1}})).(({{1, β}, {β, 1}}))

Formatting numbers

Correct this input so that all of the numbers are displayed with an explicit exponent (this can be done by setting the ExponentFunction  option to Identity), and add a second argument to NumberForm so that the numbers are displayed with no more than two digits to the right of the decimal point.

\(NumberForm[
      ColumnForm[{23.72*^-8, 3407. , \(-970.6\), \(-0.00135\), 
          193.9}], 
NumberFormat -> \((#1 <> "e" <> #3 &)\)]\)

Formatting a table

Use TableForm and PaddedForm to format the following data in a table so that the numbers in each column are aligned on their decimal points.

data = {{230. , 0. , 17.4}, {17. , 1.3, 2.9}, {0.031, 0.071, 
          8.01}, {1.58, 1.1, 0.099}}

The resulting display should be similar to this.

Then, create a GridBox expression with the option ColumnAlignments->"." as another way to do accomplish the alignment.  Use DisplayForm to display the grid.

Editing a box expression

Make a copy of the table that is displayed by this input, open the underlying Cell expression, and add options in the GridBox expression to draw lines between the rows and the columns of the table and to draw a frame around the outside of the table.

TableForm[{{3.59*^-6, 43.2, 109}, {4.61*^-6, 108.0, 112}, {4.91*^-6, 
          297.5, 117}, {5.13*^-6, 732.1, 128}, {6.33*^-6, 2110.0, 
          115}}, 
TableAlignments -> Center, 
      TableHeadings -> {Automatic, {"x", "y", "z"}}]

Including typeset output in a plot

Up until now, we have only used StandardForm for typesetting.  By default, the Mathematica notebook interface displays all your input and output in StandardForm.  This formats some expressions, like square roots, as you would see them in a mathematics text book.  However, it does not follow other conventions from math texts such as using parenthesis for function application or italicizing single-letter variable names.

There is another typesetting format, TraditionalForm, that does these things.  It also uses a different font than StandardForm, and contains special formatting rules for a greater number of Mathematica functions.  Because parentheses sometimes mean function application and sometimes mean multiplication in TraditionalForm, care must be taken when creating TraditionalForm input.  Convert the following StandardForm input cell to TraditionalForm by selecting the cell and choosing TraditionalForm from the Convert To submenu of the Cell menu.

Limit[BesselJ[0, x] + (Sqrt[x]), x -> 10]

Finally, modify the following input so that the label is (Subscript[J, 0])(x) + (Sqrt[x])and change the coordinates of the label so that it does not fall on top of the line in the plot.

Plot[
      BesselJ[0, x] + Sqrt[x], {x, 0, 10}, 
Epilog -> 
        Text[StyleForm[BesselJ[0, x] + Sqrt[x], FontSize -> 18], {4, 2}, 
          FormatType -> StandardForm]]

Typesetting within text

We've seen how to use typesetting in input, but you can also use typesetting within text like this paragraph.  When you do this, you create an inline cell which contains your typesetting.  By default, Mathematica uses TraditionalForm formatting rules for inline cells.  You can create an inline cell while typing in a text cell by pressing Ctrl[(] or Ctrl[9].  When you're finished with your inline cell, press Ctrl[)] or Ctrl[0] to continue typing text.  While you're inside the inline cell, there will be a faint background color indicating the extent of the inline cell.

Use the keyboard and palettes to type a copy of the following cell.

The divisor function (Subscript[σ, k])(n) is the sum of the (k^th) powers of the divisors of n.  The function (Subscript[σ, 0])(n) gives the total number of divisors of n, and if often denoted d(n).  The function (Subscript[σ, 1])(n), equal to the sum of the divisors of n, is often denoted σ(n).

Creating a button

Convert the contents of this cell into a button such that, when the button is pressed, the contents of the button will be pasted at the current position. Use a paste button from the Create Button submenu of the Input menu.

({{x = r Sin[θ] Cos[ϕ]}, {y = r Sin[θ] Cos[ϕ]}, {z = r Cos[θ]}})

Indenting paragraphs

The ParagraphIndent option specifies where the first line of a paragraph should begin.  Setting this to a positive value will indent the first line by that many points.  Use the Option Inspector and the ParagraphIndent option to indent the first line of this cell (the cell that you are reading) by 24 points.

Setting ParagraphIndent to a negative value will cause a hanging indent, so that the first line extends further to the left than the remaining lines.  Use the ShowExpression menu item in the Format menu and the ParagraphIndent option to format this cell (the cell that you are reading) with a hanging indent of 24 points.  This is a common format for references or bulletted or numbered lists.