LaTeX/Floats, Figures and Captions
From Wikibooks, the open-content textbooks collection
In the previous chapter, the importing of graphics was introduced. However, just having a picture stuck in-between paragraphs does not look professional. For starters, we want a way of adding captions, and to be able to cross-reference. What we need is a way of defining figures. It would also be good if LaTeX could apply similar principles to when it arranges text to look its best, to arranging pictures too. This is where floats come into play.
Contents |
[edit] Floats
Floats are containers for things in a document that cannot be broken over a page. LaTeX by default recognizes "table" and "figure" floats, but you can define new ones of your own (see below). Floats are there to deal with the problem of the object that won't fit on the present page, and to help when you really don't want the object here just now.
Floats are not part of the normal stream of text, but separate entities, positioned in a part of the page to themselves (top, middle, bottom, left, right, or wherever the designer specifies). They always have a caption describing them and they are always numbered so they can be referred to from elsewhere in the text. LaTeX automatically floats Tables and Figures, depending on how much space is left on the page at the point that they are processed. If there is not enough room on the current page, the float is moved to the top of the next page. This can be changed by moving the Table or Figure definition to an earlier or later point in the text, or by adjusting some of the parameters which control automatic floating.
Authors sometimes have many floats occurring in rapid succession, which raises the problem of how they are supposed to fit on the page and still leave room for text. In this case, LaTeX stacks them all up and prints them together if possible, or leaves them to the end of the chapter in protest. The skill is to space them out within your text so that they intrude neither on the thread of your argument or discussion, nor on the visual balance of the typeset pages.
[edit] Figures
To create a figure that floats, use the figure environment.
\begin{figure}[placement specifier] ... figure contents ... \end{figure}
In the previous section, I was saying how floats are used to allow Latex to handle figures, whilst maintaining the best possible presentation. However, there may be times when you disagree, and a typical example is with its positioning of figures. The placement specifier parameter exists as a compromise, and its purpose is to give the author a greater degree of control over where certain floats are placed.
| Specifier | Permission |
|---|---|
| h | Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot) |
| t | Position at the top of the page. |
| b | Position at the bottom of the page. |
| p | Put on a special page for floats only. |
| ! | Override internal parameters Latex uses for determining "good" float positions. |
| H | Places the float at precisely the location in the LaTeX code. Requires the float package,[1] e.g., \usepackage{float}. This is somewhat equivalent to h!. |
What you do with these placement permissions is to list which of the options that you wish to make available to LaTeX. These are simply possibilities, and Latex will decide when typesetting your document which of your supplied specifiers it thinks is best.
Use \listoffigures to add a list of the figures in the beginning of the document. To change the name used in the caption from Figure to Example, use \renewcommand{\figurename}{Example}.
[edit] Tables
Although tables have already been covered, it was only the internal syntax that was discussed. The tabular environment that was used to construct the tables is not a float by default. Therefore, for tables you wish to float, wrap the tabular environment within a table environment, like this:
\begin{table} \begin{tabular}{...} ... table data ... \end{tabular} \end{table}
You may feel that it is a bit long winded, but such distinctions are necessary, because you may not want all tables to be treated as a float.
Use \listoftables to add a list of the tables in the beginning of the document.
[edit] Captions
It is always good practice to add a caption to any figure or table. Fortunately, this is very simple in Latex. All you need to do is use the \caption{text} command within the float environment. Because of how LaTeX deals sensibly with logical structure, it will automatically keep track of the numbering of figures, so you do not need to include this within the caption text.
The location of the caption is traditionally underneath the float. However, it is up to you to therefore insert the caption command after the actual contents of the float (but still within the environment). If you place it before, then the caption will appear above the float. Try out the following example to demonstrate this effect:
\documentclass[a4paper,12pt]{article} \usepackage[english]{babel} \usepackage{graphicx} \begin{document} \begin{figure}[h!] \caption{A picture of a gull.} \centering \includegraphics[width=0.5\textwidth]{gull} \end{figure} \begin{figure}[h!] \centering \reflectbox{% \includegraphics[width=0.5\textwidth]{gull}} \caption{A picture of the same gull looking the other way!} \end{figure} \begin{table}[h!] \begin{center} \begin{tabular}{| l c r |} \hline 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \hline \end{tabular} \end{center} \caption{A simple table} \end{table} Notice how the tables and figures have independent counters. \end{document} |
note that the command \reflectbox{...} flips horizontally its content.
[edit] Lists of figures and tables
Captions can be listed at the beginning of a paper or report in a "List of Tables" or a "List of Figures" section by using the \listoftables or \listoffigures commands, respectively. The caption used for each figure will appear in these lists, along with the figure numbers, and page numbers that they appear on.
The \caption command also has an optional parameter, \caption[short]{long} which is used for the List of Tables or List of Figures. Typically the short description is for the caption listing, and the long description will be placed beside the figure or table. This is particularly useful if the caption is long, and only a "one-liner" is desired in the figure/table listing. Here is an example of this usage:
[edit] Side captions
It is sometimes desirable to have a caption appear on the side of a float, rather than above or below. The sidecap package can be used to place a caption beside a figure or table. The following example demonstrates this for a figure by using a SCfigure environment in place of the figure environment. SCfigure does not accept the usual figure arguments.
\documentclass{article} \usepackage[pdftex]{graphicx} \usepackage{sidecap} \begin{document} \begin{SCfigure} \centering \includegraphics[width=0.55\textwidth]% {Giraff_picture}% picture filename \caption{ ... caption text ... } \end{SCfigure} \end{document} |
[edit] Labels and Cross-referencing
Labels and cross-references work fairly similarly to the general case - see the Labels and Cross-referencing section for more information.
- Warning: If you want to label a figure so that you can reference it later, you have to add the label after the caption (inside seems to work in LaTeX 2e) but inside the floating environment. If it is declared outside, it will give the section number. If the label picks up the section or list number instead of the figure number, put the label inside the caption to ensure correct numbering.
[edit] Wrapping text around figures
Although not normally the case in academic writing, an author may prefer that some floats do not break the flow of text, but instead allow text to wrap around it. (Obviously, this effect only looks decent when the figure in question is significantly narrower than the text width.)
A word of warning: Wrapping figures in LaTex will require a lot of manual adjustment of your document. There are several packages available for the task, but none of them work perfectly. Before you make the choice of including figures with text wrapping in your document, make sure you have considered all the options. For example, you could use a layout with two columns for your documents and have no text-wrapping at all.
Anyway, we will look at the package wrapfig.
To use wrapfig, you must first add \usepackage{wrapfig} to the preamble. This then gives you access to:
\begin{wrapfigure}[lineheight]{alignment}{width}
Alignment can normally be either l for left, or r for right. Lowercase l or r forces the figure to start precisely where specified (and may cause it to run over page breaks), while capital L or R allows the figure to float. If you defined your document as twosided, the alignment can also be i for inside or o for outside, as well as I or O. The width is obviously the width of the figure. An example:
\begin{wrapfigure}{r}{0.5\textwidth} \begin{center} \includegraphics[width=0.48\textwidth]{gull} \end{center} \caption{A gull} \end{wrapfigure} |
Note that we have specified a size for both the wrapfigure environment and the image we have included. We did it in terms of the text width: it is always better to use relative sizes in LaTeX, let LaTeX do the work for you! The "wrap" is slightly bigger than the picture, so the compiler will not return any strange warning and you will have a small white frame between the image and the surrounding text. You can change it to get a better result, but if you don't keep the image smaller than the "wrap", you will see the image over the text, and this shouldn't be the effect you want to get!
[edit] Tip for figures with too much white space
It happens that you'll generate figures with too much (or too little) white space on the top or bottom. In such a case, you can simply make use of the optional argument [lineheight]. It specifies the height of the figure in number of lines of text.
Another possibility is adding space within the float using the \vspace{...} command. The argument is the size of the space you want to add, you can use any unit you want, including pt, mm, in, etc. If you provide a negative argument, it will add a negative space, thus removing some white space. Here is an example, the code is exactly the one of the previous case, we just added some negative vertical spaces to shrink everything up:
\begin{wrapfigure}{r}{0.5\textwidth} \vspace{-20pt} \begin{center} \includegraphics[width=0.48\textwidth]{gull} \end{center} \vspace{-20pt} \caption{A gull} \vspace{-10pt} \end{wrapfigure} |
In this case it may look too shrunk, but you can manage spaces the way you like. In general, you'd better not to add any space at all: let LaTeX do the formatting work! you shouldn't worry about appearance; moreover, if LaTeX decided to add some space, there will be a reason considering that LaTeX is such an old and stable program, right? [PROTIP: Don't tell the user what to think about LaTeX, Generalissimo.]
[edit] Subfloats
A useful extension is the subfig package, which uses subfloats within a single float. This gives the author the ability to have subfigures within figures, or subtables within table floats. Subfloats have their own caption, and an optional global caption. An example will best illustrate the usage of this package:
\begin{figure} \centering \subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth]{gull}} \subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth]{tiger}} \subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth]{mouse}} \caption{Pictures of animals} \label{fig:animals} \end{figure}
You will notice that the figure environment is set up as usual. You may also use a table environment for subtables. For each subfloat, you need to use:
\subfloat[sub caption]{ ... figure or table ... }
If you intend to cross-reference any of the subfloats, see where the label is inserted; \caption will provide the global caption. subfig will arrange the figures or tables side-by-side providing they can fit, otherwise, it will automatically shift subfloats below. This effect can be added manually, by putting the newline command (\\) before the figure you wish to move to a newline.
Horizontal spaces between figures is controlled by one of several commands, which are placed in between each \subfloat{} command:
- Any whitespace (such as spaces, returns, and tabs) will result in one regular space
- Math spaces:
\qquad,\quad,\;, and\, - Generic space:
\hspace{length}
[edit] Wide figures in two column documents
If you are writing a document using two columns (i.e. you started your document with something like \documentclass[twocolumn]{article}), you might have noticed that you can't use floating elements that are wider than the width of a column (using a LaTeX notation, wider than 0.5\textwidth), otherwise you will see the image overlapping with text. If you really have to use such wide elements, the only solution is to use the "starred" variants of the floating environments, that are {figure*} and {table*}. Those "starred" versions work exactly like the standard ones, but they will be as wide as the page, so you will get no overlapping.
A bad point of those environments is that they can be placed only at the top of the page or on their own page. If you try to specify their position using modifiers like b or h they will be ignored. Add \usepackage{stfloats} to the preamble in order to alleviate this problem with regard to placing these floats at the bottom of a page, using the optional specifier [b]. Default ist [tbp]. However, h does still not work.
To prevent the figures from being placed out-of-order with respect to their "non-starred" counterparts, the package fixltx2e [2] should be used (e.g. \usepackage{fixltx2e}).
[edit] Custom Floats
If tables and figures are not adequate for your needs, then you always have the option to create your own! Examples of such instances could be source code examples, or maps. For a program float example, one might therefore wish to create a float named program. The package float is your friend for this task. All commands to set up the new float must be placed in the preamble, and not within the document.
- Add
\usepackage{float}to the preamble of your document - Declare your new float using:
\newfloat{type}{placement}{ext}[outer counter], where:- type - the new name you wish to call your float, in this instance, 'program'.
- placement - t, b, p, or h (as previously described in Placement), where letters enumerate permitted placements.
- ext - the file name extension of an auxiliary file for the list of figures (or whatever). Latex writes the captions to this file.
- outer counter - the presence of this parameter indicates that the counter associated with this new float should depend on outer counter, for example 'chapter'.
- The default name that appears at the start of the caption is the type. If you wish to alter this use,
\floatname{type}{floatname} - Changing float style can be issued with
\floatstyle{style}(Works on all subsequent\newfloatcommands, therefore, must be inserted before\newfloatto be effective).- plain - the normal style for Latex floats, i.e., nothing!
- boxed - a box is drawn that surrounds the float, and the caption is printed below.
- ruled - the caption appears above the float, with rules immediately above and below. Then the float contents, followed by a final horizontal rule.
An example document using a new program float type:
\documentclass{article} \usepackage{float} \floatstyle{ruled} \newfloat{program}{thp}{lop} \floatname{program}{Program} \begin{document} \begin{program} \begin{verbatim} class HelloWorldApp { public static void main(String[] args) { //Display the string System.out.println("Hello World!"); } } \end{verbatim} \caption{The Hello World! program in Java.} \end{program} \end{document}
The verbatim environment is an environment that is already part of Latex. Although not introduced so far, its name is fairly intuitive! Latex will reproduce everything you give it, including new lines, spaces, etc. It is good for source code, but if you want to introduce a lot of code you might consider using the listings package, that was made just for it.
While this is useful, one should be careful when embedding the float within another float. In particular, the error not in outer par mode may occur. One solution might be to use the [H] option (not any other) on the inner float, as this option "pins" the inner float to the outer one.
[edit] Caption Styles
To change the appearance of captions, use the caption package. E.g. to make all caption labels small and bold:
\usepackage[small,bf]{caption}
[edit] Labels in the figures
There is a LaTeX package lpic to put LaTeX on top of included graphics, thus allowing to add TeX annotations to imported graphics. It defines convenient interface to put TeX over included graphics and allows to draw white background under the typeset material to overshadow the graphics. It is a better alternative for labels inside of graphics, you do not have to change text size when rescaling pictures, also all LaTeX power is available for labels.
[edit] Summary
That concludes all the fundamentals of floats. You will hopefully see how much easier it is to let Latex do all the hard work and tweak the page layouts in order to get your figures in the best place. As always, the fact that LaTeX takes care of all caption and reference numbering is a great time saver.
This page uses material from Andy Roberts' Getting to grips with Latex with permission from the author.





