Move source code listings chapter after graphics and tables.

This commit is contained in:
Kremitzl 2022-05-22 10:44:06 +02:00
parent aa15f951a2
commit 4073b4992b
7 changed files with 950 additions and 950 deletions

@ -1 +1 @@
Subproject commit 24119fc714d85a572a09660a5a11953d96ea9a2d
Subproject commit 34f46b2328daf35dbff58bddda42657b70a9167b

View File

@ -1,292 +1,110 @@
@slide(layout=chapter-slide)
@title
Quelltext-Listings
Grafiken
@slide(layout=content-only)
@title
Unsere Optionen
Grafiken abbilden <span class="emoji">🖼️</span>
@content
Es gibt zwei verbreitete Wege, Quelltext in LaTeX-Dokumenten zu zeigen.
* ++ **`minted`** erstellt **sehr hübsche** Quelltext-Listings, aber es benötigt **etwas mehr Konfiguration** und funktioniert nicht auf allen Betriebssystemen zuverlässig.
* ++ **`listings`** ist eine **einfach zu nutzende** (aber nicht so hübsche) Alternative für diejenigen, die Probleme mit `minted` haben.
++ Die Befehle beider Pakete sind sehr ähnlich.
@slide(layout=content-and-preview)
@title
Installation <span class="emoji">🖥️</span>
@content
`minted` benötigt die Programmiersprache Python. Nach der **[Installation von Python](https://www.python.org/)** kann das zugehörige Paket **[`Pygments`](http://pygments.org/)** in der Eingabeaufforderung installiert werden durch:
``` {.lang-sh .hljs}
pip install Pygments
```
Anschließend muss das Paket `minted` (oder `listings`) nur noch eingebunden werden:
Um Grafiken darzustellen, muss das Paket `graphicx` eingebunden werden.
``` {.lang-tex .hljs}
\usepackage{minted}
\usepackage{listings}
\begin{figure}
\includegraphics{<dateipfad>}
\caption[<kurztitel (abbildungsver-
zeichnis)>]{<bildunterschrift>}
\end{figure}
```
@preview
![](svg/chapter-10/minted-overview-crop.svg){ .thin-padding }
Bildgröße angeben:
@slide(layout=content-only)
@title
Anpassungen beim Kompilieren
@content
<div class="box warning">
Um Dokumente mit Quelltext-Listings kompilieren zu können, muss die Flag `--shell-escape` an den Compiler übergeben werden.<br />
<strong>Dies ist nur bei vertrauenswürdigen Dokumenten empfehlenswert.</strong>
</div>
In TeXstudio muss diese unter **`Optionen → TeXstudio konfigurieren → Befehle`** (`TeXstudio → Einstellungen` unter macOS) bei PdfLaTeX vor `%.tex` ergänzt werden:
<code>pdflatex -synctex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex</code>
``` {.lang-tex .hljs}
\includegraphics[width=0.5\textwidth,
height=5cm]{<dateipfad>}
```
@slide(layout=content-only)
@title
Ein Hinweis zu unserem Skript
Positionierung auf der Seite
@content
Unsere Übungsmaterialien erlauben es, zwischen `minted` und `listings` zu wählen.
Von Haus aus nutzen wir `listings`.
Um `minted` zu aktivieren muss eine Datei namens `listings-mode.tex` mit dem folgenden Inhalt im Wurzelverzeichnis unseres Projekts erstellt werden:
``` {.lang-tex .hljs}
\newcommand\listingsmode{minted}
\begin{figure}[<positionskürzel>]
```
Kompiliert erneut und die Listings sollten durch `minted` erstellt worden sein.
LaTeX positioniert Grafiken automatisch. Durch Kürzel können wir unsere Präferenzen (auch in Kombination) angeben.
@slide(layout=content-and-preview-with-category)
<table>
<tr>
<th style="min-width:4em">Kürzel</th>
<th>Position</th>
</tr>
<tr class="fragment">
<td>h</td>
<td>hier, wenn es dir recht ist</td>
</tr>
<tr class="fragment">
<td>t</td>
<td>oberer Seitenrand (top)</td>
</tr>
<tr class="fragment">
<td>b</td>
<td>unterer Seitenrand (bottom)</td>
</tr>
<tr class="fragment">
<td>p</td>
<td>auf einer eigenen Seite (page)</td>
</tr>
<tr class="fragment">
<td>H</td>
<td>Hier, verdammt noch mal! <br />(benötigt Paket `float`)</td>
</tr>
</table>
@category
minted
@slide(layout=content-only)
@title
Code im LaTeX-Quelltext
Zentrierte Anordnung
@content
++ In einer eigenen Umgebung:
``` {.lang-tex .hljs .fragment}
\section*{Haskell-Magie}
Quadrate aller geraden % …
\begin{minted}{haskell}
[x^2 | x <- [1..200], even x]
\end{minted}
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\begin{center}
\includegraphics{<dateipfad>}
\end{center}
\end{figure}
```
++ Inline im Quelltext:
``` {.lang-tex .hljs .fragment}
\section*{Ein HTML-Beispiel}
Eine Überschrift wird so ausgezeichnet:
\mint{html}|<h2>LaTeX im Studium</h2>|.
```
@preview
![](svg/chapter-10/minted-haskell-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
minted
@title
Externer Code
@content
Um Redundanz zu vermeiden, ist es manchmal praktisch, den Quelltext direkt aus der Quelldatei einzulesen.
Alternativ:
``` {.lang-tex .hljs}
\section*{Einfache Java-Anwendung}
\inputminted{java}{Test.java}
\begin{figure}[<position>]
\centering
\includegraphics{<dateipfad>}
\end{figure}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/minted-java-crop.svg)
@slide(layout=content-only-with-category)
@category
minted
@title
Themen und Stile
@content
Durch optionale Parameter können Zeilennummerierung, Umbrüche und Farben ausgewählt werden. Außerdem sind zahlreiche Themes verfügbar.
``` {.lang-tex .hljs}
\usemintedstyle{monokai}
\begin{minted}[
linenos=true,
breaklines=true,
]{javascript}
% ...
\end{minted}
```
<span class="emoji">🔗</span> **[Einführung](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted)** und **[offizielle Dokumentation](https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf)**
@slide(layout=content-and-preview-with-category)
@category
listings
@title
Code im LaTeX-Quelltext
@content
In einer eigenen Umgebung:
``` {.lang-tex .hljs}
\section*{Haskell-Magie}
Quadrate aller geraden % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
```
Das Paket `listings` stellt keinen Befehl für inline-Listings zur Verfügung.
@preview
![](svg/chapter-10/listings-haskell-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
listings
@title
Externer Code
@content
Praktischerweise bietet auch `listings` einen Befehl zum Einfügen externen Codes:
``` {.lang-tex .hljs}
\section*{Einfache Java-Anwendung}
\lstinputlisting[language=Java]{Test.java}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/listings-java-crop.svg)
@slide(layout=content-only-with-category)
@category
listings
@title
Themen und Stile
@content
`listings` stellt keine eigenen Themes zur Verfügung. Dies kann aber durch die vielen Konfigurationsoptionen ausgeglichen werden.
``` {.lang-tex .hljs}
\begin{lstlisting}[
language=Java,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
keywordstyle=\color{ForestGreen},
commentstyle=\color{DarkGray},
literate={ö}{{\"o}}1
]
% …
\end{lstlisting}
```
Für die Erstellung eigener Themes ist der Befehl `\lstset` nützlich.
@slide(layout=content-only-with-category)
@category
listings
@title
Weitere Materialien
@content
* [Einführung](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide) in das Paket
* offizielle [Dokumentation](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide)
* das Paket [`xcolor`](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX) für Farben
* zwei [Themes](https://github.com/jez/latex-solarized) im solarized-Stil für `listings`
@slide(layout=task)
@task-number
10
11
@title
`minted`
Grafiken einfügen
@content
* Im Ordner `exercises/source-code-listings` findet ihr eine Datei namens `Source.java`.
* **Bindet** den Java-Quelltext in der Datei `source-code-listings.tex` **ein**. (Beachtet, dass der Dateipfad relativ zu `main.tex` ist.)
* Aktiviert das **Syntax-Highlighting** durch Angabe der Sprache Java.
* **Nummeriert** die Zeilen und aktiviert **Umbrüche**.
* Nutzt das **Theme** `native`.
* Ändert die **Hintergrundfarbe** des Themes zu Dunkelblau.
* Bindet jetzt nur die **Zeilen 5 bis 7** ein.
* Entfernt die **Leerzeichen** am Anfang der Zeilen durch die passende Option (Tipp: Die Dokumentation spricht hier von `gobble`).
Seht bei Fragen in der Dokumentation des Paketes `minted` nach.
@slide(layout=task)
@task-number
10
@title
`listings`
@content
* Im Ordner `exercises/source-code-listings` findet ihr eine Datei namens `Source.java`.
* **Bindet** den Java-Quelltext in der Datei `source-code-listings.tex` **ein**. (Beachtet, dass der Dateipfad relativ zu `main.tex` ist.)
* Aktiviert das **Syntax-Highlighting** durch Angabe der Sprache Java.
* **Nummeriert** die Zeilen und aktiviert **Umbrüche**.
* Setzt mit dem Parameter `basicstyle` eine dicktengleiche Schrift (`\ttfamily \small`).
* Ändert die Schlüsselwortfarbe zu Blau.
* Verbergt die Sonderzeichen, die **Leerzeichen in Zeichenketten** markieren.
Seht bei Fragen in der Dokumentation des Paketes `listings` nach.
* Im Ordner `exercises/graphics` findet ihr eine Bilddatei namens `latex-logo.png`.
* Fügt sie **genau** unterhalb dieser Aufgabenstellung im Skript ein.
* Das Bild soll **zentriert** auf der Seite eingebunden werden.
* Fügt außerdem eine **Bildunterschrift** ein.
* Passt die **Größe** des Bildes an Breite der Seite (`\textwidth`) an.

View File

@ -1,289 +1,110 @@
@slide(layout=chapter-slide)
@title
Source code listings
@slide(layout=content-only)
@title
Our options
@content
There are two popular ways to display source code in a LaTeX document.
* ++ **`minted`** renders **very pretty** source code listings, but it requires some **extra configuration** and might not work on every operating system.
* ++ **`listings`** is an **easy-to-use** (but not as pretty) option for those having troubles with `minted`.
++ Their commands are very similar.
@slide(layout=content-and-preview)
@title
Installation <span class="emoji">🖥️</span>
@content
`minted` requires the Python programming language. As soon as **[Python is installed](https://www.python.org/)**, we can download the corresponding Python package **[`Pygments`](http://pygments.org/)** using a command prompt:
``` {.lang-sh .hljs}
pip install Pygments
```
Add the LaTeX packages for `minted` or `listings` and we are good to go:
``` {.lang-tex .hljs}
\usepackage{minted}
\usepackage{listings}
```
@preview
![](svg/chapter-10/minted-overview-english-crop.svg){ .thin-padding }
@slide(layout=content-only)
@title
Compiler settings
@content
<div class="box warning">
We need to pass the additional flag `--shell-escape` to our compiler in order to use `minted`.<br />
<strong>Do this only for documents you trust!</strong>
</div>
In TeXstudio, go to **`Options → Configure TeXstudio → Commands`** (`TeXstudio → Settings` on macOS) and add the flag in the PdfLaTeX row before `%.tex`:
<code>pdflatex -synctex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex</code>
Graphics
@slide(layout=content-only)
@title
A note on our script
Including graphics <span class="emoji">🖼️</span>
@content
Our exercise material allows you to choose between `minted` and `listings`.
By default, we use `listings`.
To activate `minted`, create a file called `listings-mode.tex` in the project's root directory and add the following line:
To display graphics, we need the `graphicx` package.
``` {.lang-tex .hljs}
\newcommand\listingsmode{minted}
\begin{figure}
\includegraphics{<file path>}
\caption[<short caption (table of
figures)>]{<full caption>}
\end{figure}
```
After compiling again, your listings should be rendered by `minted`.
@slide(layout=content-and-preview-with-category)
@category
minted
@title
In-situ listings
@content
++ Within a dedicated environment:
``` {.lang-tex .hljs .fragment}
\section*{Haskell Magic}
Squares of all even % …
\begin{minted}{haskell}
[x^2 | x <- [1..200], even x]
\end{minted}
```
++ Directly inline:
``` {.lang-tex .hljs .fragment}
\section*{An HTML Example}
A headline is denoted the following way:
\mint{html}|<h2>LaTeX at University</h2>|
```
@preview
![](svg/chapter-10/minted-haskell-english-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
minted
@title
External source code
@content
We can avoid redundancy by including source code directly from its source file.
Specifying the size:
``` {.lang-tex .hljs}
\section*{Simple Java Application}
\inputminted{java}{Test.java}
\includegraphics[width=0.5\textwidth,
height=5cm]{<file path>}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/minted-java-english-crop.svg)
@slide(layout=content-only-with-category)
@category
minted
@slide(layout=content-only)
@title
Themes and styles
Layout on the page
@content
Optional parameters allow us to activate line numbers, automated line breaks, and syntax highlighting based on numerous color schemes.
``` {.lang-tex .hljs}
\usemintedstyle{monokai}
\begin{minted}[
linenos=true,
breaklines=true,
]{javascript}
% ...
\end{minted}
\begin{figure}[<position code>]
```
<span class="emoji">🔗</span> **[introduction](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted)** and **[official documentation](https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf)**
LaTeX places graphics automatically. With position codes, we can express our preferences (they can be combined as well).
@slide(layout=content-and-preview-with-category)
<table>
<tr>
<th style="min-width:4em">code</th>
<th>position</th>
</tr>
<tr class="fragment">
<td>h</td>
<td>here, if you dont mind</td>
</tr>
<tr class="fragment">
<td>t</td>
<td>top of the page</td>
</tr>
<tr class="fragment">
<td>b</td>
<td>bottom of the page</td>
</tr>
<tr class="fragment">
<td>p</td>
<td>on its own page</td>
</tr>
<tr class="fragment">
<td>H</td>
<td>Here, for Gods sake! <br />(`float` package required)</td>
</tr>
</table>
@category
listings
@slide(layout=content-only)
@title
In-situ listings
Centred alignment
@content
Within a dedicated environment:
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\begin{center}
\includegraphics{<path-to-file>}
\end{center}
\end{figure}
```
Alternatively:
``` {.lang-tex .hljs}
\section*{Haskell Magic}
Squares of all even % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
\begin{figure}[<position>]
\centering
\includegraphics{<path-to-file>}
\end{figure}
```
The `listings` package does not provide an inline command.
@preview
![](svg/chapter-10/listings-haskell-english-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
listings
@title
External source code
@content
Conveniently, also `listings` offers an import command:
``` {.lang-tex .hljs}
\section*{Simple Java Application}
\lstinputlisting[language=Java]{Test.java}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/listings-java-english-crop.svg)
@slide(layout=content-only-with-category)
@category
listings
@title
Themes and styles
@content
`listings` does not provide any themes by default, but can be configured extensively.
``` {.lang-tex .hljs}
\begin{lstlisting}[
language=Java,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
keywordstyle=\color{ForestGreen},
commentstyle=\color{DarkGray},
literate={ö}{{\"o}}1
]
% …
\end{lstlisting}
```
Have a look at the `\lstset` command for creating your own themes.
@slide(layout=content-only-with-category)
@category
listings
@title
Further resources
@content
* [introduction](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide) to the package
* official [documentation](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide)
* [`xcolor` package](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX) for colors
* two [solarized themes](https://github.com/jez/latex-solarized) for `listings`
@slide(layout=task)
@task-number
10
11
@title
Listings with `minted`
Inserting graphics
@content
* In the directory `exercises/source-code-listings` you can find a file named `Source.java`.
* **Include** it in the file `source-code-listings.tex`. (Keep in mind that the file path is relative to the main LaTeX document, i.&thinsp;e., to `main.tex`.)
* Activate **syntax highlighting** by stating the programming language Java.
* **Number** the code lines and add **line breaks**.
* Use the **theme** `native`.
* Change the **background colour** to dark blue.
* Now only include **lines 5 to 7**.
* Delete the **spaces** at the beginning of the lines. (Hint: The documentation speaks of `autogobble`).
If you have questions, try to consult the documentation of the `minted` package.
@slide(layout=task)
@task-number
10
@title
Listings with `listings`
@content
* In the directory `exercises/source-code-listings` you can find a file named `Source.java`.
* **Include** it in the file `source-code-listings.tex`. (Keep in mind that the file path is relative to the main LaTeX document, i.&thinsp;e., to `main.tex`.)
* Activate **syntax highlighting** by stating the programming language Java.
* **Number** the code lines.
* Set the `basicstyle` to a proper mono-spaced font (`\ttfamily \small`)
* Change the **keyword color** to blue.
* Don't show special characters for **spaces in strings**.
If you have questions, try to consult the documentation of the `listings` package.
* In the directory `exercises/graphics` you can find an image file named `latex-logo.png`.
* **Include** the figure in `exercises/graphics/graphics.tex` and place it **exactly** where you include it.
* The image shall be **centered**.
* Additionally, add a **caption** for the figure.
* Adapt the **width** of the image to the width of the text (`\textwidth`).

View File

@ -1,110 +1,228 @@
@slide(layout=chapter-slide)
@title
Grafiken
Tabellen
@slide(layout=content-only)
@title
Grafiken abbilden <span class="emoji">🖼</span>
Grundstruktur <span class="emoji">🗒</span>
@content
Um Grafiken darzustellen, muss das Paket `graphicx` eingebunden werden.
Für typografisch schöne Tabellen nutzen wir das Paket `booktabs`.
``` {.lang-tex .hljs}
\begin{figure}
\includegraphics{<dateipfad>}
\caption[<kurztitel (abbildungsver-
zeichnis)>]{<bildunterschrift>}
\end{figure}
``` {.hljs .lang-tex}
\usepackage{booktabs}
```
Bildgröße angeben:
``` {.lang-tex .hljs}
\includegraphics[width=0.5\textwidth,
height=5cm]{<dateipfad>}
``` {.hljs .lang-tex}
\begin{table}[<position>]
\begin{tabular}{<spaltendefinition>}
% Tabelleninhalt
\end{tabular}
\caption{<tabellenunterschrift>}
\end{table}
```
Die Positionierung erfolgt analog zu Grafiken.
@slide(layout=content-only)
@slide(layout=content-and-preview)
@title
Positionierung auf der Seite
Spaltendefinitionen
@content
``` {.lang-tex .hljs}
\begin{figure}[<positionskürzel>]
``` {.hljs .lang-tex}
\begin{tabular}{lrcl}
% Tabelleninhalt
\end{tabular}
```
LaTeX positioniert Grafiken automatisch. Durch Kürzel können wir unsere Präferenzen (auch in Kombination) angeben.
<table>
<tr>
<th style="min-width:4em">Kürzel</th>
<th>Position</th>
<th>Kürzel</th>
<th>Bedeutung</th>
</tr>
<tr class="fragment">
<td>h</td>
<td>hier, wenn es dir recht ist</td>
<td>l</td>
<td>linksbündige Spalte</td>
</tr>
<tr class="fragment">
<td>t</td>
<td>oberer Seitenrand (top)</td>
<td>c</td>
<td>zentrierte Spalte</td>
</tr>
<tr class="fragment">
<td>b</td>
<td>unterer Seitenrand (bottom)</td>
</tr>
<tr class="fragment">
<td>p</td>
<td>auf einer eigenen Seite (page)</td>
</tr>
<tr class="fragment">
<td>H</td>
<td>Hier, verdammt noch mal! <br />(benötigt Paket `float`)</td>
<td>r</td>
<td>rechtsbündige Spalte</td>
</tr>
</table>
++ Vertikale Trennlinien und doppelte horizontale Linien sind unter Typograph\*innen nicht gern gesehen und sollten vermieden werden.
@preview
![](svg/chapter-12/tabelle-beispiel-crop.svg){.thin-padding}
@slide(layout=content-and-preview)
@title
Tabelleninhalt
@content
``` {.hljs .lang-tex}
\begin{tabular}{lll}
\toprule
Spalte 1 & Spalte 2 & Spalte 3 \\
\midrule
Inhalt a & Inhalt b & Inhalt c \\
Inhalt e & Inhalt f & Inhalt g \\
Inhalt i & Inhalt j & Inhalt k \\
\bottomrule
\end{tabular}
```
* ++ Zellen werden durch `&` abgetrennt.
* ++ Reihen werden durch `\\` beendet.
* ++ `\toprule`, `\midrule` und `\bottomrule` strukturieren die Tabelle.
@preview
![](svg/chapter-12/tabelle-inhalt-beispiel-crop.svg){.thin-padding}
@slide(layout=wide-content)
@title
Die gesamte Tabelle
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{lrcl}
\toprule
Sprache & Autor & Erscheinungsjahr & Aktuelle Version \\
\midrule
C++ & Bjarne Stroustrup & 1985 & C++ 17 \\
Java & James Gosling & 1998 & 13 \\
Python & Guido van Rossum & 1991 & 3.8.0 \\
\bottomrule
\end{tabular}
\caption{Bekannte Programmiersprachen}
\end{table}
```
<p data-category="Hinweis">Mit `@{}` vor und nach einer Spaltendefinition wird der umgebende Weißraum unterdrückt.</p>
<p data-category="Beispiel">`\begin{tabular}{@{}lrcl@{}}` beschränkt die Linien auf die Breite des Tabelleninhalts.</p>
@slide(layout=content-only)
@title
Zentrierte Anordnung
Besonders lange Tabellen
@content
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\begin{center}
\includegraphics{<dateipfad>}
\end{center}
\end{figure}
Tabellen, die über eine Seite hinaus gehen, werden von `tabular` einfach abgeschnitten. Die Lösung bietet hier das Paket `longtable`:
``` {.hljs .lang-tex}
\usepackage{longtable}
```
Alternativ:
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\centering
\includegraphics{<dateipfad>}
\end{figure}
``` {.hljs .lang-tex}
\begin{longtable}{<spaltendefinition>}
% Tabelleninhalt
\caption{<tabellenunterschrift>}
\label{<label>}
\end{longtable}
```
@slide(layout=content-only)
@title
Hinweise zu `longtable`
@content
``` {.hljs .lang-tex}
\begin{longtable}{<spaltendefinition>}
% Tabelleninhalt
\caption{<tabellenunterschrift>}
\label{<label>}
\end{longtable}
```
* ++ Die `longtable`-Umgebung vereint die `tabular`-Umgebung und die `table`-Umgebung.
* ++ Titel und Label können deshalb direkt unter dem Tabelleninhalt stehen.
* ++ Sofern das Paket `booktabs` eingebunden ist, stehen seine Features auch in `longtable` zur Verfügung.
@slide(layout=content-only)
@title
Besonders breite Tabellen
@content
Sollte die Tabelle einmal zu breit für eine Seite werden, kann sie auch im Querformat dargestellt werden:
``` {.hljs .lang-tex}
\usepackage{rotating}
```
``` {.hljs .lang-tex}
\begin{sidewaystable}[<position>]
\begin{tabular}{<spaltendef.>}
% Tabelleninhalt
\end{tabular}
\end{sidewaystable}
```
@slide(layout=content-and-preview)
@title
Weiterführendes Beispiel
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{llr}
\toprule
Struktur & \multicolumn{2}{l}{Zeitkomplexität für Zugriff} \\ \cmidrule(r){2-3}
& Average & Worst \\
\midrule
Stack & $\mathcal{O}(n)$ & $\mathcal{O}(n)$ \\
Binärbaum & $\mathcal{O}(log(n))$ & $\mathcal{O}(log(n))$ \\
AVL-Baum & $\mathcal{O}(log(n))$ & $\mathcal{O}(log(n))$ \\
\bottomrule
\end{tabular}
\end{table}
```
[<span class="emoji">🔗</span> Dokumentation zu Booktabs](http://packages.oth-regensburg.de/ctan/macros/latex/contrib/booktabs/booktabs.pdf)
@preview
![](svg/chapter-12/tabelle-fortgeschritten-beispiel-crop.svg){.thin-padding}
@slide(layout=content-only)
@title
Mehr Komfort
@content
Der [Tables Generator](https://tablesgenerator.com/) ist ein wunderbares Werkzeug, um schnell Tabellen verschiedener Formate zu erstellen.
[![](svg/chapter-12/tables-generator.png)](https://tablesgenerator.com/)
@slide(layout=task)
@task-number
11
12
@title
Grafiken einfügen
Tabellen setzen
@content
* Im Ordner `exercises/graphics` findet ihr eine Bilddatei namens `latex-logo.png`.
* Fügt sie **genau** unterhalb dieser Aufgabenstellung im Skript ein.
* Das Bild soll **zentriert** auf der Seite eingebunden werden.
* Fügt außerdem eine **Bildunterschrift** ein.
* Passt die **Größe** des Bildes an Breite der Seite (`\textwidth`) an.
* Die Liste in der Datei `exercises/tables/tables.tex` enthält Infos zu ein paar Lehrveranstaltung an der Fakultät WIAI.
* Wandelt die Liste in eine **Tabelle** um.
* Die Tabelle soll Spalten für den *Namen*, das *Kürzel* und das *Semester* der Vorlesungen haben.
* Fügt außerdem eine **zentrierte Spalte** ganz links in der Tabelle hinzu, in der ihr die Lehrveranstaltungen **nummeriert**.

View File

@ -1,110 +1,229 @@
@slide(layout=chapter-slide)
@title
Graphics
Tables
@slide(layout=content-only)
@title
Including graphics <span class="emoji">🖼</span>
Basic structure <span class="emoji">🗒</span>
@content
To display graphics, we need the `graphicx` package.
For typographically pleasing tables, we use the `booktabs` package.
``` {.lang-tex .hljs}
\begin{figure}
\includegraphics{<file path>}
\caption[<short caption (table of
figures)>]{<full caption>}
\end{figure}
``` {.hljs .lang-tex}
\usepackage{booktabs}
```
Specifying the size:
``` {.lang-tex .hljs}
\includegraphics[width=0.5\textwidth,
height=5cm]{<file path>}
``` {.hljs .lang-tex}
\begin{table}[<position>]
\begin{tabular}{<column definition>}
% table content
\end{tabular}
\caption{<caption>}
\end{table}
```
Positioning works just like with graphics.
@slide(layout=content-only)
@slide(layout=content-and-preview)
@title
Layout on the page
Column definitions
@content
``` {.lang-tex .hljs}
\begin{figure}[<position code>]
``` {.hljs .lang-tex}
\begin{tabular}{lrcl}
% table content
\end{tabular}
```
LaTeX places graphics automatically. With position codes, we can express our preferences (they can be combined as well).
<table>
<tr>
<th style="min-width:4em">code</th>
<th>position</th>
<th>letter</th>
<th>meaning</th>
</tr>
<tr class="fragment">
<td>h</td>
<td>here, if you dont mind</td>
<td>l</td>
<td>left-justified column</td>
</tr>
<tr class="fragment">
<td>t</td>
<td>top of the page</td>
<td>c</td>
<td>centred column</td>
</tr>
<tr class="fragment">
<td>b</td>
<td>bottom of the page</td>
</tr>
<tr class="fragment">
<td>p</td>
<td>on its own page</td>
</tr>
<tr class="fragment">
<td>H</td>
<td>Here, for Gods sake! <br />(`float` package required)</td>
<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
![](svg/chapter-12/table-example-crop.svg){.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
![](svg/chapter-12/table-content-example-crop.svg){.thin-padding}
@slide(layout=wide-content)
@title
The entire table
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{lrcl}
\toprule
Language & Author & Year & Version \\
\midrule
C++ & Bjarne Stroustrup & 1985 & C++ 17 \\
Java & James Gosling & 1998 & 13 \\
Python & Guido van Rossum & 1991 & 3.8.0 \\
\bottomrule
\end{tabular}
\caption{Well-known programming languages}
\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
Centred alignment
Particularly long tables
@content
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\begin{center}
\includegraphics{<path-to-file>}
\end{center}
\end{figure}
Tables that exceed one page are simply cut off by `tabular`. A solution is offered by the `longtable` package:
``` {.hljs .lang-tex}
\usepackage{longtable}
```
Alternatively:
``` {.lang-tex .hljs}
\begin{figure}[<position>]
\centering
\includegraphics{<path-to-file>}
\end{figure}
``` {.hljs .lang-tex}
\begin{longtable}{<column definition>}
% table content
\caption{<caption>}
\label{<label>}
\end{longtable}
```
@slide(layout=content-only)
@title
Notes on `longtable`
@content
``` {.hljs .lang-tex}
\begin{longtable}{<column definition>}
% table content
\caption{<caption>}
\label{<label>}
\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=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=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}
```
[<span class="emoji">🔗</span> Booktabs documentation](http://packages.oth-regensburg.de/ctan/macros/latex/contrib/booktabs/booktabs.pdf)
@preview
![](svg/chapter-12/table-advanced-example-crop.svg){.thin-padding}
@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.
[![](svg/chapter-12/tables-generator.png)](https://tablesgenerator.com/)
@slide(layout=task)
@task-number
11
12
@title
Inserting graphics
Typesetting tables
@content
* In the directory `exercises/graphics` you can find an image file named `latex-logo.png`.
* **Include** the figure in `exercises/graphics/graphics.tex` and place it **exactly** where you include it.
* The image shall be **centered**.
* Additionally, add a **caption** for the figure.
* Adapt the **width** of the image to the width of the text (`\textwidth`).
* 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.

View File

@ -1,228 +1,292 @@
@slide(layout=chapter-slide)
@title
Tabellen
Quelltext-Listings
@slide(layout=content-only)
@title
Grundstruktur <span class="emoji">🗒️</span>
Unsere Optionen
@content
Für typografisch schöne Tabellen nutzen wir das Paket `booktabs`.
Es gibt zwei verbreitete Wege, Quelltext in LaTeX-Dokumenten zu zeigen.
``` {.hljs .lang-tex}
\usepackage{booktabs}
```
* ++ **`minted`** erstellt **sehr hübsche** Quelltext-Listings, aber es benötigt **etwas mehr Konfiguration** und funktioniert nicht auf allen Betriebssystemen zuverlässig.
* ++ **`listings`** ist eine **einfach zu nutzende** (aber nicht so hübsche) Alternative für diejenigen, die Probleme mit `minted` haben.
``` {.hljs .lang-tex}
\begin{table}[<position>]
\begin{tabular}{<spaltendefinition>}
% Tabelleninhalt
\end{tabular}
\caption{<tabellenunterschrift>}
\end{table}
```
Die Positionierung erfolgt analog zu Grafiken.
++ Die Befehle beider Pakete sind sehr ähnlich.
@slide(layout=content-and-preview)
@title
Spaltendefinitionen
Installation <span class="emoji">🖥️</span>
@content
``` {.hljs .lang-tex}
\begin{tabular}{lrcl}
% Tabelleninhalt
\end{tabular}
`minted` benötigt die Programmiersprache Python. Nach der **[Installation von Python](https://www.python.org/)** kann das zugehörige Paket **[`Pygments`](http://pygments.org/)** in der Eingabeaufforderung installiert werden durch:
``` {.lang-sh .hljs}
pip install Pygments
```
<table>
<tr>
<th>Kürzel</th>
<th>Bedeutung</th>
</tr>
<tr class="fragment">
<td>l</td>
<td>linksbündige Spalte</td>
</tr>
<tr class="fragment">
<td>c</td>
<td>zentrierte Spalte</td>
</tr>
<tr class="fragment">
<td>r</td>
<td>rechtsbündige Spalte</td>
</tr>
</table>
Anschließend muss das Paket `minted` (oder `listings`) nur noch eingebunden werden:
++ Vertikale Trennlinien und doppelte horizontale Linien sind unter Typograph\*innen nicht gern gesehen und sollten vermieden werden.
``` {.lang-tex .hljs}
\usepackage{minted}
\usepackage{listings}
```
@preview
![](svg/chapter-12/tabelle-beispiel-crop.svg){.thin-padding}
![](svg/chapter-10/minted-overview-crop.svg){ .thin-padding }
@slide(layout=content-and-preview)
@slide(layout=content-only)
@title
Tabelleninhalt
Anpassungen beim Kompilieren
@content
``` {.hljs .lang-tex}
\begin{tabular}{lll}
\toprule
Spalte 1 & Spalte 2 & Spalte 3 \\
\midrule
Inhalt a & Inhalt b & Inhalt c \\
Inhalt e & Inhalt f & Inhalt g \\
Inhalt i & Inhalt j & Inhalt k \\
\bottomrule
\end{tabular}
<div class="box warning">
Um Dokumente mit Quelltext-Listings kompilieren zu können, muss die Flag `--shell-escape` an den Compiler übergeben werden.<br />
<strong>Dies ist nur bei vertrauenswürdigen Dokumenten empfehlenswert.</strong>
</div>
In TeXstudio muss diese unter **`Optionen → TeXstudio konfigurieren → Befehle`** (`TeXstudio → Einstellungen` unter macOS) bei PdfLaTeX vor `%.tex` ergänzt werden:
<code>pdflatex -synctex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex</code>
@slide(layout=content-only)
@title
Ein Hinweis zu unserem Skript
@content
Unsere Übungsmaterialien erlauben es, zwischen `minted` und `listings` zu wählen.
Von Haus aus nutzen wir `listings`.
Um `minted` zu aktivieren muss eine Datei namens `listings-mode.tex` mit dem folgenden Inhalt im Wurzelverzeichnis unseres Projekts erstellt werden:
```{.lang-tex .hljs}
\newcommand\listingsmode{minted}
```
Kompiliert erneut und die Listings sollten durch `minted` erstellt worden sein.
@slide(layout=content-and-preview-with-category)
@category
minted
@title
Code im LaTeX-Quelltext
@content
++ In einer eigenen Umgebung:
``` {.lang-tex .hljs .fragment}
\section*{Haskell-Magie}
Quadrate aller geraden % …
\begin{minted}{haskell}
[x^2 | x <- [1..200], even x]
\end{minted}
```
++ Inline im Quelltext:
``` {.lang-tex .hljs .fragment}
\section*{Ein HTML-Beispiel}
Eine Überschrift wird so ausgezeichnet:
\mint{html}|<h2>LaTeX im Studium</h2>|.
```
* ++ Zellen werden durch `&` abgetrennt.
* ++ Reihen werden durch `\\` beendet.
* ++ `\toprule`, `\midrule` und `\bottomrule` strukturieren die Tabelle.
@preview
![](svg/chapter-12/tabelle-inhalt-beispiel-crop.svg){.thin-padding}
![](svg/chapter-10/minted-haskell-crop.svg)
@slide(layout=wide-content)
@slide(layout=content-and-preview-with-category)
@category
minted
@title
Die gesamte Tabelle
Externer Code
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{lrcl}
\toprule
Sprache & Autor & Erscheinungsjahr & Aktuelle Version \\
\midrule
C++ & Bjarne Stroustrup & 1985 & C++ 17 \\
Java & James Gosling & 1998 & 13 \\
Python & Guido van Rossum & 1991 & 3.8.0 \\
\bottomrule
\end{tabular}
\caption{Bekannte Programmiersprachen}
\end{table}
Um Redundanz zu vermeiden, ist es manchmal praktisch, den Quelltext direkt aus der Quelldatei einzulesen.
``` {.lang-tex .hljs}
\section*{Einfache Java-Anwendung}
\inputminted{java}{Test.java}
```
<p data-category="Hinweis">Mit `@{}` vor und nach einer Spaltendefinition wird der umgebende Weißraum unterdrückt.</p>
<p data-category="Beispiel">`\begin{tabular}{@{}lrcl@{}}` beschränkt die Linien auf die Breite des Tabelleninhalts.</p>
@slide(layout=content-only)
@title
Besonders lange Tabellen
@content
Tabellen, die über eine Seite hinaus gehen, werden von `tabular` einfach abgeschnitten. Die Lösung bietet hier das Paket `longtable`:
``` {.hljs .lang-tex}
\usepackage{longtable}
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
``` {.hljs .lang-tex}
\begin{longtable}{<spaltendefinition>}
% Tabelleninhalt
\caption{<tabellenunterschrift>}
\label{<label>}
\end{longtable}
```
@slide(layout=content-only)
@title
Hinweise zu `longtable`
@content
``` {.hljs .lang-tex}
\begin{longtable}{<spaltendefinition>}
% Tabelleninhalt
\caption{<tabellenunterschrift>}
\label{<label>}
\end{longtable}
```
* ++ Die `longtable`-Umgebung vereint die `tabular`-Umgebung und die `table`-Umgebung.
* ++ Titel und Label können deshalb direkt unter dem Tabelleninhalt stehen.
* ++ Sofern das Paket `booktabs` eingebunden ist, stehen seine Features auch in `longtable` zur Verfügung.
@slide(layout=content-only)
@title
Besonders breite Tabellen
@content
Sollte die Tabelle einmal zu breit für eine Seite werden, kann sie auch im Querformat dargestellt werden:
``` {.hljs .lang-tex}
\usepackage{rotating}
```
``` {.hljs .lang-tex}
\begin{sidewaystable}[<position>]
\begin{tabular}{<spaltendef.>}
% Tabelleninhalt
\end{tabular}
\end{sidewaystable}
```
@slide(layout=content-and-preview)
@title
Weiterführendes Beispiel
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{llr}
\toprule
Struktur & \multicolumn{2}{l}{Zeitkomplexität für Zugriff} \\ \cmidrule(r){2-3}
& Average & Worst \\
\midrule
Stack & $\mathcal{O}(n)$ & $\mathcal{O}(n)$ \\
Binärbaum & $\mathcal{O}(log(n))$ & $\mathcal{O}(log(n))$ \\
AVL-Baum & $\mathcal{O}(log(n))$ & $\mathcal{O}(log(n))$ \\
\bottomrule
\end{tabular}
\end{table}
```
[<span class="emoji">🔗</span> Dokumentation zu Booktabs](http://packages.oth-regensburg.de/ctan/macros/latex/contrib/booktabs/booktabs.pdf)
@preview
![](svg/chapter-12/tabelle-fortgeschritten-beispiel-crop.svg){.thin-padding}
![](svg/chapter-10/minted-java-crop.svg)
@slide(layout=content-only)
@slide(layout=content-only-with-category)
@category
minted
@title
Mehr Komfort
Themen und Stile
@content
Der [Tables Generator](https://tablesgenerator.com/) ist ein wunderbares Werkzeug, um schnell Tabellen verschiedener Formate zu erstellen.
Durch optionale Parameter können Zeilennummerierung, Umbrüche und Farben ausgewählt werden. Außerdem sind zahlreiche Themes verfügbar.
[![](svg/chapter-12/tables-generator.png)](https://tablesgenerator.com/)
``` {.lang-tex .hljs}
\usemintedstyle{monokai}
\begin{minted}[
linenos=true,
breaklines=true,
]{javascript}
% ...
\end{minted}
```
<span class="emoji">🔗</span> **[Einführung](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted)** und **[offizielle Dokumentation](https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf)**
@slide(layout=content-and-preview-with-category)
@category
listings
@title
Code im LaTeX-Quelltext
@content
In einer eigenen Umgebung:
``` {.lang-tex .hljs}
\section*{Haskell-Magie}
Quadrate aller geraden % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
```
Das Paket `listings` stellt keinen Befehl für inline-Listings zur Verfügung.
@preview
![](svg/chapter-10/listings-haskell-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
listings
@title
Externer Code
@content
Praktischerweise bietet auch `listings` einen Befehl zum Einfügen externen Codes:
``` {.lang-tex .hljs}
\section*{Einfache Java-Anwendung}
\lstinputlisting[language=Java]{Test.java}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/listings-java-crop.svg)
@slide(layout=content-only-with-category)
@category
listings
@title
Themen und Stile
@content
`listings` stellt keine eigenen Themes zur Verfügung. Dies kann aber durch die vielen Konfigurationsoptionen ausgeglichen werden.
``` {.lang-tex .hljs}
\begin{lstlisting}[
language=Java,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
keywordstyle=\color{ForestGreen},
commentstyle=\color{DarkGray},
literate={ö}{{\"o}}1
]
% …
\end{lstlisting}
```
Für die Erstellung eigener Themes ist der Befehl `\lstset` nützlich.
@slide(layout=content-only-with-category)
@category
listings
@title
Weitere Materialien
@content
* [Einführung](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide) in das Paket
* offizielle [Dokumentation](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide)
* das Paket [`xcolor`](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX) für Farben
* zwei [Themes](https://github.com/jez/latex-solarized) im solarized-Stil für `listings`
@slide(layout=task)
@task-number
12
10
@title
Tabellen setzen
`minted`
@content
* Die Liste in der Datei `exercises/tables/tables.tex` enthält Infos zu ein paar Lehrveranstaltung an der Fakultät WIAI.
* Wandelt die Liste in eine **Tabelle** um.
* Die Tabelle soll Spalten für den *Namen*, das *Kürzel* und das *Semester* der Vorlesungen haben.
* Fügt außerdem eine **zentrierte Spalte** ganz links in der Tabelle hinzu, in der ihr die Lehrveranstaltungen **nummeriert**.
* Im Ordner `exercises/source-code-listings` findet ihr eine Datei namens `Source.java`.
* **Bindet** den Java-Quelltext in der Datei `source-code-listings.tex` **ein**. (Beachtet, dass der Dateipfad relativ zu `main.tex` ist.)
* Aktiviert das **Syntax-Highlighting** durch Angabe der Sprache Java.
* **Nummeriert** die Zeilen und aktiviert **Umbrüche**.
* Nutzt das **Theme** `native`.
* Ändert die **Hintergrundfarbe** des Themes zu Dunkelblau.
* Bindet jetzt nur die **Zeilen 5 bis 7** ein.
* Entfernt die **Leerzeichen** am Anfang der Zeilen durch die passende Option (Tipp: Die Dokumentation spricht hier von `gobble`).
Seht bei Fragen in der Dokumentation des Paketes `minted` nach.
@slide(layout=task)
@task-number
10
@title
`listings`
@content
* Im Ordner `exercises/source-code-listings` findet ihr eine Datei namens `Source.java`.
* **Bindet** den Java-Quelltext in der Datei `source-code-listings.tex` **ein**. (Beachtet, dass der Dateipfad relativ zu `main.tex` ist.)
* Aktiviert das **Syntax-Highlighting** durch Angabe der Sprache Java.
* **Nummeriert** die Zeilen und aktiviert **Umbrüche**.
* Setzt mit dem Parameter `basicstyle` eine dicktengleiche Schrift (`\ttfamily \small`).
* Ändert die Schlüsselwortfarbe zu Blau.
* Verbergt die Sonderzeichen, die **Leerzeichen in Zeichenketten** markieren.
Seht bei Fragen in der Dokumentation des Paketes `listings` nach.

View File

@ -1,229 +1,289 @@
@slide(layout=chapter-slide)
@title
Tables
Source code listings
@slide(layout=content-only)
@title
Basic structure <span class="emoji">🗒️</span>
Our options
@content
For typographically pleasing tables, we use the `booktabs` package.
There are two popular ways to display source code in a LaTeX document.
``` {.hljs .lang-tex}
\usepackage{booktabs}
```
``` {.hljs .lang-tex}
\begin{table}[<position>]
\begin{tabular}{<column definition>}
% table content
\end{tabular}
\caption{<caption>}
\end{table}
```
Positioning works just like with graphics.
* ++ **`minted`** renders **very pretty** source code listings, but it requires some **extra configuration** and might not work on every operating system.
* ++ **`listings`** is an **easy-to-use** (but not as pretty) option for those having troubles with `minted`.
++ Their commands are very similar.
@slide(layout=content-and-preview)
@title
Column definitions
Installation <span class="emoji">🖥️</span>
@content
``` {.hljs .lang-tex}
\begin{tabular}{lrcl}
% table content
\end{tabular}
`minted` requires the Python programming language. As soon as **[Python is installed](https://www.python.org/)**, we can download the corresponding Python package **[`Pygments`](http://pygments.org/)** using a command prompt:
``` {.lang-sh .hljs}
pip install Pygments
```
<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>
Add the LaTeX packages for `minted` or `listings` and we are good to go:
++ Vertical separator lines and double horizontal lines are frowned upon by typographers and should be avoided.
``` {.lang-tex .hljs}
\usepackage{minted}
\usepackage{listings}
```
@preview
![](svg/chapter-12/table-example-crop.svg){.thin-padding}
![](svg/chapter-10/minted-overview-english-crop.svg){ .thin-padding }
@slide(layout=content-and-preview)
@slide(layout=content-only)
@title
Table content
Compiler settings
@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}
<div class="box warning">
We need to pass the additional flag `--shell-escape` to our compiler in order to use `minted`.<br />
<strong>Do this only for documents you trust!</strong>
</div>
In TeXstudio, go to **`Options → Configure TeXstudio → Commands`** (`TeXstudio → Settings` on macOS) and add the flag in the PdfLaTeX row before `%.tex`:
<code>pdflatex -synctex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex</code>
@slide(layout=content-only)
@title
A note on our script
@content
Our exercise material allows you to choose between `minted` and `listings`.
By default, we use `listings`.
To activate `minted`, create a file called `listings-mode.tex` in the project's root directory and add the following line:
```{.lang-tex .hljs}
\newcommand\listingsmode{minted}
```
After compiling again, your listings should be rendered by `minted`.
@slide(layout=content-and-preview-with-category)
@category
minted
@title
In-situ listings
@content
++ Within a dedicated environment:
``` {.lang-tex .hljs .fragment}
\section*{Haskell Magic}
Squares of all even % …
\begin{minted}{haskell}
[x^2 | x <- [1..200], even x]
\end{minted}
```
++ Directly inline:
``` {.lang-tex .hljs .fragment}
\section*{An HTML Example}
A headline is denoted the following way:
\mint{html}|<h2>LaTeX at University</h2>|
```
* ++ Columns are separated by `&`.
* ++ Rows are ended by `\\`.
* ++ `\toprule`, `\midrule` and `\bottomrule` structure the table.
@preview
![](svg/chapter-12/table-content-example-crop.svg){.thin-padding}
![](svg/chapter-10/minted-haskell-english-crop.svg)
@slide(layout=wide-content)
@slide(layout=content-and-preview-with-category)
@category
minted
@title
The entire table
External source code
@content
``` {.hljs .lang-tex}
\begin{table}[h]
\begin{tabular}{lrcl}
\toprule
Language & Author & Year & Version \\
\midrule
C++ & Bjarne Stroustrup & 1985 & C++ 17 \\
Java & James Gosling & 1998 & 13 \\
Python & Guido van Rossum & 1991 & 3.8.0 \\
\bottomrule
\end{tabular}
\caption{Well-known programming languages}
\end{table}
We can avoid redundancy by including source code directly from its source file.
``` {.lang-tex .hljs}
\section*{Simple Java Application}
\inputminted{java}{Test.java}
```
<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
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}
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
``` {.hljs .lang-tex}
\begin{longtable}{<column definition>}
% table content
\caption{<caption>}
\label{<label>}
\end{longtable}
```
@slide(layout=content-only)
@title
Notes on `longtable`
@content
``` {.hljs .lang-tex}
\begin{longtable}{<column definition>}
% table content
\caption{<caption>}
\label{<label>}
\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=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=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}
```
[<span class="emoji">🔗</span> Booktabs documentation](http://packages.oth-regensburg.de/ctan/macros/latex/contrib/booktabs/booktabs.pdf)
@preview
![](svg/chapter-12/table-advanced-example-crop.svg){.thin-padding}
![](svg/chapter-10/minted-java-english-crop.svg)
@slide(layout=content-only)
@slide(layout=content-only-with-category)
@category
minted
@title
More comfort
Themes and styles
@content
The [Tables Generator](https://tablesgenerator.com/) is a wonderful tool to quickly create tables of different formats.
Optional parameters allow us to activate line numbers, automated line breaks, and syntax highlighting based on numerous color schemes.
[![](svg/chapter-12/tables-generator.png)](https://tablesgenerator.com/)
``` {.lang-tex .hljs}
\usemintedstyle{monokai}
\begin{minted}[
linenos=true,
breaklines=true,
]{javascript}
% ...
\end{minted}
```
<span class="emoji">🔗</span> **[introduction](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted)** and **[official documentation](https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf)**
@slide(layout=content-and-preview-with-category)
@category
listings
@title
In-situ listings
@content
Within a dedicated environment:
``` {.lang-tex .hljs}
\section*{Haskell Magic}
Squares of all even % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
```
The `listings` package does not provide an inline command.
@preview
![](svg/chapter-10/listings-haskell-english-crop.svg)
@slide(layout=content-and-preview-with-category)
@category
listings
@title
External source code
@content
Conveniently, also `listings` offers an import command:
``` {.lang-tex .hljs}
\section*{Simple Java Application}
\lstinputlisting[language=Java]{Test.java}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/chapter-10/listings-java-english-crop.svg)
@slide(layout=content-only-with-category)
@category
listings
@title
Themes and styles
@content
`listings` does not provide any themes by default, but can be configured extensively.
``` {.lang-tex .hljs}
\begin{lstlisting}[
language=Java,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
keywordstyle=\color{ForestGreen},
commentstyle=\color{DarkGray},
literate={ö}{{\"o}}1
]
% …
\end{lstlisting}
```
Have a look at the `\lstset` command for creating your own themes.
@slide(layout=content-only-with-category)
@category
listings
@title
Further resources
@content
* [introduction](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide) to the package
* official [documentation](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide)
* [`xcolor` package](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX) for colors
* two [solarized themes](https://github.com/jez/latex-solarized) for `listings`
@slide(layout=task)
@task-number
12
10
@title
Typesetting tables
Listings with `minted`
@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.
* In the directory `exercises/source-code-listings` you can find a file named `Source.java`.
* **Include** it in the file `source-code-listings.tex`. (Keep in mind that the file path is relative to the main LaTeX document, i.&thinsp;e., to `main.tex`.)
* Activate **syntax highlighting** by stating the programming language Java.
* **Number** the code lines and add **line breaks**.
* Use the **theme** `native`.
* Change the **background colour** to dark blue.
* Now only include **lines 5 to 7**.
* Delete the **spaces** at the beginning of the lines. (Hint: The documentation speaks of `autogobble`).
If you have questions, try to consult the documentation of the `minted` package.
@slide(layout=task)
@task-number
10
@title
Listings with `listings`
@content
* In the directory `exercises/source-code-listings` you can find a file named `Source.java`.
* **Include** it in the file `source-code-listings.tex`. (Keep in mind that the file path is relative to the main LaTeX document, i.&thinsp;e., to `main.tex`.)
* Activate **syntax highlighting** by stating the programming language Java.
* **Number** the code lines.
* Set the `basicstyle` to a proper mono-spaced font (`\ttfamily \small`)
* Change the **keyword color** to blue.
* Don't show special characters for **spaces in strings**.
If you have questions, try to consult the documentation of the `listings` package.