Vladar's Blog

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:

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.

\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}

table-booktabs

To draw table lines of varying thickness, use the boldline package6:

\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}

table-boldline

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:

\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}

table-hhline

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}

table-rowcolors

📝 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

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}

table-makecell

Discuss this post on Reddit


  1. https://www.ctan.org/pkg/longtable

  2. https://www.ctan.org/pkg/supertabular

  3. https://ctan.org/pkg/rotating

  4. http://mirrors.ctan.org/macros/latex/required/graphics/rotex.pdf

  5. https://www.ctan.org/pkg/booktabs/

  6. https://www.ctan.org/pkg/boldline

  7. https://www.ctan.org/pkg/hhline

  8. https://www.ctan.org/pkg/xcolor

  9. https://www.ctan.org/pkg/makecell

  10. https://www.ctan.org/pkg/multirow

  11. https://www.ctan.org/pkg/diagbox

#gamedev #guide #latex #software #ttrpg