L^{A}TEX courses Course 3: Tables and images

Isabelle HURBAIN ihurbain@isabelle-hurbain.com August 4, 2009

Contents

1 Introduction 1 2 Tables 1 3 Images 3 4 Floating objets 4 5 In the next course 6

1 Introduction

In this course, we will talk about tables and images, and about the concept of floatings objects.

2 Tables

It is often useful to put tables in documents. It is really easy to do it with L^{A}TEX: let’s see how.

A first example could be this one :

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

This makes this :

Fruit Color
Banana Yellow
Grape Violet

Okay, not very impressive indeed. The table environment is delimited by

\begin{tabular} and \end{tabular}. The {cc} argument tells L^{A}TEX we

want two columns, with centered text in them. The & delimits the columns and the \\ 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 :

Fruit Color
Banana Yellow
Grape Violet

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

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

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

Fruit Color
Banana Yellow
Grape Violet

You can also try other alignments in the table (instead of c): l aligns left, r aligns right, and \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 :

Title This is the title Content This is the content of a 5-cm-wide cell! Easy isn’t it ?

and is done like this :

\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}

It is of course possible to center the table, by adding a \begin{center} at the beginning and a \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 4 how to fix that.

There are other formats of tables, like longtable or tabularx. longtable allows you to put a table on several pages, and tabularx allows you to do fixed-width tables with a “floating” dimension. Read the L^{A}TEX Companion for more information about this topic.

3 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 :

• 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).

• 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

• any solution is useable, but you must ensure that the resulting EPS has a bounding box (or L^{A}TEX will violently complain about it).

To insert images, you have to put the command

\usepackage[dvips]{graphicx}

in your preamble. It tells L^{A}TEX 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

\includegraphics{foo.eps}

to insert foo.eps.

You can rescale your image with the settings width, height and 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 scale allows you to use a percentage of the real size of the image. You use it as showed below:

\includegraphics[width=8cm]{foo.eps}

4 Floating objets

Floating objects are a way to integrate your tables and images much more smoothly. The idea is to tell L^{A}TEX “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{table}{!ht}

at the beginning and

\end{table}

at the end of your table.

It will tell L^{A}TEX to put your table “as much as possible” (!) “here” (h) or “at the top of a page” (t).

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

To have a floating image in your document, use

\begin{figure}{!ht}

at the beginning and

\end{figure}

at the end of your image.

The syntax is exactly the same as for table.

So your floating object is, well, floating, so you can’t say in your text “You can see below the results… ” and insert your image, as you can’t say if L^{A}TEX 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

\caption{The title of the figure}

Fruit Color
Banana Yellow
Grape Violet

Table 1: Correspondance between fruits and their colors

inside the figure or table environment. L^{A}TEX will automatically generate a number (like for the chapters for example) to allow the referencing. To get this number, you can use

\label{img:imgref}

which must be put after the caption.

The \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 1.

This is done with:

\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}

and the reference is done using

\ref{tab:example}

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 placeins that provides the command

\FloatBarrier. To use it, put
\usepackage{placeins}

in the preamble of your document and use \FloatBarrier whenever you want to flush the floating objects buffer. However, be careful : as it goes against L^{A}TEX rules of floating objects’ placement, you may have bad surprises (for example, a page half empty before the first floating object).

5 In the next course

In the next course we will speak about what makes the real force of L^{A}TEX: typesetting math expressions.