Tables and Matrices

Formatting lists as tables and matrices.

Here is a list:

Table[(i+45)^j,{i,3},{j,3}]

(RefLink[Grid, paclet:ref/Grid]) gives the list typeset in a tabular format:

Grid[%]

(RefLink[TableForm, paclet:ref/TableForm]) displays the list in a tabular format:

TableForm[%%]

(RefLink[Grid, paclet:ref/Grid]) and (RefLink[Column, paclet:ref/Column]) are wrappers which do not evaluate, but typeset their contents into appropriate forms. They are typesetting constructs and require a front end to render correctly.

(RefLink[Column, paclet:ref/Column]) is a shorthand for a (RefLink[Grid, paclet:ref/Grid]) with one column:

Column[Range[5]]

The (RefLink[FullForm, paclet:ref/FullForm]) of a (RefLink[Grid, paclet:ref/Grid]) or (RefLink[Column, paclet:ref/Column]) demonstrates that the head is inert:

FullForm[%]

All of these wrappers can be used to present any kind of data, including graphical data:

Grid[{{"disk",Graphics[Disk[],ImageSize->25]}, {"square",Graphics[Rectangle[],ImageSize->25]}}]

Printing tables of numbers.

Here is a list of numbers:

fac={10!,15!,20!}

(RefLink[Column, paclet:ref/Column]) displays the list in a column:

Column[fac]

This aligns the numbers by padding each one to leave room for up to 20 digits:

PaddedForm[Column[fac],20]

In this particular case, you could also align the numbers using the (RefLink[Alignment, paclet:ref/Alignment]) option:

Column[fac,Alignment->{Right}]

This lines up the numbers, padding each one to have room for 8 digits, with 5 digits to the right of the decimal point:

PaddedForm[Column[{6.7,6.888,6.99999}],{8,5}]

Symbols used to represent spanning in (RefLink[Grid, paclet:ref/Grid]).

(RefLink[Grid, paclet:ref/Grid]) takes a rectangular matrix as its first argument. Individual elements of the (RefLink[Grid, paclet:ref/Grid]) can span across multiple rows, columns, or a rectangular subgrid by specifying the areas to be spanned. The spanning element is always specified in the upper left-hand corner of the spanning area, and the remaining area is filled in with the appropriate spanning symbols.

This shows a spanning row, where the spanning portion is filled in using (RefLink[SpanFromLeft, paclet:ref/SpanFromLeft]):

Grid[{{"t", SpanFromLeft, SpanFromLeft, SpanFromLeft}, {"a", "b", "c", "d"}}]

Similarly, a column can be spanned using (RefLink[SpanFromAbove, paclet:ref/SpanFromAbove]):

Grid[{{"t", "a"}, {SpanFromAbove, "b"}}]

When specifying a rectangular spanning area, (RefLink[SpanFromBoth, paclet:ref/SpanFromBoth]) is used in every element which is both below and to the right of the spanning element:

Grid[{{"t", SpanFromLeft,"a"}, {SpanFromAbove, SpanFromBoth,"b"},{"c","d","e"}}]

Some options which affect the behavior of a (RefLink[Grid, paclet:ref/Grid]) as a whole.

The (RefLink[Frame, paclet:ref/Frame]) option can specify a frame around the entire (RefLink[Grid, paclet:ref/Grid]):

Grid[{{"a", "b"}, {"c", "d"}}, Frame->True]

This uses (RefLink[FrameStyle, paclet:ref/FrameStyle]) to change the appearance of a frame:

Grid[{{"a", "b"}, {"c", "d"}},Frame->True,FrameStyle->{Brown,AbsoluteThickness[5]}]

This uses (RefLink[Background, paclet:ref/Background]) to specify a background color for the entire (RefLink[Grid, paclet:ref/Grid]):

Grid[{{"a", "b"}, {"c", "d"}},Background->Pink,Frame->True]

The position of a (RefLink[Grid, paclet:ref/Grid]) relative to its surroundings can be controlled using the (RefLink[BaselinePosition, paclet:ref/BaselinePosition]) option:

Row[{"A matrix:", Grid[{{1, 2}, {3, 4}}, BaselinePosition->Top]}]

This aligns the bottom of the grid with the baseline:

Row[{"A matrix:", Grid[{{1, 2}, {3, 4}}, BaselinePosition->Bottom]}]

This sets the base style of the entire (RefLink[Grid, paclet:ref/Grid]) to be the Subsection style:

Grid[{{"a", "bit"}, {"of", "text"}},BaseStyle->{"Subsection"}]

(RefLink[Column, paclet:ref/Column]) is a shorthand for specifying a (RefLink[Grid, paclet:ref/Grid]) with one column. Since the two functions are similar, the same options can be used for each one.

This sets some (RefLink[Grid, paclet:ref/Grid]) options for (RefLink[Column, paclet:ref/Column]):

Column[{1,2,3,4},Background->Pink,Frame->True]

Some options which affect the columns and rows of a (RefLink[Grid, paclet:ref/Grid]).

