2.9.11 Adjusting Details of FormattingMathematica 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.
| option | default value | | | ColumnAlignments | Center | how to align columns | | RowAlignments | Baseline | how to align rows | | ColumnSpacings | 0.8 | spacings between columns in ems | | RowSpacings | 1.0 | spacings between rows in x-heights | | ColumnsEqual | False | whether to make all columns equal width | | RowsEqual | False | whether to make all rows equal total height | | ColumnWidths | Automatic | the actual width of each column in ems | | RowMinHeight | 1 | the minimum total height in units of font size assigned to each row | | GridBaseline | Axis | with what part of the whole grid the baselines of boxes around it should be aligned | | ColumnLines | False | whether to draw lines between columns | | RowLines | False | whether to draw lines between rows | | GridDefaultElement | " " | what to insert when a new element is interactively created |
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=
|
|
| Center | centered (default) | | Left | left justified (aligned on left edge) | | Right | right justified (aligned on right edge) | | "." | aligned at decimal points | | "c" | aligned at the first occurrence of the specified character | { , , ... } | separate specifications for each column in the grid |
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 am . 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.
| Center | centered | | Top | tops aligned | | Bottom | bottoms aligned | | Baseline | baselines aligned (default) | | Axis | axes aligned | { , , ... } | separate specifications for each row in the grid |
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=
|
|
| option | default value | | | Background | GrayLevel[0.8] | button background color | | ButtonFrame | "Palette" | the type of frame for the button | | ButtonExpandable | True | whether a button should expand to fill a position in a GridBox | | ButtonMargins | 3 | the margin in printer's points around the contents of a button | | ButtonMinHeight | 1 | the minimum total height of a button in units of font size | | ButtonStyle | "Paste" | the style from which properties of the button not explicitly specified should be inherited |
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.
| printer's point | approximately 1/72 inch (or sometimes the size of a pixel on a display) | | pica | 12 printer's points, or 1/6 inch | | font point size | the maximum distance in printer's points between the top and bottom of any character in a particular font | | em | a width equal to the point font size--approximately the width of an "M" | | en | half an em | | x-height | the height of an "x" character in the current font |
Units of distance.
| full name | alias | | | \[InvisibleSpace] | is | zero-width space | | \[VeryThinSpace] |   | 1/18 em (x x) | | \[ThinSpace] |    | 3/18 em (x x) | | \[MediumSpace] |     | 4/18 em (x x) | | \[ThickSpace] |      | 5/18 em (x x) | | \[NegativeVeryThinSpace] | - | em (x x) | | \[NegativeThinSpace] | -  | em (x x) | | \[NegativeMediumSpace] | -   | em (x x) | | \[NegativeThickSpace] | -    | em (x x) | | \[RawSpace] | | keyboard space character | | \[SpaceIndicator] | space | the character indicating a space |
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=
|
|
StyleBox[boxes, AutoSpacing->False]
| | leave the same space around every character in boxes |
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    ,    ,    ,    "nudge" whatever you have selected by one pixel at your current screen magnification. Such adjustments are represented within Mathematica using AdjustmentBox objects.
AdjustmentBox[box, BoxMargins->{{left, right}, {bottom, top}}]
| | draw margins of the specified widths around box |
AdjustmentBox[box, BoxBaselineShift->up]
| | shift the height at which baselines of boxes around box should be aligned |
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.
StyleBox[boxes, ShowContents->False]
| | leave space for boxes but do not display them |
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=
|
|
| option | default value | | | SpanMinSize | Automatic | minimum size of expandable characters in units of font size | | SpanMaxSize | Automatic | maximum size of expandable characters in units of font size | | SpanSymmetric | True | whether vertically expandable characters should be symmetric about the axis of the box they are in | | SpanLineThickness | Automatic | thickness in printer's points of fraction lines etc. |
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.
parentheses, arrows, bracketing bars
| | grow without bound | | brackets, braces, slash | grow to limited size |
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=
|
|
| option | default value | | | ScriptSizeMultipliers | 0.71 | how much smaller to make each level of subscripts, etc. | | ScriptMinSize | 4 | the minimum point size to use for subscripts, etc. | | ScriptBaselineShifts | {Automatic, Automatic} | the distance in x-heights to shift subscripts and superscripts |
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=
|
|
| option | default value | | | LimitsPositioning | Automatic | whether to change positioning in the way conventional for limits |
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. | |
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.
| option | default value | | | MultilineFunction | Automatic | what to do when a box breaks across several lines |
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.
| full name | alias | | | \[NoBreak] | nb | inhibit a line break | | \[NonBreakingSpace] | nbs | insert a space, inhibiting a line break on either side of it | | \[NewLine] | | insert a line break, setting the indenting level at the time the new line is started | | \[IndentingNewLine] | nl | insert a line break, always maintaining the correct indenting level |
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. | |
Out[44]=
|
|
| But here the break is at a much deeper level. | |
In[45]:=
Nest[List, x+y, 30]
|
Out[45]=
|
|
|