\chapter{Graphics}\label{sec:grap}

\begin{figure}[tb]
\topline
\includegraphics[width=\exacolwidth]{../figures/lijb512}\hfill
\includegraphics[width=\exacolwidth]{../figures/lijb64}
\midline
\includegraphics[width=\exacolwidth]{../figures/macos9a}\hfill
\includegraphics[width=\exacolwidth]{../figures/mac64.png}
\botline
\caption{Bitmapped- or raster graphics: above a photograph, below a
  screenshot, both with an enlarged detail at the
  right}\label{bitmaps}
\end{figure}

\begin{figure}[tbp]
\topline
\includegraphics[width=\exacolwidth]{../figures/libochart}\hfill
\includegraphics
  [clip,width=\exacolwidth,viewport=213 53 338 103]{../figures/libochart}
\midline
\includegraphics[height=1.7in]{../figures/penguins}\hfill
\includegraphics
  [clip,height=1.7in,viewport=60 125 200 225]{../figures/penguins}
\midline
\includegraphics[height=1.75in]{../figures/pgfdemo}\hfill
\includegraphics
  [clip,height=1.7in,viewport=81 75 152 120]{../figures/pgfdemo}\par
\botline
\caption{Vector art: a LibreOffice data plot, a drawing created with
  Skencil and Inkscape and a function plot generated with
  pgfplots}\label{fig:vectors}
\end{figure}

\begin{figure}[t]
\topline
\includegraphics%
  [width=\exacolwidth,viewport=0 0 359 267]{../figures/theend}\hfill
\includegraphics%
  [clip,width=\exacolwidth,viewport=160 62 200 92]{../figures/theend}\par
\botline
\caption{Raster and vector combined}\label{theend}
\end{figure}

Broadly speaking, there are two ways to get pictures into your
\LaTeX{} output:
\begin{enumerate}
\item Create graphics externally, and load them with \LaTeX{} commands
\item Add picture code directly to the \LaTeX{} source.
\end{enumerate}
The TikZ package offers a convenient general-purpose set of macros for
programming diagrams, and there are several other options.
However, a big subject such as TikZ is beyond the scope of this
introduction; here we shall only look at external graphics.

\section{External graphics}

Before we go any further, you should have some rudimentary
understanding of graphics file formats.  The most important
distinction is between bitmaps and vectors.\enlargethispage*{13bp}

Bitmaps are built up from pixels, \emph{i.e.}, tiny blocks
of solid color. The smaller the blocks, the sharper the picture and
the bigger the file. If you scale them up too far, the blocks become
apparent, see figure~\ref{bitmaps}.

Vector graphics are built up from mathematical shapes: lines, arcs,
b\'ezier curves, text objects, see figure~\ref{fig:vectors}. They
scale well. Avoid converting vector graphics to  bitmap.