The options for (RefLink[Grid, paclet:ref/Grid]) which affect individual rows and columns all share a similar syntax. The options can be specified as {x,y} , where x applies to all of the columns and y applies to all of the rows; x and y can be single values, or they can be a list of values which represent each column or row in turn.

With no (RefLink[Alignment, paclet:ref/Alignment]) setting, elements align to the center horizontally and on the baseline vertically:

Grid[{{"ten", 10!}, {"twenty", 20!}}]

This changes the horizontal alignment of columns to be on the right:

Grid[{{"ten", 10!}, {"twenty", 20!}}, Alignment->{Right, Baseline}]

This sets the horizontal alignment of each column separately:

Grid[{{"ten", 10!}, {"twenty", 20!}}, Alignment->{{Left,Right}, Baseline}]

When (RefLink[Background, paclet:ref/Background]) or (RefLink[ItemStyle, paclet:ref/ItemStyle]) options specify distinct settings for rows and columns, the front end will attempt to combine the settings where the rows and columns overlap.

This shows how the green row combines with columns of various colors:

Grid[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, Background->{{Orange,None,Cyan}, {None,Green,None}}]

This example shows how (RefLink[ItemStyle, paclet:ref/ItemStyle]) can combine styles specified in both rows and columns:

Grid[{{1, 2}, {3, 4}}, ItemStyle->{{Red,Automatic}, {Bold,Italic}}]

To repeat an individual row or column specification over multiple rows or columns, wrap it in a list. The repeated element will be used as often as necessary. If you wrap multiple elements in a list, the entire list will be repeated in sequence.

The red divider is repeated:

Grid[{{1,2,3,4,5,6},{7,8,9,10,11,12}}, Dividers->{{None,{Red},None},None}]

Here, red and black dividers are repeated in sequence:

Grid[{{1,2,3,4,5,6},{7,8,9,10,11,12}}, Dividers->{{None,{Red,Black},None},None}]

The (RefLink[ItemSize, paclet:ref/ItemSize]) and (RefLink[Spacings, paclet:ref/Spacings]) options take their horizontal measurements in ems and their vertical measurements in line heights based upon the current font. Both options also can take a (RefLink[Scaled, paclet:ref/Scaled]) coordinate, where the coordinate specifies the fraction of the total cell width or window height. The (RefLink[ItemSize, paclet:ref/ItemSize]) option also allows you to request as much space as is required to fit all of the elements in the given row or column by using the keyword (RefLink[Full, paclet:ref/Full]).

This makes all of the items 3 ems wide and 1 line height tall:

Grid[{{1, 2}, {3, 4}}, Dividers->All, ItemSize->{3, 1}]

The same example in a new font size will show at a different size:

Style[Grid[{{1, 2}, {3, 4}}, Dividers->All, ItemSize->{3, 1}],FontSize->18]

The buttons in this example will always be sized to be a quarter of the width of the cell:

Grid[{{Button["Left button"], Button["Right button"]}},ItemSize->{{{Scaled[0.25]}}, Full}]

The first and last settings for (RefLink[Spacings, paclet:ref/Spacings]) specify one-half of the top and bottom space:

Grid[{{1, 2}, {3, 4}}, Frame->True, Spacings->{{1, 0, 1}, {1, 1, 1}}]

Some options for (RefLink[Item, paclet:ref/Item]).

Many of the settings which can be applied to entire rows and columns can also be applied individually to the elements of a (RefLink[Grid, paclet:ref/Grid]) or (RefLink[Column, paclet:ref/Column]) by using the (RefLink[Item, paclet:ref/Item]) wrapper. (RefLink[Item, paclet:ref/Item]) allows you to change these settings at the granularity of a single item. Settings which are specified at the (RefLink[Item, paclet:ref/Item]) level always override settings from the (RefLink[Grid, paclet:ref/Grid]) or (RefLink[Column, paclet:ref/Column]) as a whole.

This sets item-specific options for the lower left-hand element:

Grid[{{1,2},{Item[3, Background->LightGreen, Frame->True],4}}]

The (RefLink[Background, paclet:ref/Background]) setting for (RefLink[Item, paclet:ref/Item]) overrides the one for the (RefLink[Column, paclet:ref/Column]):

Column[{1,2,Item[3, Background->Pink]}, Background->Yellow]

Most of the options to (RefLink[Item, paclet:ref/Item]) take the same settings as their (RefLink[Grid, paclet:ref/Grid]) counterparts. However, the (RefLink[Alignment, paclet:ref/Alignment]) and (RefLink[ItemSize, paclet:ref/ItemSize]) options, which allow complex row and column settings in (RefLink[Grid, paclet:ref/Grid]), take only the {horizontal,vertical} setting in (RefLink[Item, paclet:ref/Item]).

This specifies a larger item area and how the text should be aligned within it:

Column[{Item["Some aligned text",Frame->True,ItemSize->{15,3},Alignment->{Center,Bottom}], "caption"}]

