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.

Here is a list.
In[1]:=
Click for copyable input
Out[1]=
Grid gives the list typeset in a tabular format.
In[2]:=
Click for copyable input
Out[2]=
TableForm displays the list in a tabular format.
In[3]:=
Click for copyable input
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.

Column is a shorthand for a Grid with one column.
In[4]:=
Click for copyable input
Out[4]=
The FullForm of a Grid or Column demonstrates that the head is inert.
In[5]:=
Click for copyable input
Out[5]//FullForm=
All of these wrappers can be used to present any kind of data, including graphical data.
In[6]:=
Click for copyable input
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.
In[7]:=
Click for copyable input
Out[7]=
Column displays the list in a column.
In[8]:=
Click for copyable input
Out[8]=
This aligns the numbers by padding each one to leave room for up to 20 digits.
In[9]:=
Click for copyable input
Out[9]//PaddedForm=
In this particular case, you could also align the numbers using the Alignment option.
In[10]:=
Click for copyable input
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.
In[11]:=
Click for copyable input
Out[11]//PaddedForm=
SpanFromLeftspan from the element on the left
SpanFromAbovespan from the element above
SpanFromBothspan from the element above and to the left

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.
In[12]:=
Click for copyable input
Out[12]=
Similarly, a column can be spanned using SpanFromAbove.
In[13]:=
Click for copyable input
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.
In[14]:=
Click for copyable input
Out[14]=
option
default value
BackgroundNonewhat background colors to use
BaselinePositionAutomaticwhat to align with a surrounding text baseline
BaseStyle{}base style specifications for the grid
FrameNonewhere to draw frames in the grid
FrameStyleAutomaticstyle to use for frames

Some options which affect the behavior of a Grid as a whole.

The Frame option can specify a frame around the entire Grid.
In[15]:=
Click for copyable input
Out[15]=
This uses FrameStyle to change the appearance of a frame.
In[16]:=
Click for copyable input
Out[16]=
This uses Background to specify a background color for the entire Grid.
In[17]:=
Click for copyable input
Out[17]=
The position of a Grid relative to its surroundings can be controlled using the BaselinePosition option.
In[18]:=
Click for copyable input
Out[18]=
This aligns the bottom of the grid with the baseline.
In[19]:=
Click for copyable input
Out[19]=
This sets the base style of the entire Grid to be the Subsection style.
In[20]:=
Click for copyable input
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.

This sets some Grid options for Column.
In[21]:=
Click for copyable input
Out[21]=
option
default value
Alignment{Center,Baseline}horizontal and vertical alignment of items
DividersNonewhere to draw divider lines in the grid
ItemSizeAutomaticwidth and height of each item
ItemStyleNonestyles for columns and rows
Spacings{0.8,0.1}horizontal and vertical spacings

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 , 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.
In[22]:=
Click for copyable input
Out[22]=
This changes the horizontal alignment of columns to be on the right.
In[23]:=
Click for copyable input
Out[23]=
This sets the horizontal alignment of each column separately.
In[24]:=
Click for copyable input
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.
In[25]:=
Click for copyable input
Out[25]=
This example shows how ItemStyle can combine styles specified in both rows and columns.
In[26]:=
Click for copyable input
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.
In[27]:=
Click for copyable input
Out[27]=
Here, red and black dividers are repeated in sequence.
In[28]:=
Click for copyable input
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.
In[29]:=
Click for copyable input
Out[29]=
The same example in a new font size will show at a different size.
In[30]:=
Click for copyable input
Out[30]=
The buttons in this example will always be sized to be a quarter of the width of the cell.
In[31]:=
Click for copyable input
Out[31]=
The first and last settings for Spacings specify one-half of the top and bottom space.
In[32]:=
Click for copyable input
Out[32]=
option
default value
Alignment{Center,Baseline}horizontal and vertical alignment of items
BackgroundNonewhat background colors to use
BaseStyle{}base style specifications for the item
FrameNonewhere to draw frames around the item
FrameStyleAutomaticstyle to use for frames
ItemSizeAutomaticwidth and height of each item

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.
In[33]:=
Click for copyable input
Out[33]=
The Background setting for Item overrides the one for the Column.
In[34]:=
Click for copyable input
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 setting in Item.

This specifies a larger item area and how the text should be aligned within it.
In[35]:=
Click for copyable input
Out[35]=
The width value of the ItemSize option is used to determine line breaking.
In[36]:=
Click for copyable input
Out[36]=
The ItemSize here specifies a minimum height of 2 line heights, but the item is larger.
In[37]:=
Click for copyable input
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 .
In[39]:=
Click for copyable input
Out[39]//TableForm=
Here is a 2×2×2 array.
In[40]:=
Click for copyable input
Out[40]//TableForm=
And here is a 2×2×2×2 array.
In[41]:=
Click for copyable input
Out[41]//TableForm=

In general, when you print an dimensional table, successive dimensions are alternately given as columns and rows. By setting the option TableDirections->{dir1,dir2,}, where the 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.
In[42]:=
Click for copyable input
Out[42]//TableForm=

TableForm can handle arbitrary "ragged" arrays. It leaves blanks wherever there are no elements supplied.

TableForm can handle "ragged" arrays.
In[43]:=
Click for copyable input
Out[43]//TableForm=
You can include objects that behave as "subtables".
In[44]:=
Click for copyable input
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 is treated as a single table entry.
In[45]:=
Click for copyable input
Out[45]//TableForm=
option name
default value
TableDepthInfinitymaximum 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.
In[46]:=
Click for copyable input
Out[46]//TableForm=
This centers all entries.
In[47]:=
Click for copyable input
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 specifies that successive objects should abut.

This leaves 6 spaces between the entries in each row, and no space between successive rows.
In[48]:=
Click for copyable input
Out[48]//TableForm=
Noneno labels in any dimension
Automaticsuccessive integer labels in each dimension
{{lbl11,lbl12,},}explicit labels

Settings for the option TableHeadings.

This puts integer labels in a 2×2×2 array.
In[49]:=
Click for copyable input
Out[49]//TableForm=
This gives a table in which the rows are labeled by integers, and the columns by a list of strings.
In[50]:=
Click for copyable input
Out[50]//TableForm=
This labels the rows but not the columns. TableForm automatically drops the third label since there is no corresponding row.
In[51]:=
Click for copyable input
Out[51]//TableForm=