348 lines
7.2 KiB
Markdown
348 lines
7.2 KiB
Markdown
@slide(layout=chapter-slide)
|
||
|
||
@number
|
||
9
|
||
|
||
@title
|
||
Tables
|
||
|
||
|
||
@slide(layout=content-only)
|
||
|
||
@title
|
||
Basic structure <span class="emoji">🗒️</span>
|
||
|
||
@content
|
||
For typographically pleasing tables, we use the `booktabs` package.
|
||
|
||
``` {.hljs .lang-tex}
|
||
\usepackage{booktabs}
|
||
```
|
||
|
||
``` {.hljs .lang-tex}
|
||
\begin{table}[<position>]
|
||
\caption{<caption>}
|
||
\begin{tabular}{<column definition>}
|
||
% table content
|
||
\end{tabular}
|
||
\end{table}
|
||
```
|
||
|
||
Positioning works just like with graphics.
|
||
|
||
|
||
@slide(layout=content-and-preview)
|
||
|
||
@title
|
||
Column definitions
|
||
|
||
@content
|
||
``` {.hljs .lang-tex}
|
||
\begin{tabular}{lrcl}
|
||
% table content
|
||
\end{tabular}
|
||
```
|
||
|
||
<table>
|
||
<tr>
|
||
<th>letter</th>
|
||
<th>meaning</th>
|
||
</tr>
|
||
<tr class="fragment">
|
||
<td>l</td>
|
||
<td>left-justified column</td>
|
||
</tr>
|
||
<tr class="fragment">
|
||
<td>c</td>
|
||
<td>centred column</td>
|
||
</tr>
|
||
<tr class="fragment">
|
||
<td>r</td>
|
||
<td>right-justified column</td>
|
||
</tr>
|
||
</table>
|
||
|
||
++ Vertical separator lines and double horizontal lines are frowned upon by typographers and should be avoided.
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
|
||
@slide(layout=content-and-preview)
|
||
|
||
@title
|
||
Table content
|
||
|
||
@content
|
||
``` {.hljs .lang-tex}
|
||
\begin{tabular}{lll}
|
||
\toprule
|
||
Column 1 & Column 2 & Column 3 \\
|
||
\midrule
|
||
Content a & Content b & Content c \\
|
||
Content e & Content f & Content g \\
|
||
Content i & Content j & Content k \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
```
|
||
* ++ Columns are separated by ’`&`’.
|
||
* ++ Rows are ended by ‘`\\`’.
|
||
* ++ `\toprule`, `\midrule` and `\bottomrule` structure the table.
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
|
||
@slide(layout=wide-content)
|
||
|
||
@title
|
||
The entire table
|
||
|
||
@content
|
||
``` {.hljs .lang-tex}
|
||
\begin{table}[h]
|
||
\caption{Well-known programming languages}
|
||
\begin{tabular}{lrcl}
|
||
\toprule
|
||
Language & Author & Year & File extension \\
|
||
\midrule
|
||
C++ & Bjarne Stroustrup & 1985 & .cpp \\
|
||
Java & James Gosling & 1998 & .java \\
|
||
Python & Guido van Rossum & 1991 & .py \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
\end{table}
|
||
```
|
||
|
||
<p data-category="Note">`@{}` to the left and right of a column definition removes the padding of the corresponding column.</p>
|
||
|
||
<p data-category="Example">`\begin{tabular}{@{}lrcl@{}}` limits the row separators to the width of the table content</p>
|
||
|
||
|
||
@slide(layout=content-only)
|
||
|
||
@title
|
||
More comfort
|
||
|
||
@content
|
||
The [Tables Generator](https://tablesgenerator.com/) is a wonderful tool to quickly create tables of different formats.
|
||
|
||
[](https://tablesgenerator.com/)
|
||
|
||
|
||
@slide(layout=task)
|
||
|
||
@task-number
|
||
9
|
||
|
||
@title
|
||
Typesetting tables
|
||
|
||
@content
|
||
* The list in file `exercises/tables/tables.tex` stores information on a few modules of the WIAI faculty.
|
||
* Transform the list into a **table**.
|
||
* The table shall have colums for the *name*, the *abbreviation* (Kürzel) and the *semester* of the lectures.
|
||
* Add a **column** with **center-aligned text** on the left side of the table in order to **number** the lectures.
|
||
* Add a **caption** for the table.
|
||
|
||
|
||
|
||
@slide(layout=extra-content-only)
|
||
|
||
@title
|
||
Particularly long tables
|
||
|
||
@content
|
||
Tables that exceed one page are simply cut off by `tabular`. A solution is offered by the `longtable` package:
|
||
|
||
``` {.hljs .lang-tex}
|
||
\usepackage{longtable}
|
||
```
|
||
|
||
``` {.hljs .lang-tex}
|
||
\begin{longtable}{<column definition>}
|
||
\caption{<caption>}
|
||
\label{<label>}
|
||
% table content
|
||
\end{longtable}
|
||
```
|
||
|
||
|
||
@slide(layout=extra-content-only)
|
||
|
||
@title
|
||
Notes on `longtable`
|
||
|
||
@content
|
||
``` {.hljs .lang-tex}
|
||
\begin{longtable}{<column definition>}
|
||
\caption{<caption>}
|
||
\label{<label>}
|
||
% table content
|
||
\end{longtable}
|
||
```
|
||
* The `longtable` environment merges the `tabular` and `table` environments.
|
||
* Caption and label can therefore be inserted directly under the table content.
|
||
* As long as the `booktabs` package is used, its features are also provided in `longtable`.
|
||
|
||
|
||
@slide(layout=extra-content-only)
|
||
|
||
@title
|
||
Particularly wide tables
|
||
|
||
@content
|
||
If you need a table to be wider than a page, you can display it in landscape orientation:
|
||
|
||
``` {.hljs .lang-tex}
|
||
\usepackage{rotating}
|
||
```
|
||
|
||
``` {.hljs .lang-tex}
|
||
\begin{sidewaystable}[<position>]
|
||
\begin{tabular}{<column def.>}
|
||
% table content
|
||
\end{tabular}
|
||
\end{sidewaystable}
|
||
```
|
||
|
||
@slide(layout=extra-content-and-preview)
|
||
|
||
@title
|
||
Colouring a table
|
||
|
||
@content
|
||
```{.hljs .lang-tex}
|
||
\usepackage[table]{xcolor}
|
||
```
|
||
|
||
```{.hljs .lang-tex}
|
||
\begin{tabular}{lll}
|
||
\toprule
|
||
A & \cellcolor{yellow}yellow & cell \\
|
||
A & \cellcolor{yellow!25}lighter yellow & cell \\
|
||
\rowcolor[HTML]{FFDE21}
|
||
A & yellow & row \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
```
|
||
|
||
* Use `\cellcolor` to color individual cells.
|
||
* Use `\rowcolor` to color entire rows.
|
||
* Color can be defined by name, percentage (e.g. `yellow!25`), or hex (`[HTML]{...}`).
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
@slide(layout=extra-content-and-preview)
|
||
|
||
@title
|
||
Merge cells
|
||
|
||
@content
|
||
```{.hljs .lang-tex}
|
||
\begin{tabular}{lll}
|
||
\toprule
|
||
Animal & Food & Size \\
|
||
\midrule
|
||
horse & hay & large \\
|
||
frog & flies & small \\
|
||
fooboar & \multicolumn{2}{c}{unknown} \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
```
|
||
* `\multicolumn{2}{c}{...}` combines two columns into one.
|
||
* The second argument specifies the alignment (and borders).
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
@slide(layout=extra-content-and-preview)
|
||
|
||
@title
|
||
Line breaks in a cell
|
||
|
||
@content
|
||
```{.hljs .lang-tex}
|
||
\usepackage{makecell}
|
||
```
|
||
|
||
```{.hljs .lang-tex}
|
||
\begin{tabular}{ll}
|
||
\toprule
|
||
Name & Description \\
|
||
\midrule
|
||
Cat & \makecell[l]{small \\ likes to sleep} \\
|
||
Dog & \makecell[l]{loyal \\ needs walks} \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
```
|
||
|
||
* By default, `\makecell` centers its contents.
|
||
* Use `\makecell[l]{content}` to left-align the content.
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
@slide(layout=extra-content-and-preview)
|
||
|
||
@title
|
||
Fixed column width
|
||
|
||
@content
|
||
The `p{4cm}` column automatically wraps text within 4cm.
|
||
|
||
```{.hljs .lang-tex}
|
||
\begin{tabular}{lp{4cm}}
|
||
\toprule
|
||
Item & Description \\
|
||
\midrule
|
||
Chair & Four legs, one surface \\
|
||
Table & Like a chair but different \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
```
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
|
||
@slide(layout=extra-content-and-preview)
|
||
|
||
@title
|
||
An advanced example
|
||
|
||
@content
|
||
``` {.hljs .lang-tex}
|
||
\begin{table}[h]
|
||
\begin{tabular}{llr}
|
||
\toprule
|
||
Structure & \multicolumn{2}{l}{Access time complexity} \\ \cmidrule(r){2-3}
|
||
& Average & Worst \\
|
||
\midrule
|
||
Stack & $\mathcal{O}(n)$ & $\mathcal{O}(n)$ \\
|
||
Binary tree & $\mathcal{O}(log(n))$ & $\mathcal{O}(n)$ \\
|
||
AVL tree & $\mathcal{O}(log(n))$ & $\mathcal{O}(log(n))$ \\
|
||
\bottomrule
|
||
\end{tabular}
|
||
\end{table}
|
||
```
|
||
|
||
@preview
|
||
{.thin-padding}
|
||
|
||
|
||
@slide(layout=extra-content-only)
|
||
|
||
@title
|
||
Further resources
|
||
|
||
@content
|
||
* For tips on how to format tables — including what to align left, how to label columns and when to use borders —
|
||
refer to the APA style guide:
|
||
|
||
[<span class="emoji">🔗</span> Table guidelines](https://apastyle.apa.org/style-grammar-guidelines/tables-figures/tables)
|
||
|
||
* The `booktabs` package provides commands for high-quality tables:
|
||
|
||
[<span class="emoji">🔗</span> Booktabs documentation](https://ctan.org/pkg/booktabs/) |