The width value of the (RefLink[ItemSize, paclet:ref/ItemSize]) option is used to determine line breaking:

Column[{Item[N[Pi,20],ItemSize->{10,2}],"digits of pi"}]

The (RefLink[ItemSize, paclet:ref/ItemSize]) here specifies a minimum height of 2 line heights, but the item is larger:

Column[{Item[N[Pi,50],ItemSize->{10,2}],"digits of pi"}]

Formatting Higher-Dimensional Data

(RefLink[Column, paclet:ref/Column]) supports one-dimensional data, and (RefLink[Grid, paclet:ref/Grid]) supports two-dimensional data. To print arrays with an arbitrary number of dimensions, you can use (RefLink[TableForm, paclet:ref/TableForm]).

Here is the format for a 2×2 array of elements a[i,j]:

TableForm[Array[a,{2,2}]]

Here is a 2×2×2 array:

TableForm[{Array[a,{2,2}],Array[b,{2,2}]}]

And here is a 2×2×2×2 array:

TableForm[{{Array[a,{2,2}],Array[b,{2,2}]},{Array[c,{2,2}],Array[d,{2,2}]}}]

In general, when you print an n-dimensional table, successive dimensions are alternately given as columns and rows. By setting the option (RefLink[TableDirections, paclet:ref/TableDirections])->{(Subscript[dir, 1]),(Subscript[dir, 2]),…}, where the (Subscript[dir, i]) are (RefLink[Column, paclet:ref/Column]) or (RefLink[Row, paclet:ref/Row]), you can specify explicitly which way each dimension should be given. By default, the option is effectively set to {(RefLink[Column, paclet:ref/Column]),(RefLink[Row, paclet:ref/Row]),(RefLink[Column, paclet:ref/Column]),(RefLink[Row, paclet:ref/Row]),…}.

The option (RefLink[TableDirections, paclet:ref/TableDirections]) allows you to specify explicitly how each dimension in a multidimensional table should be given:

TableForm[{Array[a,{2,2}],Array[b,{2,2}]},TableDirections->{Row,Row,Column}]

(RefLink[TableForm, paclet:ref/TableForm]) can handle arbitrary "ragged" arrays. It leaves blanks wherever there are no elements supplied.

(RefLink[TableForm, paclet:ref/TableForm]) can handle "ragged" arrays:

TableForm[{{a,a,a},{b,b}}]

You can include objects that behave as "subtables":

TableForm[{{a,{{p,q},{r,s}},a,a},{{x,y},b,b}}]

You can control the number of levels in a nested list to which (RefLink[TableForm, paclet:ref/TableForm]) goes by setting the option (RefLink[TableDepth, paclet:ref/TableDepth]).

This tells (RefLink[TableForm, paclet:ref/TableForm]) only to go down to depth 2. As a result {x,y} is treated as a single table entry:

TableForm[{{a,{x,y}},{c,d}},TableDepth->2]

Options for (RefLink[TableForm, paclet:ref/TableForm]).

With the option (RefLink[TableAlignments, paclet:ref/TableAlignments]), you can specify how each entry in the table should be aligned with its row or column. For columns, you can specify (RefLink[Left, paclet:ref/Left]), (RefLink[Center, paclet:ref/Center]), or (RefLink[Right, paclet:ref/Right]). For rows, you can specify (RefLink[Bottom, paclet:ref/Bottom]), (RefLink[Center, paclet:ref/Center]), or (RefLink[Top, paclet:ref/Top]). If you set (RefLink[TableAlignments, paclet:ref/TableAlignments])->(RefLink[Center, paclet:ref/Center]), all entries will be centered both horizontally and vertically. (RefLink[TableAlignments, paclet:ref/TableAlignments])->(RefLink[Automatic, paclet:ref/Automatic]) uses the default choice of alignments.

Entries in columns are by default aligned on the left:

TableForm[{a,bbbb,cccccccc}]

This centers all entries:

TableForm[{a,bbbb,cccccccc},TableAlignments->Center]

You can use the option (RefLink[TableSpacing, paclet:ref/TableSpacing]) to specify how much horizontal space there should be between successive columns, or how much vertical space there should be between successive rows. A setting of 0 specifies that successive objects should abut.

This leaves 6 spaces between the entries in each row, and no space between successive rows:

TableForm[{{a,b},{ccc,d}},TableSpacing->{0,6}]

Settings for the option (RefLink[TableHeadings, paclet:ref/TableHeadings]).

This puts integer labels in a 2×2×2 array:

TableForm[Array[a,{2,2,2}],TableHeadings->Automatic]

This gives a table in which the rows are labeled by integers, and the columns by a list of strings:

TableForm[{{a,b,c},{ap,bp,cp}},TableHeadings->{Automatic,{"first","middle","last"}}]

This labels the rows but not the columns. (RefLink[TableForm, paclet:ref/TableForm]) automatically drops the third label since there is no corresponding row:

TableForm[{{2,3,4},{5,6,1}},TableHeadings->{{"row a","row b","row c"},None}]