A very minimal table, hence with left-justified cells, no borders. We do wrap the tabular element in a table element to get centering, numbering and a caption.
Red
Green
Yellow
Blue
White
Pink
Table13.1 Some Colors
Note that tables may be constructed using the LaTeX Complex Table Editor tool online at latex-tables.comand then exported in PreTeXt syntax.
Tables can be used many ways. We describe long division of polynomials by using vertical and horizontal borders on individual entries of a table. The division lines are slightly thicker than the subtraction lines. This is a good example of the typical abuse of tables for horizontal and vertical layout. Also indicative of this abuse is that it might make more sense to call this a “Figure,” not a “Table”.
\(x\)
\(-\)
\(5\)
\(x\)
\(+\)
\(2\)
\(x^2\)
\(-\)
\(3x\)
\(-\)
\(8\)
\(x^2\)
\(+\)
\(2x\)
\(-5x\)
\(-\)
\(8\)
\(-5x\)
\(-\)
\(10\)
\(2\)
Table13.2 Polynomial Long Division
The next table describes how to construct tables via the tabular element. The table element may be used to enclose the raw table, so as to associate a caption and get vertical separation with horizontal centering.
The tabular element contains a sequence of row elements, and must contain at least one. Each row contains a sequence of cell elements and must have the same number in each row (acccounting for the use of the colspan attribute). The contents of the cell elements are the text to appear in entries of the table.
A sequence of col elements may optionally be used. But if one appears, then there must be the right number for the width of the table. They are empty elements always, and just carry information about their respective column.
Where the body of the table below has an entry, it means the attribute may be used on the element, and affects the range of the tabular described by the element. Employment of an attribute on elements to the right in the table will supersede use on elements to the left. Generally, every cell has right and bottom borders, but only cells at the left side of the table have a left border and only cells across the top have a top border. Only one cell has four borders.
Attributes
Elements
Values
tabular
col
row
cell
* = default
top
\(\times\)
\(\times\)
none*, minor, medium, major
left
\(\times\)
\(\times\)
none*, minor, medium, major
bottom
\(\times\)
\(\times\)
\(\times\)
none*, minor, medium, major
right
\(\times\)
\(\times\)
\(\times\)
none*, minor, medium, major
halign
\(\times\)
\(\times\)
\(\times\)
\(\times\)
left*, center, right, justify
halign
p
decimal, character
header
p
p
p
yes/no*
footer
p
p
p
yes/no*
valign
\(\times\)
\(\times\)
top, middle, bottom
colspan
\(\times\)
1*, integer
rowspan
p
1*, integer
width
\(\times\)
percentage
colors
p
p
p
p
Table13.3 Tabular Elements and Attributes (p = planned)
Bully Pulpit: Vertical Rules in Tables
One of the goals of PreTeXt is to gently guide authors towards good choices in the design of their documents, even if we do not claim to know it all ourselves. Take a close look at that table about tables. What's missing? No vertical rules. Try living without them, you will not really miss them. If you think you need to divide a table into two halves, maybe you really need two tables (and then see the “side-by-side” capabilities, Section 22).
In the documentation for his excellent LaTeX package, booktabs, Simon Fear gives two rules for what he calls “formal tables”: (1) Never, ever use vertical rules, and (2) Never use double rules. We have resisted the temptation to enforce the former and have provided an alternative to the second (three thicknesses). He refers to using tables for layout as creating “tableau.” If you are finicky about the look of your work, the first three pages of the documentation is recommended reading.
That all said, we now give several examples in order to stress and demonstrate our code.
An example of aligning table cells' contents horizontally. See the source for comments.
1234567890
1234567890
1234567890
1234567890
[First
Second
Third
Fourth
A
B
C
D
1
2
3
4
Table13.4 Horizontal Alignment Example
Example from above, but now with horizontal rules, plus an extra row to test the bottom border. See the source for comments.
1234567890
1234567890
1234567890
1234567890
First
Second
Third
Fourth
A
B
C
D
1
2
3
4
1
2
3
4
Table13.5 Horizontal Rules Example
For a table without a caption, create a <tabular> and place it inside a <sidebyside>. This will allow control over the horizontal placment, but without a caption, there is no number, and the tabular cannot be cross-referenced.
One
Same example as before, but now with vertical rules. See the source for comments.
Table13.10 Column Spans, No col Elements, Nine Columns
This example tests several things. In LaTeX output, figures, tables, listings and side-by-sides are “floats” whose placement can migrate, but we have tries to supress this behavior. However, a float that is the first item of an “environment” (like a theorem or an example) can still float to a position before its title. If that does not happen here, then our additional defenses are working.
This example also checks that the total number of columns is correctly computed from the first row, which features several colspan attributes.
A bare minimum table (one row with one cell) to test edge cases:
One
Table13.11 One entry table
Table cells with a fixed width where text wraps are known as “paragraph cells”. A cell will be created as a paragraph cell if and only if it has <p> children. And such cells should only have <p> children. The width of a paragraph cell is determined by a width attribute on the corresponding <col> (as a percentage). If no width is specified (or there isn't even a <col> in the first place) then xsltproc will abort. If the column has a non-paragraph cell with contents that are wider than the paragraph cells, results will be undesirable. There is presently no implementation for a paragraph cell that has a colspan greater than \(1\text{,}\) although cells with colspan greater than \(1\) that are above or below a paragraph cell will behave. Setting width on a <col> that has no paragraph cells may produce unexpected results. A valign for the parent <row> (or the ambient <tabular>) can control vertical alignment (top, middle, or bottom). A paragraph cell's halign attribute (left, center, right, or justify) controls how the text is justfied. Cells inherit halign from <row>, <col>, and <tabular> in that order of preference. In a non-paragraph cell where halign='justify', the horizontal alignment will match the behavior of halign='left'.
Unit
Stands For
Defininion
Roughly
s
second
the duration of 9192631770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the cesium-133 atom
an extraneous paragraph just to demonstrate the inter-paragraph formatting.
the time it takes you to say the phrase “differential calculus”
min
minute
exactly \(60\) seconds
how long it takes to microwave a full dinner plate from the refrigerator
h
hour
exactly \(3600\) seconds; exaclty \(60\) minutes
the length of one episode of a premium cable television show
Table13.12 Time Units
Table cells can have multiline content using <line> elements. This is not the same thing as a paragraph cell—line breaking will happen precisely where the author tells it to. A <line> will not break, even on a narrow screen. If a cell uses a <line>, it must only use a sequence of <line>s and no other content. As with paragraph cells, you can use a valign attribute for the row.
One Fish Two Fish Red Fish Blue Fish
I am the Lorax. I speak for the trees.
Look at me! Look at me! Look at me NOW! It is fun to have fun. But you have to know how.
Table13.13 Dr. Seuss lines
This is a table torture test with many combinations of halign, valign, colspan, <p> children, and <line> children.
Cell too wide
Lf md
Lef mid par cel
Rt md
Rig mid par cel
Cn md
Cen mid par cel
Js md
Jus mid par cel jus mid par cel
Colspan=2 lef mid with lines
Colspan=3 rig mid
Lines Between Par
Lines Between No Par
Par in row with lines
L t
Lef top par cel
R t
Rig top par cel
C t
Cen top par cel
J t
Jus top par cel jus top par cel
L b
Lef bot par cel
R b
Rig bot par cel
C b
Cen bot par cel
J b
Jus bot par cel jus bot par cel
Colspan=3 lef bot
Colspan=2 rig bot with lines
Lines Under Par
Lines Under No Par
Par in row with lines
Table13.14 Table Torture Test
And now a <sidebyside> with a <table> and a <tabular> to check that width is scaled appropriately. See Section 22 to learn about <sidebyside>s.
A1.S1
All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives.
Should be 50% of 45% except perhaps on small screens.
A1.S2.C1
The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.
Should be 50% of 55% except perhaps on small screens.
Figure13.15 Some text from the US Constitution
Tables are formed in LaTeX output with copious use of the \multicolumn macro to override more global alignment settings, and to spread the content of one cell across several columns. However, sometimes LaTeX's special characters have behaved badly in this situation. So the table below, two items per row, is just designed for LaTeX testing. But of course, it should still render fine in other formats. The three test cases are from 8.7, but without 50 alphabetic characters and 8 digits, which should not be problems in this context. The first column's entries are forced to be wrapped in a \multicolumn by specifying their horizontal alignment. The second column's entries will not be wrapped in a \multicolumn.So the two columns will look identical, other than the first having a left alignment, and the second has the default center alignment.