latex-skript/content/graphics.tex

51 lines
2.8 KiB
TeX

\chapter{Graphics}
\label{sec:graphics}
Since in \LaTeX{} we work with plain text, we cannot simply embed graphics into our text as we may be used to from other word processing programs.
Instead, we reference external image files by a command. The figure is then embedded and positioned at compile time.
\section{Inserting graphics}
\label{sec:display-graphics}
In order to be able to reference graphics, the package \texttt{graphicx} has to be included. For inserting a figure, we can use the following commands:
\codeblock{latex}{listings/graphics/insert-graphics.tex}
\noindent The command \mono{includegraphics} can be used to change the image size. The desired height and width of the figure can be indicated separately, as illustrated by the following example:
\codeblock{latex}{listings/graphics/image-size.tex}
\section{Positioning}
\label{sec:graphics-placement}
One interesting aspect of the what-you-get-is-what-you-mean paradigm is the way how graphics can be positioned.
The compiler creates multiple layouts and evaluates them.
By default, graphics are placed at the potentially optimal position that is calculated by the compiler.
By moving them around, typographic blemishes like widows and orphans\footnote{The first (last) line of a paragraph appears alone as last (first) line on the previous (next) page, cf. \url{https://en.wikipedia.org/wiki/Widows_and_orphans}.} can be avoided.
As a consequence, graphics are not necessarily placed between the two text blocks that we specify, but at another position.
In order to reference a picture, that possibly is placed on another page, we can use labels, which are covered in \cref{sec:references}.
On top of that, we can limit the positioning of our image more or less rigorously by adding optional parameters to the \texttt{figure} environment. The available positioning shortcuts can be found in \cref{tbl:placement-abbreviations}.
\begin{table}[h!]
\centering
\begin{tabular}{cl}
\toprule
Shortcut & Position \\
\midrule
h & here, if possible \\
t & on top of the page \emph{(top)} \\
b & at the bottom of the page \emph{(bottom)} \\
p & on its own page \emph{(page)} \\
H & definitely here (requires package \texttt{float}) \\
\bottomrule
\end{tabular}
\caption{Shortcuts for positioning graphics}
\label{tbl:placement-abbreviations}
\end{table}
\codeblock{latex}{listings/graphics/positioning.tex}
Besides the vertical positioning, also the horizontal orientation may be of importance.
By default, graphics are left-justified.
The command \code{latex}{\textbackslash centering} centers all following objects in the current environment.
If we want the centering to affect only one object, we can alternatively wrap it with \code{latex}{\textbackslash begin\{center\}} and \code{latex}{\textbackslash end\{center\}}.