Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

09 Table Formatting

0 (0 Likes / 0 Dislikes)
This section of Introduction to Wolfram Notebooks describes a more precise tools for formatting tables, like the table shown here, and is specifically about options for setting up details, like the horizontal divider and the rows with alternating backgrounds, and tools for controlling the appearance of individual items in a table. Starting with the first example, here is an input that generates a template for that table. Evaluating this input gives a result in an Output cell, which I will convert to a Text cell, since most of the entries in that table are plain text. The input uses the Grid function. The first argument of the Grid function is the actual array of table entries that gives the headings and an array of placeholders, and there are six options. The Background option sets up the alternating white and gray backgrounds for the rows. If I click inside the table and use CONTROL+RETURN to add more rows, the alternating backgrounds continue for the new rows. The Alignment option specifies the horizontal alignment of entries within each column and vertical alignment within each row. The Dividers option draws the lines between the rows. This is where the thick horizontal divider below the heading is specified. The BaseStyle option specifies the font to use for entries in the table. And finally, the ItemSize option specifies the space to allow for each column, and the Spacings option specifies space between columns. There are lots of details that these options control, but the basic ideas are fairly simple and several of these options use similar designs that can be illustrated by considering their effect on a generic table. Here is a table with placeholder entries and with Frame set to All to make it easier to see the rows and the columns. Starting with the Background option, the Background option can be set to a single color, to set the background for the entire grid, but the general form of the Background option is a list with three elements where the first element specifies the background for columns, the second element specifies the background for rows and the third element can be used to specify the background for individual elements in the table. For example, entering Red, Green, Blue in the first element causes the first three columns to be red, green and blue. Enclosing colors in a list causes those colors to be repeated across the table. For example, this gives red for the first column and repeated green and blue columns for the rest of the table. The same structure applies to rows. For example, if that same color specification is the second element in the value of the Background option, the result is a table where the first row is red and the remaining rows alternate between green and blue. The third element in the value of the Background option can be used for specifying the background color of individual entries in the table. For example, this specifies that the seventh element in the fourth row should have an orange background. Similar principles apply to the Dividers option and to the Alignment option and to all of the options that control specific rows and columns. For example, this shows a value for the Dividers option that gives a column divider every third column and a row divider above the second row. As with the Background option, the value of the Dividers option is a list. The first element of that list specifies column dividers and the second element specifies row dividers. The dividers can also have styles. For example, this specifies that the column dividers should be repeated thick lines and light gray lines, and that the horizontal divider below the top row should be a thick red line. With those general principles in mind, here again is the first example, where I will go ahead and fill in some values in the table. This value of the Background option gives the alternating white and light gray background for the rows. The Alignment option specifies that items in the first column should be centered horizontally. The second column should be aligned on the right, as is typically done for columns of numbers and items in the third column are aligned on the left. This value of the Dividers option gives the thick line below the top row and the light gray dividers for all of the rows after that. The light gray divider becomes part of the background for all of the rows except the last one, where the divider shows up as a light grey line at the bottom of the table. The BaseStyle option specifies the default font to use for all of the items in the table. The ItemSize option here specifies the width of each column. The first two columns are given as scaled amounts, which means that they are scaled to a fraction of the formatting width, which is related to the width of the window. So the width of the columns changes with the width of the display, which can be desirable if the table might be displayed in different windows. Finally, this value of the Spacings option adds some space between the last two columns. A good way to learn what these options do is to try different values for the options and observe the effect on the table. For example, here is a simple Manipulate program that gives a control for adjusting the space between the last two columns. Rather than using options for the Grid function, a separate strategy for specifying styles is to use Item expressions as elements in the table and to specify options within each Item expression. For example, rather than using the Background option to specify the background color of a particular entry in the table, another way to get the same effect is to make that one element into an Item expression and to include the Background option in that Item expression. A related feature is using the special symbols SpanFromLeft, SpanFromAbove and SpanFromBoth to get table entries that span more than one row or more than one column. For example, here is a small table of placeholders. Replacing one of those placeholders with the special symbol SpanFromLeft causes that entry in the table to be merged with the entry to the left. There are also symbols called SpanFromAbove and SpanFromBoth that can be used to merge entries vertically or in both directions. The affect actually ends up being the same as selecting items to be merged and choosing Make Spanning from the Table/Matrix submenu. As a final example which uses several of these tools, here is the second of the two tables that were shown earlier together with an input to generate that table. This input again uses the Grid function with several options. The ItemSize option specifies columns with a width of three, which means three units called ems. An em is a unit equal to the point size of the current font. If the ItemSize option is omitted, then the columns are adjusted to the width of the widest item in the column, which here gives a table with columns that seem a bit too narrow, so the ItemSize option is included to give a table that is not quite so cramped. This value of the Frame option draws the divider lines and the frame around the table. This value of the Alignment option specifies that items should be centered both vertically and horizontally and the Background option specifies a light gray background for the top row and alternating orange and light blue background, for the remaining rows. Background is used in this table as a color code for the T and F entries in the table. The Background option happens in this example to give the correct color for all of the entries, except for one where the background is specified in an Item expression. Without that Item expression, the color for that item would be light blue, which doesn't follow the intended color code. This table also includes two SpanFromAbove entries, which are there to generate the two items in the first column that span more than one row. That's the end of the examples for this section. A good source for additional information is the documentation for the Grid function and related functions, and especially this tutorial on grids, rows and columns. And it is of course almost always useful to just construct a generic table and try these things out for yourself.

Video Details

Duration: 7 minutes and 39 seconds
Language: English
License: Dotsub - Standard License
Genre: None
Views: 0
Posted by: wolfram on Feb 4, 2020

09 Table Formatting

Caption and Translate

    Sign In/Register for Dotsub above to caption this video.