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

Grid

Grid
is an object that formats with the arranged in a two-dimensional grid.
  • In a notebook, you can enter expressions in a two-dimensional layout by using Ctrl+, to add columns and Ctrl+Enter to add rows. By default, this layout evaluates to a nested list. You can make a Grid that preserves the layout by using Grid[layout].
  • You can use Tab to move to the next placeholder in a Grid. Ctrl+Space moves out of the whole Grid.
  • First[grid] extracts the list of lists that make up a grid.
  • The can be expressions of any kind, including graphics and controls.
  • You can enter text in an element of a Grid by giving it as a string. You can use Text or Style to specify a particular style.
  • The in Grid do not all need to be the same length; there will be empty space in the grid at the ends of shorter rows.
  • You can specify that an element e should span multiple positions in a grid using, for example, Grid[{{e, SpanFromLeft, SpanFromLeft}, ...}].
  • The following specify that a particular position should be spanned:
SpanFromLeftspan from left
SpanFromAbovespan from above
SpanFromBothspan from both left and above
  • In a notebook, you can combine selected elements in a grid to make a single spanning element using the Make Spanning menu item.
  • The following options can be given:
Alignment{Center,Baseline}horizontal and vertical alignment of items
AutoDeleteFalsewhether to remove the grid structure if only one item remains
BackgroundNonewhat background colors to use
BaselinePositionAutomaticwhat to align with a surrounding text baseline
BaseStyle{}base style specifications for the grid
DefaultElement""what element to insert in an empty item
DividersNonewhere to draw divider lines in the grid
FrameNonewhere to draw frames in the grid
FrameStyleAutomaticstyle to use for frames
ItemSizeAutomaticwidth and height of each item
ItemStyleNonestyles for columns and rows
SpacingsAutomatichorizontal and vertical spacings
  • Common settings for Frame include:
Noneno frame
Trueframe around the whole grid
Allframe around every item in the grid
Centerdraw interior frames only
{All, False}put a frame at every horizontal position (column lines)
{False, All}put a frame at every vertical position (row lines)
specapply spec to all items
{specx}apply at successive horizontal positions
{specx,specy}apply at successive horizontal and vertical positions
{specx,specy,rules}also allow explicit rules for individual elements
  • The can have the following forms:
{s1,s2,...,sn}use through , then use defaults
{{c}}use c in all cases
{{c1,c2}}alternate between and
{{c1,c2,...}}cycle through all
{s,{c}}use s, then repeatedly use c
{s1,{c},sn}use , then repeatedly use c, but use at the end
{s1,s2,...,{c1, c2, ... }, sm, ... , sn}
use the first sequence of at the beginning, then cyclically use the , then use the last sequence of at the end
{s1,s2,...,{},sm,...,sn}use the first sequence of at the beginning and the last sequence at the end
{i1->v1,i2->v2,...}specify what to use at positions
{spec,rules}use rules to override specifications in spec
  • With settings of the form , if there are more specified than items across the grid, from the beginning are used for the first items, and ones from the end are used for the last items.
  • With the default setting ItemSize->Automatic, Grid will break elements across multiple lines if necessary.
  • Grid will not change the size of graphics or other objects that have explicit ImageSize settings.
  • For a grid with n items in a particular direction, Dividers and Spacings can specify settings for the n+1 gaps between elements, starting before the first element and ending after the last element.
specapply spec to all gaps between items
{specx,specy}apply , to successive horizontal and vertical gaps
Axisaxis of the middle row in the grid
Baselinebaseline of the middle row in the grid
Bottombottom of the whole grid
Centerhalfway from top to bottom
Toptop of the whole grid
{{i,j},pos}position pos in element
pos->refbaseline offset to make pos be aligned with ref
  • The settings for BaseStyle are appended to the default style typically given by the style in the current stylesheet.
Display elements in a grid:
Put a frame around every element:
Use Text to format grid elements as text:
Display elements in a grid:
In[1]:=
Click for copyable input
Out[1]=
 
Put a frame around every element:
In[1]:=
Click for copyable input
Out[1]=
 
Use Text to format grid elements as text:
In[1]:=
Click for copyable input
Out[1]=
Grid can contain elements of any size:
Grid can contain graphics or any other expressions:
Grid can contain active controls:
Create a grid with a single row:
Grid can be used to format grids with no rows, empty rows, or ragged rows:
Put a frame around the whole grid:
Put a frame around every element:
Draw all the frames in red:
Put a frame around the first row and column:
Draw different frames with different styles:
Put dividers at all horizontal positions:
Put dividers at all vertical positions:
Put dividers at the third horizontal and second vertical positions:
Make the element 4 span the column to its right:
Make it span three columns:
Make the element 2 span the row below it:
Make it span three rows:
Span throughout a 2×2 block:
Align contents to the left:
Align contents to the right:
Draw the grid with a pink background:
A pink background for the column at the first horizontal position:
Alternating pink and yellow at successive horizontal positions:
Alternating pink and yellow at successive vertical positions:
Make the grid contents red:
Embed styling options with a particular grid item:
Make each row and column just big enough to fit the items they contain:
Leave the same amount of space for all items:
Set the horizontal spacing between items:
Set the horizontal and vertical spacings:
Grids can be nested:
Align elements around the center of the grid:
Align numbers on the decimal point:
Draw the grid with a pink background:
Pink and gray backgrounds for the first and second horizontal positions:
An equivalent syntax:
Pink and gray backgrounds for the first and second vertical positions:
Alternating pink and gray backgrounds:
Alternating backgrounds with yellow superimposed in the first and last positions:
Blending of colors:
Set the background for specific items:
Set the background for a subregion of the grid:
Align the center of the grid with the baseline of surrounding text:
Align the bottom of the grid with the baseline:
Align a specific grid element with the baseline:
Draw all interior dividers:
Draw a divider at every other horizontal position:
Include the final position:
Draw dividers with specified styles:
Frame the entire grid:
Frame all elements in the grid:
Frame specific elements:
Frame a region:
FrameStyle supports a variety of graphics directives:
FrameStyle is applied to all sources of grid lines, including Dividers and Item:
Directives given with FrameStyle are combined with those specified elsewhere:
Make each item a fixed number of character-widths wide:
ItemSize->All makes all items the same size:
ItemSize->Automatic line-wraps textual items at the page width:
Prevent line-wrapping:
Set one overall style for grid items:
Use Directive to wrap multiple style directives:
Use a style from the current stylesheet:
Style specific elements:
Style a region:
Insert no additional space between rows or columns:
Insert no additional space between rows:
Specify spacing with numeric values:
Use different spacings at the first vertical divider:
A simple user interface layout:
Presenting tabular data:
Styles given in a list will be interpreted as corresponding to successive columns or rows:
Directive groups styles into a single collective unit:
To span across both rows and columns requires appropriate use of SpanFromBoth:
A visual design:
A Sudoku grid:
Create a random arrangement of spanning rows and columns:
New in 6