\documentclass[a4paper, 11pt]{article}
\title{\LaTeX{} courses\\ Course 3: Tables and images}
\author{Isabelle HURBAIN \\ ihurbain@isabelle-hurbain.com}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
In this course, we will talk about tables and images, and about the concept of floatings objects.

\section{Tables}

It is often useful to put tables in documents. It is really easy to do it with \LaTeX{}: let's see how.

A first example could be this one :

\begin{verbatim}
\begin{tabular}{cc}
Fruit & Color\\
Banana & Yellow\\
Grape & Violet
\end{tabular}
\end{verbatim}

This makes this :

\begin{tabular}{cc}
Fruit & Color\\
Banana & Yellow\\
Grape & Violet\\
\end{tabular}

Okay, not very impressive indeed. The table environment is delimited by \verb+\begin{tabular}+ and \verb+\end{tabular}+. The \verb+{cc}+ argument tells \LaTeX{} we want two columns, with centered text in them. The \verb+&+ delimits the columns and the \verb+\\+ delimits the lines.

This might seem a bit complex but once you're used to it, it is really powerful and much quicker to use than any GUI.

However, it is a bit simple and raw at the time.

Something like this would be much nicer :

\begin{tabular}{|c|c|}
\hline
Fruit & Color\\
\hline
\hline
Banana & Yellow\\
Grape & Violet\\
\hline
\end{tabular}

And it is not much more complicated. You'll have to add the columns delimiters \verb+|+ in the table definition, and the horizontal lines with \verb+\hline+, like this :

\begin{verbatim}
\begin{tabular}{|c|c|}
\hline
Fruit & Color\\
\hline
\hline
Banana & Yellow\\
Grape & Violet\\
\hline
\end{tabular}
\end{verbatim}

Of course, you can use as much \verb+|+ and \verb+\hline+ you wish, you could easily do something like this (try it as an exercise!):

\begin{tabular}{||c|c||}
\hline
\hline
Fruit & Color\\
\hline
\hline
Banana & Yellow\\
\hline
Grape & Violet\\
\hline
\hline
\end{tabular}

You can also try other alignments in the table (instead of \verb+c+): \verb+l+ aligns left, \verb+r+ aligns right, and \verb+\p{3cm}+ allows you to get several lines of text in a same cell. This cell has a width of 3cm as put in argument. An example of this feature is below :

\begin{tabular}{|c|p{5cm}|}
\hline
Title & This is the title \\
\hline
Content & This is the content of a 5-cm-wide cell! Easy isn't it ?\\
\hline
\end{tabular}

and is done like this : 
\begin{verbatim}
\begin{tabular}{|c|p{5cm}|}
\hline
Title & This is the title \\
\hline
Content & This is the content of a 5-cm-wide cell! Easy isn't it ?\\
\hline
\end{tabular}
\end{verbatim}

It is of course possible to center the table, by adding a \verb+\begin{center}+ at the beginning and a \verb+\end{center}+ at the end of the table.

The table is not yet very well placed (much to close to the text), and it can be misplaced (at the very end of the page for example). We will see in section \ref{floating} how to fix that.

There are other formats of tables, like \verb+longtable+ or \verb+tabularx+. \verb+longtable+ allows you to put a table on several pages, and \verb+tabularx+ allows you to do fixed-width tables with a ``floating'' dimension. Read the \LaTeX{} Companion for more information about this topic.

\section{Images}
You can also put images in your documents. The first thing to do is to convert your images in EPS (Encapsulated PostScript). To achieve this you have several solutions :
\begin{itemize}
\item convert them directly from another format (with The Gimp for example): save your image with a .eps extension, or choose the Postscript format (but do not forget to tick the ``Encapsulated PostScript'' case).
\item if you make your own graphics, you can save them directly in .eps. For vectorial drawings, you can use tgif or xfig, which make good EPS exports. OpenOffice.org achieves it as well, except that you have to tell it to export only the selection not to have the whole page exported. Plus, I got some problems with fonts... so be careful
\item any solution is useable, but you must ensure that the resulting EPS has a bounding box (or \LaTeX{} will violently complain about it).
\end{itemize}

