latex-skript/content/graphics.tex

61 lines
3.0 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 text 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:
\begin{minted}[tabsize=4]{latex}
\begin{figure}
\includegraphics{<file path>}
\caption[<short title>]{<caption>}
\end{figure}
\end{minted}
\noindent The command \mintinline{latex}{includegraphics} can be used to change the image size. The desired height and width of the figure can be indicated separately, like illustrated by the following example:
\begin{minted}{latex}
\includegraphics[width=0.5\textwidth,height=5cm]{<file path>}
\end{minted}
\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.
By default, the graphic is placed at the potentially optimal position that is calculated by the compiler. It creates multiple layouts and evaluates them. By moving graphics, 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 bage \emph{(bottom)} \\
p & on its own apge\emph{(page)} \\
H & Definitely here! (requires package \texttt{float}) \\
\bottomrule
\end{tabular}
\caption{Shortcuts for Positioning Graphics}
\label{tbl:placement-abbreviations}
\end{table}
\begin{minted}[tabsize=4]{latex}
\begin{figure}[<position shortcut>]
\centering
\includegraphics{<file path>}
\end{figure}
\end{minted}
Besides the vertical positioning, also the horizontal orientation may be of importance.
By default, graphics are left-justified.
The command \mintinline{latex}{\centering} centers all following objects in the current environment.
If we want the centering to affect only one object, we can alternatively wrap the figure with \mintinline{latex}{\begin{center}} and \mintinline{latex}{\end{center}}.