The other preamble contents
As the lesson didn’t cover all the available preamble-tokens, a few others are explained with examples here. You might want to revisit the tables at the start of the lesson to get an overview of the things available. The short descriptions provided there should suffice to understand what the different column types m
, b
, w
, and W
do after you understood l
, c
, r
, and p
. If not you might want to experiment a bit with them. What’s still missing are the handy other preamble-tokens >
, <
, @
, !
, and |
.
Styling a column
Since >
and <
can be used to put things before and after the cell contents of a column, you can use these to add commands which affect the look of a column. For instance, if you want to italicize the first column and put a colon after it, you can do the following:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{>{\itshape}l<{:} *{2}{l}}
\toprule
Animal & Food & Size \\
\midrule
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\bottomrule
\end{tabular}
\end{document}
\itshape
makes all the following text italic, but its effect is ‘contained’ by the table cell. We will look at manual font formatting in a few lessons time.
You may want the first cell not to be affected because it is the table head. Here \multicolumn
may be used. Remember that it can be used to change a single cell’s alignment as shown below.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{>{\itshape}l<{:} *{2}{l}}
\toprule
\multicolumn{1}{l}{Animal} & Food & Size \\
\midrule
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\bottomrule
\end{tabular}
\end{document}
Manipulating the space between columns
Usually LaTeX pads each column by some space on both sides to give a balanced look and separate them. This space is defined with the length \tabcolsep
. Due to the fact that each column is padded on both sides you get one \tabcolsep
on either end of the table, and 2\tabcolsep
between two columns – one from each column. You can adjust this space to any length using \setlength
:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\setlength\tabcolsep{1cm}
\begin{document}
\begin{tabular}{lll}
Animal & Food & Size \\
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\end{tabular}
\end{document}
You can change this space to something arbitrary using @
. This will remove the padding between two columns or on either end, and instead put anything in between the columns you specify as an argument:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\begin{document}
\begin{tabular}{l@{ : }l@{\hspace{2cm}}l}
Animal & Food & Size \\
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\end{tabular}
\end{document}
(We’ll see \hspace
again shortly; you might guess that it adds a horizontal space.)
The !
preamble token does something pretty similar. The difference is, that it adds its argument in center of the space between two columns.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\begin{document}
\begin{tabular}{l!{:}ll}
Animal & Food & Size \\
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\end{tabular}
\end{document}
Vertical rules
Sometimes you have to use vertical rules.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\begin{document}
\begin{tabular}{l|ll}
Animal & Food & Size \\[2pt]
dog & meat & medium \\
horse & hay & large \\
frog & flies & small \\
\end{tabular}
\end{document}
You might notice that the behavior of |
is pretty similar to !{decl}
; it adds the vertical rule between two columns leaving the padding as it is. There is a huge downside to this though; vertical rules don’t work with the horizontal rules provided by booktabs
. You can use the horizontal rules provided by LaTeX; those are \hline
(corresponding to \toprule
, \midrule
, and \bottomrule
) and \cline
(which behaves like \cmidrule
). As shown above, vertical rules will span any space specified in the optional argument to \\
.
Customizing booktabs
rules
All the booktabs
rules and also \addlinespace
support an optional argument in brackets with which you can specify the rule’s thickness. In addition the trimming provided by \cmidrule
can be customized by specifying a length in braces after r
or l
.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{@{} lll@{}} \toprule[2pt]
Animal & Food & Size \\ \midrule[1pt]
dog & meat & medium \\
\cmidrule[0.5pt](r{1pt}l{1cm}){1-2}
horse & hay & large \\
frog & flies & small \\ \bottomrule[2pt]
\end{tabular}
\end{document}
Numeric alignment in columns
The alignment of numbers in tables can be handled by the column type S
that is provided by the siunitx
package.
A simple example with two aligned numeric columns would be:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{SS}
\toprule
{Values} & {More Values} \\
\midrule
1 & 2.3456 \\
1.2 & 34.2345 \\
-2.3 & 90.473 \\
40 & 5642.5 \\
5.3 & 1.2e3 \\
0.2 & 1e4 \\
\bottomrule
\end{tabular}
\end{document}
Note that any non-numeric cell must be “protected” by enclosing it in braces.
The siunitx
package provides many possibilities for formatting the numbers in different ways; see the package documentation.
Specifying the total table width
The width of a tabular
environment is automatically determined based on the contents of the table. There are two commonly used mechanisms to specify a different total width.
Note that it is almost always preferable to format the table to a specified width as below (perhaps using a font size such as \small
if necessary) rather than scaling a table with \resizebox
and similar commands which will produce inconsistent font sizes and rule widths.
tabular*
The tabular*
environment takes an additional width argument that specifies the total width of the table. Stretchy space must be added to the table using the \extracolsep
command. This space is added between all columns from that point in the preamble. It is almost always used with \fill
, a special space that stretches to be as large as necessary.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\begin{document}
\begin{center}
\begin{tabular}{cc}
\hline
A & B\\
C & D\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular*}{.5\textwidth}{@{\extracolsep{\fill}}cc@{}}
\hline
A & B\\
C & D\\
\hline
\end{tabular*}
\end{center}
\begin{center}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}cc@{}}
\hline
A & B\\
C & D\\
\hline
\end{tabular*}
\end{center}
\end{document}
tabularx
The tabularx
environment, provided by the package of the same name, has a similar syntax to tabular*
but instead of adjusting the inter-column space, adjusts the widths of columns specified by a new column type, X
. This is equivalent to a specification of p{...}
for an automatically determined width.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\begin{document}
\begin{center}
\begin{tabular}{lp{2cm}}
\hline
A & B B B B B B B B B B B B B B B B B B B B B B B B\\
C & D D D D D D D\\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabularx}{.5\textwidth}{lX}
\hline
A & B B B B B B B B B B B B B B B B B B B B B B B B\\
C & D D D D D D D\\
\hline
\end{tabularx}
\end{center}
\begin{center}
\begin{tabularx}{\textwidth}{lX}
\hline
A & B B B B B B B B B B B B B B B B B B B B B B B B\\
C & D D D D D D D\\
\hline
\end{tabularx}
\end{center}
\end{document}
Unlike the other forms discussed in these lessons, tabularx
needs to typeset the table several times with trial widths to determine the final setting. This means that there are several restrictions on the use of the environment; see the package documentation.
Multi-page tables
A tabular
forms an unbreakable box so it must be small enough to fit on one page, and is often placed in a floating table
environment.
Several packages provide variants with similar syntax that do allow page breaking. Here we show the longtable
package:
\documentclass{article}
\usepackage[paperheight=8cm,paperwidth=8cm]{geometry}
\usepackage{array}
\usepackage{longtable}
\begin{document}
\begin{longtable}{cc}
\multicolumn{2}{c}{A Long Table}\\
Left Side & Right Side\\
\hline
\endhead
\hline
\endfoot
aa & bb\\
Entry & b\\
a & b\\
a & b\\
a & b\\
a & b\\
a & bbb\\
a & b\\
a & b\\
a & b\\
a & b\\
a & b\\
a & b\\
a & b b b b b b\\
a & b b b b b\\
a & b b\\
A Wider Entry & b\\
\end{longtable}
\end{document}
longtable
is notable in that it preserves the column widths over all pages of the table; however in order to achieve this it may take several runs of LaTeX so that wide entries encountered later in the table can affect the column widths in earlier pages.
Table notes
It is quite common to need footnote-like marks in a table referring to notes under the table. The threeparttable
package simplifies the markup for such tables, arranging that the notes are set in a block the same width as the table. Refer to the package documentation for full details, but we show a simple example here.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{threeparttable}
\begin{document}
\begin{table}
\begin{threeparttable}
\caption{An Example}
\begin{tabular}{ll}
An entry & 42\tnote{1}\\
Another entry & 24\tnote{2}\\
\end{tabular}
\begin{tablenotes}
\item [1] the first note.
\item [2] the second note.
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
Typesetting in narrow columns
The default line breaking settings assume relatively long lines to give some flexibility in choosing line breaks. The following example shows some possible approaches. The first table shows interword spacing stretched and TeX warns about Underfull lines. Using \raggedright
usually avoids this problem but may leave some lines ‘too ragged’. The \RaggedRight
command from the ragged2e
package is a compromise; it allows some raggedness in the line lengths, but will also hyphenate where necessary, as shown in the third table.
Note the use of \arraybackslash
here, which resets the definition of \\
so that it ends the table row.
An alternative technique, as shown in the fourth table, is to use a smaller font so that the columns are not so narrow relative to the text size.
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{ragged2e}
\begin{document}
\begin{table}
\begin{tabular}[t]{lp{3cm}}
One & A long text set in a narrow paragraph, with some more example text.\\
Two & A different long text set in a narrow paragraph, with some more hard to hyphenate words.
\end{tabular}%
\begin{tabular}[t]{l>{\raggedright\arraybackslash}p{3cm}}
One & A long text set in a narrow paragraph, with some more example text.\\
Two & A different long text set in a narrow paragraph, with some more hard to hyphenate words.
\end{tabular}%
\begin{tabular}[t]{l>{\RaggedRight}p{3cm}}
One & A long text set in a narrow paragraph, with some more example text.\\
Two & A different long text set in a narrow paragraph, with some more hard to hyphenate words.
\end{tabular}
\footnotesize
\begin{tabular}[t]{lp{3cm}}
One & A long text set in a narrow paragraph, with some more example text.\\
Two & A different long text set in a narrow paragraph, with some more hard to hyphenate words.
\end{tabular}
\end{table}
\end{document}
Defining new column types
As demonstrated in the main lesson, the array
package allows constructs such as >{\bfseries}c
to denote a bold centered column. It is often convenient to define a new column type to encapsulate such use, for example
\newcolumntype{B}{>{\bfseries}c}
would allow the use of B
in table preambles to specify a bold centered column.
Vertical tricks
Often, rather than making a cell span multiple rows it is better to instead have a single row in which some cells are split vertically by the use of nested tabular
environments.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lcc}
\toprule
Test & \begin{tabular}{@{}c@{}}A\\a\end{tabular} & \begin{tabular}{@{}c@{}}B\\b\end{tabular} \\
\midrule
Content & is & here \\
Content & is & here \\
Content & is & here \\
\bottomrule
\end{tabular}
\end{document}
Note that you can control vertical alignment by an optional argument to the tabular
; it supports the usage of t
, c
, or b
for top, centered, or bottom aligned respectively and is used like this:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lcc}
\toprule
Test & \begin{tabular}[b]{@{}c@{}}A\\a\end{tabular} & \begin{tabular}[t]{@{}c@{}}B\\b\end{tabular} \\
\midrule
Content & is & here \\
Content & is & here \\
Content & is & here \\
\bottomrule
\end{tabular}
\end{document}
Line spacing in tables
In the main lesson we demonstrated \addlinespace
from the booktabs
package, which is useful for adding extra space between specific lines.
There are two general parameters that control line spacing, \arraystretch
and \extrarowheight
(the latter from the array
package).
\renewcommand\arraystretch{1.5}
will increase the baseline spacing by 50%.
Often, especially when using \hline
, it is better just to increase the height of rows, without increasing their depth below the baseline. The following example demonstrates the \extrarowheight
parameter.
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\begin{document}
\begin{center}
\begin{tabular}{cc}
\hline
Square& $x^2$\\
\hline
Cube& $x^3$\\
\hline
\end{tabular}
\end{center}
\begin{center}
\setlength\extrarowheight{2pt}
\begin{tabular}{cc}
\hline
Square& $x^2$\\
\hline
Cube& $x^3$\\
\hline
\end{tabular}
\end{center}
\end{document}