Pdflatex and the other \TeX{} engines can only work with certain
types of graphic files:\enlargethispage*{-13bp}
\begin{list}{}{%
    \leftmargin=2.2em
    \labelwidth=\leftmargin
    \labelsep=0pt
    \def\makelabel#1{\textbf{#1}\hfill}}
  \item[pdf] can contain both bitmapped and vector elements.
  \item[eps] is closely related to pdf and can also contain both
    bitmapped and vector elements. It will be converted behind the
    scenes to pdf, at least if the \TeX{} installation allows
    it\footnote{If you need more control over the eps to pdf
      conversion, or need conversion the other way, or need to crop
      margins, have a look at epspdftk, available as the
      \emph{PostScript- and pdf conversions} utility in the
      Utilities submenu of the RuG \TL{} launcher, and at its
      command-line back end epspdf.}.
  \item[png] is a bitmapped format. It is first choice for
    screenshots.
  \item[jpg] or jpeg is a bitmapped format with lossy
    compression\footnote{To reduce file size, bitmapped images are
      usually compressed. For png this is done in a lossless way,
      \emph{i.e.}, the png image contains exactly the same pixels
      as the original uncompressed image. Jpeg is compressed in a
      lossy way, \emph{i.e.}, you cannot recreate the exact original
      image from the jpeg. Nevertheless, jpeg compression works very
      well for photographic images. These can be reduced to 10\% of
      their original file size without visible loss of quality.}. It
    is first choice for photographic images.
\end{list}

\section{Producers of graphic files}

Mathematical software (R, MATLAB, Octave, Gnuplot) can generate eps
and sometimes pdf.

Professional illustration software can usually export to eps and
pdf. Inkscape is a capable free alternative to commercial products
such as Adobe Illustrator and CorelDRAW.

OpenOffice/LibreOffice and MS Office can export documents and
selections of documents to pdf.

Figure~\ref{fig:vectors} shows two vector graphic files created by
external programs and one created by a \LaTeX{} macro package.

I am not going to list programs for bitmapped graphics. There are
many good ones, often free or inexpensive.

Download \citetitle{externalgraphics} for a more in-depth although
not quite up-to-date discussion.

\section{Including an external graphics file}

Graphics inclusion is not built into the \LaTeX{} core. The graphicx
package provides this facility. You need to load it in the preamble
with
\begin{verbatim}
\usepackage{graphicx}
\end{verbatim}

You can place a figure in your document with code such as
\begin{verbatim}
\includegraphics{APicture}
\end{verbatim}
Normally, you don't need to specify the extension. Pdflatex will
look for \path{APicture.jpg}, \path{APicture.png} and
\path{APicture.pdf}.

With the above code, the graphic file should be in the same
directory as your .tex file. With a command
\begin{verbatim}
\includegraphics{figures/APicture}
\end{verbatim}
pdflatex will look in the \path{figures} subdirectory.

{\sloppy Make sure to use a relative path, forward slashes and make
  sure that there are no
  spaces or funny characters in file- or directory names:
  `\path{figures/APicture}' is fine,
  %`\verb+c:\Documents and Settings+\dbr\verb+\your name+\dbr
  %\verb+\A picture+'
  `\path{c:\Documents and Settings\your name\A picture}'
  probably is not. The \TS{} Insert Graphics wizard tries
  to produce the right syntax.\par}

\nextblock
If the picture is too large or too small, you can scale it to the
desired size with a \texttt{width} or \texttt{height} parameter:
\exa
\begin{verbatim}
\includegraphics[height=.3in]{figures/mouse}
\end{verbatim}
\exb
\includegraphics[height=.3in]{../figures/mouse}
\exc

\nextblock
Sometimes `\texttt{width=\cmd{linewidth}}' may come in handy.

You can also rotate a picture with an \texttt{angle} parameter.
Figure~\ref{fig:float} has been inserted with
\begin{verbatim}
\includegraphics[width=.7in,angle=180]{figures/mouse}
\end{verbatim}
\begin{figure}[b]
  \centering
  \includegraphics[width=.7in,angle=180]{../figures/mouse}
  \caption{An upside-down figure}\label{fig:float}
\end{figure}

\section{Floating figures and tables}\label{sec:float}

If you place large objects such as figures or tables at their
natural position in the text stream, you tend to get awkward page
breaks. Therefore, they are usually placed inside a `float', which
means in \LaTeX-speak an environment which may be moved elsewhere: to,
\emph{e.g.}, the top or bottom of a page, or to a page by itself.

\LaTeX{} defines two float environments: the \texttt{table}- and the
\texttt{figure} environment. It is possible to define more. Figure-
and table floats are numbered separately.

Within both environments, a \cmd{caption} command is defined. In the
examples below there is a \cmd{label} command after the \cmd{caption}
command for cross-referencing.

Table~\ref{tab:rules} on page~\pageref{tab:rules} has been placed
with the following code:
\begin{verbatim}
\begin{table}[t]
  \caption{Fewer rules are usually better}
  \label{tab:rules}
  \centering
  \begin{tabular}[t]{lrr}
  ...
  \end{tabular}
\end{table}
\end{verbatim}
and Figure~\ref{fig:float} on page~\pageref{fig:float} with:
\begin{verbatim}
\begin{figure}[b]
  \centering
  \includegraphics[width=.7in,angle=180]{figures/mouse}
  \caption{An upside-down figure}\label{fig:float}
\end{figure}
\end{verbatim}
Codes \texttt{[t]} and \texttt{[b]} are optional \emph{placement
specifiers.}  They indicate preferred placement of the float on the
page. Use any combination of \texttt{b} (bottom), \texttt{t} (top),
\texttt{h} (here) or \texttt{p} (a page with only floats). Default:
\texttt{[tbp]}.

Note also the \cmd{centering} command for centering the content of
the environment. This command has no effect on the caption.

If you have many floating figures and tables, it helps
placement if you have some or all of the following commands in the
preamble\footnote{You can copy-and-paste this code from the practice
  file \path{float_sample.tex}.}:
\begin{verbatim}
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{3}
\setcounter{dbltopnumber}{2}
\renewcommand{\topfraction}{.9}
\renewcommand{\textfraction}{.1}
\renewcommand{\bottomfraction}{.75}
\renewcommand{\floatpagefraction}{.9}
\renewcommand{\dblfloatpagefraction}{.9}
\renewcommand{\dbltopfraction}{.9}
\end{verbatim}
With these commands, \LaTeX{} is more willing to put several floats on a
single page and to devote a larger portion of the page to floats
without resorting to a dedicated float page.

Wrapping text around a figure requires an additional package. There
are several to choose from, but the \citetitle{ctancat} recommends
\package{wrapfig} and \package{floatflt}.

\section {Practice documents for graphics and floats}\label{sec:grap_practice}

The file \path{float_sample.tex} demonstrates both graphics inclusion
and floats (several figures and one table).

The \path{figures} subdirectory contains graphics files used in
\path{float_sample.tex}. All the files in this directory, with the
exception of \path{diamond.eps}, can be loaded directly by
pdflatex, and the latter file will be converted on-the-fly to pdf
if the \TeX{} installation allows it.
