LaTeX tables: Advanced features
Continuing from the last time, let's review advanced possibilities of table control in LaTeX, at least those you might need the most while designing your tabletop game materials.
Contents
Multi-paged tables ↩
Longtable
If you got an especially long table, you might want to employ the functionality of the longtable package1. Its longtable environment is mostly similar to the tabular, but can be split between multiple pages while keeping the same column width between them.
By using \endfirsthead, \endhead, \endfoot, and \endlastfoot commands, you can specify headers and footers for each page of the table. The full summary of longtable commands is presented on page 9 of its documentation.
\onecolumn
\begin{longtable}{l|p{30em}}
Header for the first page & Paragraph \\
\hline\hline
\endfirsthead
Header for the next pages & Paragraph \\
\hline\hline
\endhead
\hline
Footer for all pages & Paragraph \\
\endfoot
\hline\hline
Footer for the last page & Paragraph \\
\endlastfoot
~ & \lipsum[1] \\
~ & \lipsum[2] \\
~ & \lipsum[3] \\
~ & \lipsum[4] \\
~ & \lipsum[5] \\
~ & \lipsum[6] \\
~ & \lipsum[7] \\
~ & \lipsum[8] \\
~ & \lipsum[9] \\
~ & \lipsum[10] \\
\end{longtable}
Supertabular
One of the downsides of longtable package, is it works only in one-column mode. To create multi-page tables in two-column mode, you can use a simpler supertabular package2 instead.
\tablefirsthead{%
\hline
Header for the first page & Paragraph \\
\hline}
\tablehead{%
\hline
Header for the next pages & Paragraph \\
\hline}
\tabletail{%
\hline
Footer for all pages & Paragraph \\
\hline}
\tablelasttail{%
\hline
Footer for the last page & Paragraph \\
\hline
}
\begin{supertabular}{l|p{10em}}
~ & \lipsum[64] \\
~ & \lipsum[65] \\
~ & \lipsum[66] \\
~ & \lipsum[67] \\
~ & \lipsum[68] \\
\end{supertabular}
Rotation ↩
The rotating3 package provides a sidewaystable and sidewaysfigure environments, that you can use to fit a wide table sideways on a separate page.
\begin{sidewaystable}
\caption{Basic table example}
\vspace{1ex}
\label{table:side}
\begin{tabular}{|r|c|l|p{20em}|}
\hline
right aligned column & center aligned column & left aligned column & paragraph column \\
~ & ~ & ~ & \lipsum[66] \\
\hline
\end{tabular}
\end{sidewaystable}
The following package options can be used to control which way the tables rotate: clockwise or counterclockwise for single-sided documents, and figuresright or figuresleft for twoside documents.
The package also provides other environments for rotated content:
\begin{rotate}{angle}- rotate for the arbitraryangle\begin{turn}{angle}- same, but leave space for the rotated box\begin{sideways}- rotate to-90degrees\turnbox{angle}{contents}- a macro version of therotateenvironment
You can find a set of examples for this package in the rotex4 document.
Styling ↩
Lines
The booktabs package5 provides horizontal lines that have better spacing than the default ones, but does not work well with vertical lines.
\toprule[width]- the line at the start of the table\bottomrule[width]- the line at the end of the table\midrule[width]- the line inside the table\cmidrule[width](trim){range}- where the optional trim argument can take the following values in any order:r- trim the right edger{width}- same, by specifiedwidthl- trim the left edgel{width}- same, by specifiedwidth
\specialrule{width}{abovespace}{belowspace}- custom line spacing\addlinespace{width}- add extra space between rows
\begin{table}[h!]
\centering
\caption{Default lines example}
\vspace{1ex}
\label{table:l1}
\begin{tabular}{rclp{5em}}
\hline
header & header & header & header \\
\hline
right & center & left & empty \\
aligned & aligned & aligned & space \\
\cline{4-4}
column & column & column & paragraph \newline cell with \newline line breaks \\
\hline
\end{tabular}
\end{table}
\begin{table}[h!]
\centering
\caption{Booktabs lines example}
\vspace{1ex}
\label{table:l2}
\begin{tabular}{rclp{5em}}
\toprule
header & header & header & header \\
\midrule
right & center & left & empty \\
aligned & aligned & aligned & space \\
\cmidrule(l){4-4}
column & column & column & paragraph \newline cell with \newline line breaks \\
\bottomrule
\end{tabular}
\end{table}
To draw table lines of varying thickness, use the boldline package6:
V{width}- a new column type that creates a vertical line of setwidth\hlineB{width}-hlineof setwidth\cline{range}{width}-clineof setwidth
\begin{tabular}{V{5}r|c|lV{5}V{5}p{5em}V{10}}
\hlineB{10}
right & center & left & empty \\
aligned & aligned & aligned & space \\
\clineB{4-4}{5}
column & column & column & paragraph \newline cell with \newline line breaks \\
\hlineB{10}
\end{tabular}
The hhline package7 focuses on interactions of horizontal and vertical lines. The \hhline command takes an argument similar to tabular preamble, with the following parameters:
=- column with double horizontal line-- column with single horizontal line~- column with no horizontal line|- vertical line cutting through a horizontal line:- vertical line which is broken by a double horizontal line#- double horizontal line segment between two vertical linest- top half of a double horizontal line segmentb- bottom half of a double horizontal line segment*-*{3}{==#}expands to==#==#==#, as in the*-form for the preamble.
\begin{tabular}{||cc||c|c||}
\hhline{|t:==:t:==:t|}
a&b&c&d\\
\hhline{|:==:|~|~||}
1&2&3&4\\
\hhline{#==#~|=#}
i&j&k&l\\
\hhline{||--||--||}
w&x&y&z\\
\hhline{|b:==:b:==:b|}
\end{tabular}
Alternating row colors
If you load the xcolor package8 with a table option (\usepackage[table]{xcolor}), you can set the alternating row colors for your tables.
The syntax is \rowcolors{startrow}{oddcolor}{evencolor}
📝 NOTE: This command should be executed before the tabular environment.
Similarly, you can use the \rowcolor[colormodel]{color}[leftoverhang][rightoverhangs] command to change the color of one specific row. Overhang arguments could be used to set how far the color overlaps the neighboring rows.
To set the color of a column, you can use the following command:
\columncolor[colormodel]{color}[leftoverhang][right-overhang].
It could either be called in a preamble (>{\columncolor{color}}), or added to a new column type (\newcolumntype{q}{>{\columncolor{color}}l}). Overhang arguments could be used to set how far the color overlaps the neighboring columns.
\rowcolors{3}{pink}{yellow}
\begin{tabular}{>{\columncolor{gray}}lp{3em}p{3em}p{3em}p{3em}}
\rowcolor{cyan} 1 & A & B & C & D \\
2 & a & b & c & d \\
3 & I & J & K & L \\
4 & i & j & k & l \\
5 & 6 & 7 & 8 & 9 \\
\end{tabular}
📝 NOTE: The details on colors can be found in LaTeX: text spacing and decoration post.
Better Tables
The makecell package9 is a collection of commands that can help you with table layout and get better looking tables.
Cell commands
\makecell[alignment]{contents}- multi-lined cell. The alignment argument defines both vertical (t,b,c) and horizontal alignment (r,l,c,p{width}, etc.). Starred version creates vertical\jotspaces around the cell (usually 3pt). Its style is controlled by following commands:\renewcommand\theadset{\renewcommand\arraystretch{1}\setlength\extrarowheight{0pt}}- spacing\renewcommand\cellalign{cc}- alignment\renewcommand\cellgape{\gape[t]}- vertical spaces\renewcommand\cellrotangle- the angle for rotated cells and column heads
\thead{contents}- table header cell. Starred version creates vertical\jotspaces around the cell (usually 3pt). Its style is controlled by following commands:\renewcommand\theadfont{\large\itshape}- font\renewcommand\theadset{\renewcommand\arraystretch{1}\setlength\extrarowheight{0pt}}- spacing\renewcommand\theadalign{cc}- alignment\renewcommand\theadgape{\gape}- vertical spaces\renewcommand\rotheadgape{}- same, for rotated headers
\rothead- header rotated by 90 degrees counterclockwise\setlength\rotheadsize{2em}- width of rotated tabular heads
\multirowcell{num}[vmove][halign]{contents}- multirow cell, similar tomultirowpackage10\multirowthead- same, for table headers
Spacing
\gape[t or b]{contents}- vertical spaces of\jotvalue (usually 3pt). Optional argumenttchanges height only, andb- depth only.\Gape[height][depth]{contents}- height and depth correction. Without optional arguments, works like\gape\setcellgapes[t or b]{3pt}- set the parameters for vertical spacing\makegapedcells- turn on vertical spacing (call\setcellgapesbefore)\nomakegapedcells- turn off vertical spacing
Diagonally divided cells
While makecell package contains the \diaghead command to create diagonally divided table header, the better result can be achieved by the diagbox package11 and its command of the same name.
\usepackage{makecell}
\usepackage{diagbox}
...
\renewcommand\theadfont{\large\itshape}
\setcellgapes{3pt}
\makegapedcells
\begin{tabularx}{\linewidth}{|c|X|}
\hline
\diagbox{\thead{Left}}{\thead{Right}} & \thead{Header} \\
\hline
\makecell{Multilined \\ cell text} & text \\
\hline
\makecell[l]{Left aligned \\ cell text} & text \\
\hline
\makecell[r]{Right aligned \\ cell text} & text \\
\hline
\makecell[b]{Bottom aligned \\ cell text} & text \\
\hline
\makecell*[{{p{3cm}}}]{Cell long text with predefined width} & text \\
\hline
\makecell[{{>{\parindent1em}p{3cm}}}]{Cell long...} & text \\
\hline
\end{tabularx}
Discuss this post on Reddit




