diff --git a/en/lesson-08.md b/en/lesson-08.md index 93cd6e02..1e86c5f2 100644 --- a/en/lesson-08.md +++ b/en/lesson-08.md @@ -10,31 +10,51 @@ you load the `array` package, which adds more functionality to LaTeX tables, and which is not built into the LaTeX kernel only for historic reasons. So put the following in your preamble and we're good to go: + ```latex \usepackage{array} ``` + In order to typeset a `tabular` we have to tell LaTeX how many columns will be needed and how they should be aligned. This is done in a mandatory argument – often referred to as the table preamble – to the `tabular` -environment, in which you specify the columns by using single letter names. The -available column types are: +environment, in which you specify the columns by using single-letter names, +called preamble-tokens. The available column types are: | type | description | | --- |:-- | | `l` | left aligned column | -| `c` | centred column | +| `c` | centered column | | `r` | right aligned column | | `p{width}` | a column with fixed width `width`, the text will be automatically line wrapped and fully justified | -| `m{width}` | like `p`, but vertically centred compared to the rest of the row | +| `m{width}` | like `p`, but vertically centered compared to the rest of the row | | `b{width}` | like `p`, but bottom aligned | | `w{align}{width}` | prints the contents with a fixed `width`, silently overprinting if things get larger. You can choose the horizontal alignment using `l`, `c`, or `r`. | | `W{align}{width}` | like `w`, but this will issue an overfull box warning if things get too wide. | +In addition, a few other preamble-tokens are available which don't define a +column but might be useful as well: + + + +| type | description | +| --- | :-- | +| `*{num}{string}` | repeats `string` for `num` times in the preamble. With this you can define multiple identical columns. | +| `>{decl}` | this will put `decl` before the contents of every cell in the following column (this is useful, e.g., to set a different font for this column) | +| `<{decl}` | this will put `decl` after the contents of each cell in the previous column | +| `|` | add a vertical rule | +| `@{decl}` | replace the space between two columns with `decl` | +| `!{decl}` | add `decl` in the center of the existing space | + +These two tables list all the available column types from LaTeX and the `array` +package. A few more useful column types, from different packages, are presented +in the [further details page](en/more-08) for this lesson. + The columns `l`, `c`, and `r` will have the natural width of the widest cell. -Each column has to be declared, so if you want three centred columns, you'd use +Each column has to be declared, so if you want three centered columns, you'd use `ccc` in the table preamble. Spaces are ignored, so `c c c` is the same. In a table body columns are separated using an ampersand `&` and a new row is @@ -44,6 +64,7 @@ We got everything we need for our first little table. In the following code the `&` and `\\` are aligned: this isn't necessary in LaTeX, but helps reading the source. + ```latex \documentclass{article} \usepackage{array} @@ -57,90 +78,63 @@ source. \end{tabular} \end{document} ``` + +If a table column contains a lot of text you will have issues to get that +right with only `l`, `c`, and `r`. See what happens in the following example: -## Adding lines - -A word of advice prior to introducing lines; lines should be used really -sparsely in tables, and normally vertical ones often look unprofessional. In fact, -for professional tables you shouldn't use any of the standard lines; instead you -should skip this section and read about -[`booktabs`](#tables-in-printing-quality-with-the-booktabs-package). - -Nevertheless, LaTeX has got you covered if you need lines. Vertical lines are part of -the column specification and hence should go into the table preamble. To add a line -between two columns just add `|` between their specification. Please note that -vertical lines are only inserted if the cell to their left exists (except for a -vertical line left of the first column). Note how in the following example the -rightmost line will not appear in the last row. - + ```latex \documentclass{article} \usepackage{array} \begin{document} -\begin{tabular}{|l|ll|} - Animal & Food & Size \\ - dog & meat & medium \\ - horse & hay & large \\ - frog & flies & small \\ - microbe & ??? +\begin{tabular}{cl} + Animal & Description \\ + dog & The dog is a member of the genus Canis, which forms part of the + wolf-like canids, and is the most widely abundant terrestrial + carnivore. \\ + cat & The cat is a domestic species of small carnivorous mammal. It is the + only domesticated species in the family Felidae and is often referred + to as the domestic cat to distinguish it from the wild members of the + family. \\ \end{tabular} \end{document} ``` + -Horizontal lines belong to a `tabular`'s body. In LaTeX there are two different -macros to add them, the first is `\hline` adding a line across the full width of -a `tabular`, the second is `\cline` which can be used to draw lines covering -only a specific range of columns. - -`\cline` takes a mandatory argument that specifies the columns affected. It has -to be a range with a hyphen (`-`) as a separator between the first and the last -column number. You can specify multiple `\cline`s in the same row. Both an -`\hline` and a `\cline` have to be the first content of a new row (so directly -following `\\` or another `\hline` or `\cline`). - +The issue is that the `l` type column typesets its contents in a single row at +its natural width, taking as much space as it takes, even if there is a page +border in the way. To overcome this you can use the `p` column. This one +typesets its contents as paragraphs with the width you specify as an argument +and vertically aligns them at the top – which you'll want most of the +time. Compare aboves outcome to the following: + ```latex \documentclass{article} \usepackage{array} \begin{document} -\begin{tabular}{lll} - \hline - \hline - Animal & Food & Size \\ - \hline - dog & meat & medium \\ - \cline{1-2} - horse & hay & large \\ - \cline{1-1}\cline{3-3} - frog & flies & small \\ - \hline - \hline +\begin{tabular}{cp{9cm}} + Animal & Description \\ + dog & The dog is a member of the genus Canis, which forms part of the + wolf-like canids, and is the most widely abundant terrestrial + carnivore. \\ + cat & The cat is a domestic species of small carnivorous mammal. It is the + only domesticated species in the family Felidae and is often referred + to as the domestic cat to distinguish it from the wild members of the + family. \\ \end{tabular} \end{document} ``` + - -## Shortcuts and other preamble content - -The list of possible types in the table preamble above only showed the column -alignments available, but LaTeX has more things to offer in the preamble. What -is still missing is a way to change the horizontal space between two columns to -something arbitrary. This can be done with - -type | description ---- | :-- -`@{decl}` | replace the space between two columns with `decl` -`!{decl}` | add `decl` in the centre of the existing space - -In case you're wondering now, by default each column has a horizontal space of -the length `\tabcolsep` padded on both sides, resulting in a total of -`2\tabcolsep` between columns – one from each column – and a single -`\tabcolsep` on both outer ends. If we want to put a colon between the first -two columns and change the space between the latter two to `1cm` we could do so -by using +If your table has many columns of the same type it'll be cumbersome to put that +many column definitions in the preamble. Luckily you can have an easier life in +that case by using `*{num}{string}`, which repeats the `string` for `num` times. +So `*{6}{c}` results in `cccccc`. To show you that it works here is the first +table of this lesson with the newly learned syntax: ```latex @@ -148,7 +142,7 @@ by using \usepackage{array} \begin{document} -\begin{tabular}{l !{:} l @{\hspace{1cm}} l} +\begin{tabular}{*{3}{l}} Animal & Food & Size \\ dog & meat & medium \\ horse & hay & large \\ @@ -158,181 +152,216 @@ by using ``` -(We'll see `\hspace` [again shortly](lesson-11); you might guess that it adds a -horizontal space.) - -In addition to all the aforementioned preamble content, we can use a few other -things as well - - +## Adding rules -type | description ---- | :-- -`*{num}{string}` | repeats `string` for `num` times in the preamble. With this you can define multiple identical columns. -`>{decl}` | this will put `decl` before the contents of every cell in the following column (this is useful, e.g., to set a different font for this column) -`<{decl}` | this will put `decl` after the contents of each cell in the previous column +A word of advice prior to introducing rules; lines should be used really +sparsely in tables, and normally vertical ones look unprofessional. In fact, +for professional tables you shouldn't use any of the standard lines; instead you +should get familiar with the facilities of the `booktabs` package, which is why +it is covered here first – for the sake of completeness the standard +lines are shown in the more-info page. -The following example uses an italic font for the first column. Remember our -example with putting a colon in between the first two columns? How about we also -append that colon to the first column, so that things don't look as clunky. +`booktabs` provides four different types of lines. Each of those macros has to +be used as the first thing in a row or following another rule, else you'll get a +nasty error. Three rule types and their macros are: `\toprule`, `\midrule`, and +`\bottomrule`. From their names the intended place of use should be pretty +clear: ```latex \documentclass{article} -\usepackage{array} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{>{\itshape}l<{:} *{2}{l}} +\begin{tabular}{lll} + \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](lesson-11). - -## Merging cells - -In LaTeX you can merge cells horizontally pretty easily. This is done by using -`\multicolumn{num}{align}{content}`. The first argument tells LaTeX how -many columns should be merged, the second argument is the cell type, which can -be anything legal in the preamble but _only a single column type_. +The fourth rule-macro provided by `booktabs` is `\cmidrule`. It can be used to +draw a rule that doesn't span the entire width of the table but only a specified +column range. A column range is entered by a number followed by a hyphen and +then another number. Even if you only want to draw the rule only for a single +column you need to specify that as a range (but with both numbers matching). ```latex \documentclass{article} -\usepackage{array} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{*{3}{l}} - Animal & Food & Size \\ - dog & meat & medium \\ - horse & hay & large \\ - frog & flies & small \\ - microbe & \multicolumn{2}{c}{???} \\ +\begin{tabular}{lll} + \toprule + Animal & Food & Size \\ + \midrule + dog & meat & medium \\ + \cmidrule{1-2} + horse & hay & large \\ + \cmidrule{1-1} + \cmidrule{3-3} + frog & flies & small \\ + \bottomrule \end{tabular} \end{document} ``` -Note that you have to specify vertical rules you want to apply to the right of -the `\multicolumn` in the `align` argument, e.g., `\multicolumn{2}{c|}{stuff}` -– but remember, don't use vertical rules. - - -## Formal tables with the `booktabs` package - -In this section we will briefly introduce the `booktabs` package, which aids -the author in the creation of beautifully typeset tables. "Beautiful" means -in this context that the table is well readable. - -Let us start with a simple table: - +There is another handy feature of `\cmidrule`, you can shorten it on either end +with an optional argument enclosed in parenthesis: ```latex \documentclass{article} -\usepackage{array} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{@{} lll @{}} +\begin{tabular}{lll} + \toprule Animal & Food & Size \\ + \midrule dog & meat & medium \\ + \cmidrule{1-2} horse & hay & large \\ + \cmidrule(r){1-1} + \cmidrule(rl){2-2} + \cmidrule(l){3-3} frog & flies & small \\ + \bottomrule \end{tabular} \end{document} ``` -Using the `\toprule`, `\midrule` and `\bottomrule` commands from the `booktabs` package we can add some -horizontal lines. +You may have guessed that `r` and `l` mean the rule is shortened on its *r*ight +and *l*eft end, respectively. + +Sometimes a rule would be too much of a separation for two rows but to get +across the meaning more clearly you want to separate them by some means. In this +case you can use `\addlinespace` to insert a small skip. ```latex \documentclass{article} \usepackage{array} -\usepackage{booktabs} \begin{document} -\begin{tabular}{@{} lll @{}} \toprule - Animal & Food & Size \\ \midrule - dog & meat & medium \\ - horse & hay & large \\ - frog & flies & small \\ \bottomrule +\begin{tabular}{cp{9cm}} + \toprule + Animal & Description \\ + \midrule + dog & The dog is a member of the genus Canis, which forms part of the + wolf-like canids, and is the most widely abundant terrestrial + carnivore. \\ + \addlinespace + cat & The cat is a domestic species of small carnivorous mammal. It is the + only domesticated species in the family Felidae and is often referred + to as the domestic cat to distinguish it from the wild members of the + family. \\ + \bottomrule \end{tabular} \end{document} ``` -By default `\toprule` and `\bottomrule` are slightly thicker than the -`\midrule`. The thickness of the lines can be adjusted via the optional -parameter of these commands: + +## Merging cells + +In LaTeX you can merge cells horizontally by using the `\multicolumn` macro. It +has to be used as the first thing in a cell. `\multicolumn` takes three +arguments: + +1. The number of cells which should be merged +2. The alignment of the merged cell +3. The contents of the merged cell + +The alignment can contain anything legal in a `tabular`'s preamble, but _only a +single column type_. ```latex \documentclass{article} -\usepackage{array} -\usepackage{booktabs} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{@{} lll @{}} \toprule[2pt] - Animal & Food & Size \\ \midrule[1pt] +\begin{tabular}{lll} + \toprule + Animal & Food & Size \\ + \midrule dog & meat & medium \\ horse & hay & large \\ - frog & flies & small \\ \bottomrule[2pt] + frog & flies & small \\ + fuath & \multicolumn{2}{c}{unknown} \\ + \bottomrule \end{tabular} \end{document} ``` -Even more, one can set a left and right indent for the `\midrule`, so -that this line is a little bit shorter than the `\toprule` and -`\bottomrule` of the table. +You can also use `\multicolumn` on a single cell to prevent whatever you defined +in the table preamble for the current column from applying. The following uses +this to center the table's head row: ```latex \documentclass{article} -\usepackage{array} -\usepackage{booktabs} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{@{} lll@{}} \toprule[2pt] - Animal & Food & Size \\ \cmidrule[1pt](rl){1-3} +\begin{tabular}{lll} + \toprule + \multicolumn{1}{c}{Animal} & \multicolumn{1}{c}{Food} & \multicolumn{1}{c}{Size} \\ + \midrule dog & meat & medium \\ horse & hay & large \\ - frog & flies & small \\ \bottomrule[2pt] + frog & flies & small \\ + fuath & \multicolumn{2}{c}{unknown} \\ + \bottomrule \end{tabular} \end{document} ``` -The package also provides commands to increase or decrease the height -of a row. In the following example we increase the height of the -last row by 0.5ex, where `ex` is a unit that depends on the current font. +Merging cells vertically isn't supported by LaTeX as tables are build row after +row. Luckily this limitation isn't preventing us from creating readable tables, +as most of the time it suffices to just leave cells empty to give the reader the +correct idea of what was meant. ```latex \documentclass{article} -\usepackage{array} -\usepackage{booktabs} +\usepackage{array, booktabs} \begin{document} -\begin{tabular}{@{} lll @{}} \toprule[2pt] - Animal & Food & Size \\ \cmidrule[1pt](rl){1-3} - dog & meat & medium \\ - horse & hay & large \\ \addlinespace[0.5ex] - frog & flies & small \\ \bottomrule[2pt] +\begin{tabular}{lll} + \toprule + Group & Animal & Size \\ + \midrule + herbivore & horse & large \\ + & deer & medium \\ + & rabbit & small \\ + \addlinespace + carnivore & dog & medium \\ + & cat & small \\ + & lion & large \\ + \addlinespace + omnivore & crow & small \\ + & bear & large \\ + & pig & medium \\ + \bottomrule \end{tabular} \end{document} ``` + ## Exercises Use the simple table example to start experimenting with tables. Try out diff --git a/en/more-08.md b/en/more-08.md index a554ec31..09713878 100644 --- a/en/more-08.md +++ b/en/more-08.md @@ -2,6 +2,194 @@ title: "More on: Tables" --- + +## 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 the respective column, you can use these to add macros which affect the looks +of a column. For instance, if you want to italicize the first column and put a +colon after it, you can do the following: + + +```latex +\documentclass{article} +\usepackage{array} + +\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](lesson-11). + +You might find this handy, but now you want the first cell not to be affected +because it's the table head. Here `\multicolumn` comes in handy. Remember that +it can be used to change a single cell's alignment as well? In fact, if you use +`\multicolumn` you ignore whatever has been defined for these columns in the +preamble. So you can use + + +```latex +\documentclass{article} +\usepackage{array} + +\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 ends 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`: + + +```latex +\documentclass{article} +\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: + + +```latex +\documentclass{article} +\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](lesson-11); you might guess that it adds a +horizontal space.) + +The `!` preamble token does something pretty similar. The difference is, that it +_adds_ its argument to the space in the center between two columns. + + +```latex +\documentclass{article} +\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 + +Though you _don't_ want to use vertical rules, sometimes you just have to. LaTeX +has still got you covered. + + +```latex +\documentclass{article} +\usepackage{array} + +\begin{document} +\begin{tabular}{l|ll} + Animal & Food & Size \\ + 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 pretty +horizontal rules provided by `booktabs`. You can use the horizontal rules +provided by LaTeX, those are `\hline` (equaling `\toprule`, `\midrule`, and +`\bottomrule`) and `\cline` (which behaves like `\cmidrule`). + +## 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 thickness in +braces after `r` or `l`. + + +```latex +\documentclass{article} +\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 of columns The alignment of numbers in tables can be handled by the column type `S` @@ -28,11 +216,12 @@ A simple example with two aligned numeric columns would be: \end{document} ``` +Note that any non-numeric cell must be "protected" by enclosing it with braces. + The package provides many possibilities for formatting the numbers in different ways; see the [package documentation](https://texdoc.net/pkg/siunitx). - ## Specifying the total table width The width of a `tabular` environment is automatically determined based @@ -186,12 +375,13 @@ 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 refering to +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](https://texdoc.net/pkg/threeparttable) for full details, but we show a simple example here. + ```latex \documentclass{article} \usepackage{array,threeparttable} @@ -277,6 +467,54 @@ would allow the use of `B` in tabular 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. + + +```latex +\documentclass{article} +\usepackage{array} + +\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, and +bottom aligned respectively and is used like this: + + +```latex +\documentclass{article} +\usepackage{array} + +\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`