This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Textual Input and Output /

2.9.11 Adjusting Details of Formatting

Mathematica provides a large number of options for adjusting the details of how expressions are formatted. In most cases, the default settings for these options will be quite adequate. But sometimes special features in the expressions you are dealing with may require you to change the options.

Options to GridBox.

This sets up an array of numbers.

In[1]:= t = Table[{i, (2i)!, (3i)!}, {i, 4}] ;

Here is how the array is displayed with the default settings for all GridBox options.

In[2]:= GridBox[t] // DisplayForm

Out[2]//DisplayForm=

This right justifies all the columns.

In[3]:= GridBox[t, ColumnAlignments->Right] // DisplayForm

Out[3]//DisplayForm=

This left justifies the first two columns and right justifies the last one.

In[4]:= GridBox[t,
ColumnAlignments->{Left, Left, Right}] // DisplayForm

Out[4]//DisplayForm=

This sets the gutters between columns.

In[5]:= GridBox[t, ColumnSpacings->{5, 10}] // DisplayForm

Out[5]//DisplayForm=

This forces all columns to be the same width.

In[6]:= GridBox[t, ColumnsEqual->True] // DisplayForm

Out[6]//DisplayForm=

Usually a GridBox leaves room for any character in the current font to appear in each row. But with RowMinHeight->0 it packs rows in more tightly.

In[7]:= {GridBox[{{x, x}, {x, x}}],
GridBox[{{x, x}, {x, x}}, RowMinHeight->0]} // DisplayForm

Out[7]//DisplayForm=

Settings for the ColumnAlignments option.

In formatting complicated tables, it is often important to be able to control in detail the alignment of table entries. By setting ColumnAlignments->"c" you tell Mathematica to arrange the elements in each column so that the first occurrence of the character "c" in each entry is aligned.

Choosing ColumnAlignments->"." will therefore align numbers according to the positions of their decimal points. Mathematica also provides a special \[AlignmentMarker] character, which can be entered as AliasIndicatoramAliasIndicator. This character does not display explicitly, but can be inserted in entries in a table to mark which point in these entries should be lined up.

Settings for the RowAlignments option.

This is the default alignment of elements in a row of a GridBox.

In[8]:= GridBox[{{SuperscriptBox[x, 2], FractionBox[y, z]}}] //
DisplayForm

Out[8]//DisplayForm=

Here is what happens if the bottom of each element is aligned.

In[9]:= GridBox[{{SuperscriptBox[x, 2], FractionBox[y, z]}},
RowAlignments->Bottom] // DisplayForm

Out[9]//DisplayForm=

In a piece of ordinary text, successive characters are normally positioned so that their baselines are aligned. For many characters, such as m and x, the baseline coincides with the bottom of the character. But in general the baseline is the bottom of the main part of the character, and for example, in most fonts g and y have "descenders" that extend below the baseline.

This shows the alignment of characters with the default setting RowAlignments->Baseline.

In[10]:= GridBox[{{"x", "m", "g", "y"}}] // DisplayForm

Out[10]//DisplayForm=

This is what happens if instead the bottom of each character is aligned.

In[11]:= GridBox[{{"x", "m", "g", "y"}}, RowAlignments->Bottom] //
DisplayForm

Out[11]//DisplayForm=

Like characters in ordinary text, Mathematica will normally position sequences of boxes so that their baselines are aligned. For many kinds of boxes the baseline is simply taken to be the baseline of the main element of the box. Thus, for example, the baseline of a SuperScript box is taken to be the baseline of x.

