51 lines
3.4 KiB
TeX

\chapter{Tables}
\label{sec:tables}
There are two fundamental environments for tables.
The first one, called \mono{table}, is responsible for integrating the table as a whole into the document.
Positioning is done the same way as it is with graphics.
The \code{latex}{\textbackslash caption} command is the same, as well.
At the table \emph{content}, the similarities end:
While graphics are embedded from external files and not interpreted by \LaTeX{},
the inner structure of tables has to be encoded explicitly.
This is done with the \mono{tabular} environment that expects a column definition as an obligatory parameter.
The column definition consists of one letter per column specifying its text alignment:
\mono{l} for left-justified, \mono{r} for right-justified, \mono{c} for centered.
Within the \mono{tabular} environment, the actual table content follows.
Table rows are separated by \code{latex}{\textbackslash \textbackslash}, just like line breaks, and cells by an \code{latex}{\&}.
For typographically pleasing tables, we recommend the \pkg{booktabs} package.\footnote{All commands previously mentioned also work without this package, but the result will look far less professional.}
This package brings along the commands \code{latex}{\textbackslash toprule}, \code{tex}{\textbackslash midrule} and \code{tex}{\textbackslash bottomrule} that draw appropriate horizontal lines above, within and below the table, respectively.
Vertical lines can be inserted as a vertical bar character (\mono{|}) in the column definition, but this is \emph{not} recommended.\footnote{The
whitespace within the table separates the columns clearly enough, additional lines just make for visual clutter.}
If you want to remove the additional whitespace that surrounds the columns by default (e.\,g., to the left of the first column and to the right of the last), you can add the string \mono{@\{\}} at the corresponding places within the column definition.
A complete table can then look like the one shown in \cref{lst:sample-table}.
\Example{lst:sample-table}{tables/table-example}{tables/table-example-renderable}{A complete table}
\paragraph{Excess length}
For tables exceeding one page or requiring line breaks within individual cells,
the \pkg{longtable} package can be used additionally (also supported by \pkg{booktabs}).
The \pkg{longtable} environment combines the \mono{table} and \mono{tabular} environments.
With it, you get the following basic structure:
\begin{samepage}
\codeblock{latex}{listings/tables/excess-length.tex}
\end{samepage}
\paragraph{Excess width}
If you want to present a very wide table instead, it is preferable on pages in portrait orientation to turn the table by 90\textdegree{}.
This can be done with the help of the \pkg{rotating} package.
The only difference between this and a normal table is that the \mono{table} environment gets replaced by a \mono{sidewaystable} environment.
Positioning and the \mono{tabular} stay unaffected.
\paragraph{More possibilities}
Of course, \LaTeX{} offers lots of additional features for sophisticated tables, e.\,g., row- or column-spanning cells.
For something like this\,---\,or just to save some typing\,---\,we recommend the \emph{Tables Generator}\textit{,}\footnote{\url{https://tablesgenerator.com/}} that allows you to click your tables together in \textsc{wysiwyg} style and provides you with \LaTeX{} code ready for copying into your document.