Tables and Matrices
| Column[list] | typeset as a column of elements |
| Grid[list] | typeset as a grid of elements |
| TableForm[list] | print in tabular form |
Formatting lists as tables and matrices.
| Out[1]= |  |
|
Grid gives the list typeset in a tabular format.
| Out[2]= |  |
|
TableForm displays the list in a tabular format.
Out[3]//TableForm= |
| |  |
|
Grid and
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.
| Out[4]= |  |
|
Out[5]//FullForm= |
| |  |
|
All of these wrappers can be used to present any kind of data, including graphical data.
| Out[6]= |  |
|
| PaddedForm[Column[list],tot] | print a column with all numbers padded to have room for tot digits |
| PaddedForm[Grid[list],tot] | print a table with all numbers padded to have room for tot digits |
| PaddedForm[Grid[list],{tot,frac}] | put frac digits to the right of the decimal point in all approximate real numbers |
Printing tables of numbers.
Here is a list of numbers.
| Out[7]= |  |
|
Column displays the list in a column.
| Out[8]= |  |
|
This aligns the numbers by padding each one to leave room for up to 20 digits.
Out[9]//PaddedForm= |
| |  |
|
In this particular case, you could also align the numbers using the Alignment option.
| Out[10]= |  |
|
This lines up the numbers, padding each one to have room for 8 digits, with 5 digits to the right of the decimal point.
Out[11]//PaddedForm= |
| |  |
|
Symbols used to represent spanning in Grid.
Grid takes a rectangular matrix as its first argument. Individual elements of the
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 SpanFromLeft.
| Out[12]= |  |
|
| Out[13]= |  |
|
When specifying a rectangular spanning area, SpanFromBoth is used in every element which is both below and to the right of the spanning element.
| Out[14]= |  |
|
Some options which affect the behavior of a Grid as a whole.
The Frame option can specify a frame around the entire Grid.
| Out[15]= |  |
|
This uses FrameStyle to change the appearance of a frame.
| Out[16]= |  |
|
This uses Background to specify a background color for the entire Grid.
| Out[17]= |  |
|
The position of a Grid relative to its surroundings can be controlled using the BaselinePosition option.
| Out[18]= |  |
|
This aligns the bottom of the grid with the baseline.
| Out[19]= |  |
|
This sets the base style of the entire Grid to be the Subsection style.
| Out[20]= |  |
|
Column is a shorthand for specifying a
Grid with one column. Since the two functions are similar, the same options can be used for each one.
| Out[21]= |  |
|
Some options which affect the columns and rows of a Grid.
The options for
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 Alignment setting, elements align to the center horizontally and on the baseline vertically.
| Out[22]= |  |
|
This changes the horizontal alignment of columns to be on the right.
| Out[23]= |  |
|
This sets the horizontal alignment of each column separately.
| Out[24]= |  |
|
When
Background or
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.
| Out[25]= |  |
|
This example shows how ItemStyle can combine styles specified in both rows and columns.
| Out[26]= |  |
|
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.
| Out[27]= |  |
|
Here, red and black dividers are repeated in sequence.
| Out[28]= |  |
|
The
ItemSize and
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
Scaled coordinate, where the coordinate specifies the fraction of the total cell width or window height. The
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
Full.
This makes all of the items 3 ems wide and 1 line height tall.
| Out[29]= |  |
|
The same example in a new font size will show at a different size.
| Out[30]= |  |
|
The buttons in this example will always be sized to be a quarter of the width of the cell.
| Out[31]= |  |
|
The first and last settings for Spacings specify one-half of the top and bottom space.
| Out[32]= |  |
|
Some options for Item.
Many of the settings which can be applied to entire rows and columns can also be applied individually to the elements of a
Grid or
Column by using the
Item wrapper.
Item allows you to change these settings at the granularity of a single item. Settings which are specified at the
Item level always override settings from the
Grid or
Column as a whole.
This sets item-specific options for the lower left-hand element.
| Out[33]= |  |
|
| Out[34]= |  |
|
Most of the options to
Item take the same settings as their
Grid counterparts. However, the
Alignment and
ItemSize options, which allow complex row and column settings in
Grid, take only the
{horizontal, vertical} setting in
Item.
This specifies a larger item area and how the text should be aligned within it.
| Out[35]= |  |
|
The width value of the ItemSize option is used to determine line breaking.
| Out[36]= |  |
|
The ItemSize here specifies a minimum height of 2 line heights, but the item is larger.
| Out[37]= |  |
|
Formatting Higher-Dimensional Data
Column supports one-dimensional data, and
Grid supports two-dimensional data. To print arrays with an arbitrary number of dimensions, you can use
TableForm.
Here is the format for a 2×2 array of elements a[i, j].
Out[39]//TableForm= |
| |  |
|
Out[40]//TableForm= |
| |  |
|
And here is a 2×2×2×2 array.
Out[41]//TableForm= |
| |  |
|
In general, when you print an
n-dimensional table, successive dimensions are alternately given as columns and rows. By setting the option
TableDirections->{dir1, dir2, ...}, where the
diri are
Column or
Row, you can specify explicitly which way each dimension should be given. By default, the option is effectively set to
{Column, Row, Column, Row, ...}.
The option TableDirections allows you to specify explicitly how each dimension in a multidimensional table should be given.
Out[42]//TableForm= |
| |  |
|
TableForm can handle arbitrary "ragged" arrays. It leaves blanks wherever there are no elements supplied.
Out[43]//TableForm= |
| |  |
|
You can include objects that behave as "subtables".
Out[44]//TableForm= |
| |  |
|
You can control the number of levels in a nested list to which
TableForm goes by setting the option
TableDepth.
This tells TableForm only to go down to depth 2. As a result {x, y} is treated as a single table entry.
Out[45]//TableForm= |
| |  |
|
| | |
| TableDepth | Infinity | maximum number of levels to include in the table |
| TableDirections | {Column,Row,Column,...} | whether to arrange dimensions as rows or columns |
| TableAlignments | {Left,Bottom,Left,...} | how to align the entries in each dimension |
| TableSpacing | {1,3,0,1,0,...} | how many spaces to put between entries in each dimension |
| TableHeadings | {None,None,...} | how to label the entries in each dimension |
Options for TableForm.
With the option
TableAlignments, you can specify how each entry in the table should be aligned with its row or column. For columns, you can specify
Left,
Center or
Right. For rows, you can specify
Bottom,
Center or
Top. If you set
TableAlignments->Center, all entries will be centered both horizontally and vertically.
TableAlignments->Automatic uses the default choice of alignments.
Entries in columns are by default aligned on the left.
Out[46]//TableForm= |
| |  |
|
This centers all entries.
Out[47]//TableForm= |
| |  |
|
You can use the option
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.
Out[48]//TableForm= |
| |  |
|
| None | no labels in any dimension |
| Automatic | successive integer labels in each dimension |
| {{lab11,lab12,...},...} | explicit labels |
Settings for the option TableHeadings.
This puts integer labels in a 2×2×2 array.
Out[49]//TableForm= |
| |  |
|
This gives a table in which the rows are labeled by integers, and the columns by a list of strings.
Out[50]//TableForm= |
| |  |
|
This labels the rows but not the columns. TableForm automatically drops the third label since there is no corresponding row.
Out[51]//TableForm= |
| |  |
|