For a FractionBox , the fraction bar defines the axis of the box. In text in a particular font, one can also define an axis—a line going through the centers of symmetrical characters such as + and (. The baseline for a FractionBox is then taken to be the same distance below its axis as the baseline for text in the current font is below its axis.

For a GridBox, you can use the option GridBaseline to specify where the baseline should be taken to lie. The possible settings are the same as the ones for RowAlignments. The default is Axis, which makes the center of the GridBox be aligned with the axis of text around it.

The GridBaseline option specifies where the baseline of the GridBox should be assumed to be.

In[12]:= {GridBox[{{x,x},{x,x}}, GridBaseline->Top],
GridBox[{{x,x},{x,x}}, GridBaseline->Bottom]} //
DisplayForm

Out[12]//DisplayForm=

Formatting options for ButtonBox objects.

This makes a button that looks like an element of a dialog box.

In[13]:= ButtonBox["abcd",
ButtonFrame->"DialogBox"] // DisplayForm

Out[13]//DisplayForm=

Palettes are typically constructed using grids of ButtonBox objects with zero row and column spacing.

In[14]:= GridBox[{{ButtonBox["abc"], ButtonBox["xyz"]}},
ColumnSpacings->0] // DisplayForm

Out[14]//DisplayForm=

Buttons usually expand to be aligned in a GridBox.

In[15]:= GridBox[{{ButtonBox["abcd"]},
{ButtonBox["x"]}}] // DisplayForm

Out[15]//DisplayForm=

Here the lower button is made not to expand.

In[16]:= GridBox[{{ButtonBox["abcd"]}, {ButtonBox["x",
ButtonExpandable->False]}}] // DisplayForm

Out[16]//DisplayForm=

Section 2.11.6 will discuss how to set up actions for ButtonBox objects.

Units of distance.

Spacing characters of various widths. indicates the space key on your keyboard.

When you enter input such as x+y, Mathematica will automatically convert this to RowBox[{"x","+","y"}]. When the RowBox is output, Mathematica will then try to insert appropriate space between each element. Typically, it will put more space around characters such as + that are usually used as operators, and less space around characters such as x that are not. You can however always modify spacing by inserting explicit spacing characters. Positive spacing characters will move successive elements further apart, while negative ones will bring them closer together.

Mathematica by default leaves more space around characters such as + and - that are usually used as operators.

In[17]:= RowBox[{"a", "b", "+", "c", "-", "+"}] // DisplayForm

Out[17]//DisplayForm=

You can explicitly insert positive and negative spacing characters to change spacing.

In[18]:= RowBox[{"a", "\[ThickSpace]", "b", "+",
"\[NegativeMediumSpace]", "c", "-", "+"}] // DisplayForm

Out[18]//DisplayForm=

Inhibiting automatic spacing in Mathematica.

This makes Mathematica leave the same space between successive characters.

In[19]:= StyleBox[RowBox[{"a", "b", "+", "c", "-", "+"}],
AutoSpacing->False] // DisplayForm

Out[19]//DisplayForm=

When you have an expression displayed on the screen, the notebook front end allows you interactively to make detailed adjustments to the positions of elements. Typically ControlKeyLeftModifiedRightModified, ControlKeyLeftModifiedRightModified, ControlKeyLeftModifiedRightModified, ControlKeyLeftModifiedRightModified "nudge" whatever you have selected by one pixel at your current screen magnification. Such adjustments are represented within Mathematica using AdjustmentBox objects.

Adjusting the position of a box.

This adds space to the left of the B and removes space to its right.

In[20]:= RowBox[{"A", AdjustmentBox["B", BoxMargins->
{{1, -0.3}, {0, 0}}], "C", "D"}] // DisplayForm

Out[20]//DisplayForm=

By careful adjustment, you can set things up to put two characters on top of each other.

In[21]:= RowBox[{"C", AdjustmentBox["/",
BoxMargins->{{-.8, .8}, {0, 0}}]}] // DisplayForm

Out[21]//DisplayForm=

The left and right margins in an AdjustmentBox are given in ems; the bottom and top ones in x-heights. By giving positive values for margins you can force there to be space around a box. By giving negative values you can effectively trim space away, and force other boxes to be closer. Note that in a RowBox, vertical alignment is determined by the position of the baseline; in a FractionBox or an OverscriptBox, for example, it is instead determined by top and bottom margins.

Leaving space for boxes without displaying them.

If you are trying to line up different elements of your output, you can use ShowContents->False in StyleBox to leave space for boxes without actually displaying them.

This leaves space for the Y, but does not display it.

In[22]:= RowBox[{"X", StyleBox["Y", ShowContents->False], "Z"}] //
DisplayForm

Out[22]//DisplayForm=

The sizes of most characters are determined solely by what font they are in, as specified for example by the FontSize option in StyleBox. But there are some special expandable characters whose size can change even within a particular font. Examples are parentheses, which by default are taken to expand so as to span any expression they contain.

Parentheses by default expand to span whatever expressions they contain.

In[23]:= {RowBox[{"(", "X", ")"}],
RowBox[{"(", FractionBox["X", "Y"], ")"}]} // DisplayForm

Out[23]//DisplayForm=

StyleBox options for controlling expandable characters.

Parentheses within a single RowBox by default grow to span whatever other objects appear in the RowBox.

In[24]:= RowBox[{"(", "(", GridBox[{{X},{Y},{Z}}]}] // DisplayForm

Out[24]//DisplayForm=

Some expandable characters, however, grow by default only to a limited extent.

In[25]:= RowBox[{"{", "[", "(",
GridBox[{{X},{Y},{Z}}]}] // DisplayForm

Out[25]//DisplayForm=

This specifies that all characters inside the StyleBox should be allowed to grow as large as they need.

In[26]:= StyleBox[%, SpanMaxSize->Infinity] // DisplayForm

Out[26]//DisplayForm=

By default, expandable characters grow symmetrically.

In[27]:= RowBox[{"(", GridBox[{{X},{Y}},
GridBaseline->Bottom], ")"}] // DisplayForm

Out[27]//DisplayForm=

Setting SpanSymmetric->False allows expandable characters to grow asymmetrically.

In[28]:= {X, StyleBox[%, SpanSymmetric->False]} // DisplayForm

Out[28]//DisplayForm=

The notebook front end typically provides a Spanning Characters menu which allows you to change the spanning characteristics of all characters within your current selection.

Default characteristics of expandable characters.

The top bracket by default grows to span the OverscriptBox.

In[29]:= OverscriptBox["xxxxxx", "\[OverBracket]"] // DisplayForm

Out[29]//DisplayForm=

The right arrow by default grows horizontally to span the column it is in.

In[30]:= GridBox[{{"a", "xxxxxxx", "b"},
{"a", "\[RightArrow]", "b"}}] // DisplayForm

Out[30]//DisplayForm=

The up arrow similarly grows vertically to span the row it is in.

In[31]:= GridBox[{{FractionBox[X, Y],
"\[UpArrow]"}}] // DisplayForm

Out[31]//DisplayForm=

StyleBox options for controlling the size and positioning of subscripts, etc.

This sets up a collection of nested SuperscriptBox objects.

In[32]:= b = ToBoxes[X^X^X^X^X]

Out[32]=

By default, successive superscripts get progressively smaller.

In[33]:= b // DisplayForm

Out[33]//DisplayForm=

This tells Mathematica to make all levels of superscripts the same size.

In[34]:= StyleBox[b, ScriptSizeMultipliers->1] // DisplayForm

Out[34]//DisplayForm=

Here successive levels of superscripts are smaller, but only down to 5-point size.

In[35]:= StyleBox[b, ScriptMinSize->5] // DisplayForm

Out[35]//DisplayForm=

Mathematica will usually optimize the position of subscripts and superscripts in a way that depends on their environment. If you want to line up several different subscripts or superscripts you therefore typically have to use the option ScriptBaselineShifts to specify an explicit distance to shift each one.

The second subscript is by default shifted down slightly more than the first.

In[36]:= RowBox[{SubscriptBox["x", "0"], "+",
SubsuperscriptBox["x", "0", "2"]}] // DisplayForm

Out[36]//DisplayForm=

This tells Mathematica to apply exactly the same shift to both subscripts.

In[37]:= StyleBox[%, ScriptBaselineShifts->{1, Automatic}] //
DisplayForm

Out[37]//DisplayForm=

An option to UnderoverscriptBox and related boxes.

The limits of a sum are usually displayed as underscripts and overscripts.

In[38]:= Sum[f[i], {i, 0, n}]

Out[38]=

When the sum is shown smaller, however, it is conventional for the limits to be displayed as subscripts and superscripts.

In[39]:= 1/%

Out[39]=

Here low and high still display directly above and below XX.

In[40]:= UnderoverscriptBox["XX", "low", "high",
LimitsPositioning->True] // DisplayForm

Out[40]//DisplayForm=

But now low and high are moved to subscript and superscript positions.

In[41]:= FractionBox["a", %] // DisplayForm

Out[41]//DisplayForm=

LimitsPositioning->Automatic will act as if LimitsPositioning->True when the first argument of the box is an object such as \[Sum] or \[Product]. You can specify the list of such characters by setting the option LimitsPositioningTokens.

Line breaking option for boxes.

When you are dealing with long expressions it is inevitable that they will continue beyond the length of a single line. Many kinds of boxes change their display characteristics when they break across several lines.

This displays as a built-up fraction on a single line.

In[42]:= Expand[(1 + x)^5]/Expand[(1 + y)^5]

Out[42]=

This breaks across several lines.

In[43]:= Expand[(1 + x)^10]/Expand[(1 + y)^5]

Out[43]=

You can use the option MultilineFunction to specify how a particular box should be displayed if it breaks across several lines. The setting MultilineFunction->None prevents the box from breaking at all.

You can to some extent control where expressions break across lines by inserting \[NoBreak] and \[NonBreakingSpace] characters. Mathematica will try to avoid ever breaking an expression at the position of such characters.

You can force Mathematica to break a line by explicitly inserting a \[NewLine] character, obtained in the standard notebook front end simply by typing Return. With default settings for options, Mathematica will automatically indent the next line after you type a Return. However, the level of indenting used will be fixed as soon as the line is started, and will not change when you edit around it. By inserting an \[IndentingNewLine] character, you can tell Mathematica always to maintain the correct level of indenting based on the actual environment in which a line occurs.

Characters for controlling line breaking.

When Mathematica breaks an expression across several lines, it indents intermediate lines by an amount proportional to the nesting level in the expression at which the break occurred.

The line breaks here occur only at level 1.

In[44]:= Range[30]

Out[44]=

But here the break is at a much deeper level.

In[45]:= Nest[List, x+y, 30]

Out[45]=