To insert images, you have to put the command 
\begin{verbatim}
\usepackage[dvips]{graphicx}
\end{verbatim}
in your preamble. It tells \LaTeX{} that there will be images in EPS format in the document, and that the rendering package is dvips.

To insert an image, you will insert
\begin{verbatim}
\includegraphics{foo.eps}
\end{verbatim}
to insert \verb+foo.eps+. 

You can rescale your image with the settings \verb+width+, \verb+height+ and \verb+scale+. You can specify a width and/or a height for your image (if only one is specified, the image will conserve its proportion), and \verb+scale+ allows you to use a percentage of the real size of the image. You use it as showed below:
\begin{verbatim}
\includegraphics[width=8cm]{foo.eps}
\end{verbatim}

\section{Floating objets}
\label{floating}

Floating objects are a way to integrate your tables and images much more smoothly. The idea is to tell \LaTeX{} ``Okay, I'd like to put this image around this place and this table around that place, now make your little business and do it properly''. Plus, they provide interesting features such as a caption, a label for referencing and lists of figures and tables.

To have a floating table in your document, use
\begin{verbatim}
\begin{table}{!ht}
\end{verbatim}
at the beginning and
\begin{verbatim}
\end{table}
\end{verbatim}
at the end of your table.

It will tell \LaTeX{} to put your table ``as much as possible'' (\verb+!+) ``here'' (\verb+h+) or ``at the top of a page'' (\verb+t+).

Your table will be placed considering these settings; you can also use \verb+b+ to put the table at the bottom of a page, or \verb+p+ to put the table on a separated page containing only floating objects.

To have a floating image in your document, use
\begin{verbatim}
\begin{figure}{!ht}
\end{verbatim}
at the beginning and
\begin{verbatim}
\end{figure}
\end{verbatim}
at the end of your image. 

The syntax is exactly the same as for \verb+table+.

So your floating object is, well, floating, so you can't say in your text ``You can see below the results\dots'' and insert your image, as you can't say if \LaTeX{} will put it below your text, or at another place.

The work-around for this is to use references.

The first thing to do is to put a title to your floating object. You achieve this by putting 
\begin{verbatim}
\caption{The title of the figure}
\end{verbatim}
inside the figure or table environment. \LaTeX{} will automatically generate a number (like for the chapters for example) to allow the referencing. To get this number, you can use
\begin{verbatim}
\label{img:imgref}
\end{verbatim}
which must be put after the caption.

The \verb+\label+ gives a name to a given part of the document (here, a table or an image) to allow referencing. As an example is better than long talkings, look at table \ref{tab:example}.

\begin{table}
\begin{center}
\begin{tabular}{|c|c|}
\hline
Fruit & Color \\
\hline
Banana & Yellow \\
\hline
Grape & Violet\\
\hline
\end{tabular}
\caption{Correspondance between fruits and their colors}
\label{tab:example}
\end{center}
\end{table}

This is done with: 
\begin{verbatim}
\begin{table}
\begin{center}
\begin{tabular}{|c|c|}
\hline
Fruit & Color \\
\hline
Banana & Yellow \\
\hline
Grape & Violet\\
\hline
\end{tabular}
\caption{Correspondance between fruits and their colors}
\label{tab:example}
\end{center}
\end{table}
\end{verbatim}
and the reference is done using 
\begin{verbatim}
\ref{tab:example}
\end{verbatim}

It sometimes happens that you wish to have the image or the table you are talking about in a certain section (I like to have my graphs in the same section on a laboratory report for example). You can force the ``flushing'' of all floating objects using the extension \verb+placeins+ that provides the command \verb+\FloatBarrier+. To use it, put
\begin{verbatim}
\usepackage{placeins}
\end{verbatim}
in the preamble of your document and use \verb+\FloatBarrier+ whenever you want to flush the floating objects buffer. However, be careful : as it goes against \LaTeX{} rules of floating objects' placement, you may have bad surprises (for example, a page half empty before the first floating object).
\section{In the next course}
In the next course we will speak about what makes the real force of \LaTeX{}: typesetting math expressions.
\end{document}