LaTeX/Floats, Figures and Captions

The previous chapter introduced importing graphics. However, just having a picture stuck in between paragraphs does not look professional. To start with, 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 principles similar to when it arranges text to look its best to arrange pictures as well. This is where floats come into play.

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 Custom floats 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.

As with various other entities, there exist limitations on the number of unprocessed (placed) floats in line. LaTeX by default can cope with maximum 18 floats and a symptomatic error is:

! LaTeX Error: Too many unprocessed floats.

The package lifts this limit.

Figures
To create a figure that floats, use the environment.

The previous section mentioned how floats are used to allow LaTeX to handle figures while 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.

What you do with these placement permissions is to list which of the options 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. Frank Mittelbach describes the algorithm :


 * If a float is encountered, LaTeX attempts to place it immediately according to its rules (detailed later)
 * If this succeeds, the float is placed and that decision is never changed;
 * If this does not succeed, then LaTeX places the float into a holding queue to be reconsidered when the next page is started (but not earlier).
 * Once a page has finished, LaTeX examines this holding queue and tries to empty it as best as possible. For this, it will first try to generate as many float pages as possible (in the hope of getting floats off the queue). Once this possibility is exhausted, it will next try to place the remaining floats into top and bottom areas. It looks at all the remaining floats and either places them or defers them to a later page (i.e., re-adding them to the holding queue once more).
 * After that, it starts processing document material for this page. In the process, it may encounter further floats.
 * If the end of the document has been reached or if a \clearpage is encountered, LaTeX starts a new page, relaxes all restrictive float conditions, and outputs all floats in the holding queue by placing them on float page(s).

In some special cases, LaTeX won't follow these positioning parameters and additional commands will be necessary, for example, if one needs to specify an alignment other than centered for a float that sits alone in one page.

Use 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 in the figure contents. See the New commands section for more information on this.

Figures with borders
It's possible to get a thin border around all figures. You have to write the following once at the beginning of the document:

The border will not include the caption.

Tables
Floating tables are covered in a separate chapter. Let's give a quick reminder here. The environment that was used to construct the tables is not a float by default. Therefore, for tables you wish to float, wrap the environment within a  environment, like this:

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 to add a list of the tables in the beginning of the document.

Keeping floats in their place
The package provides the command, which can be used to prevent floats from being moved over it. This can, e.g., be useful at the beginning of each section. The package even provides an option to change the definition of to automatically include a. This can be set by loading the package with the option. may also be useful to prevent floats intruding on lists created using or.

The package can be used to force floats to appear after they are defined, and the  package can be used to place all floats at the end of a document.

The package provides the   option to floating environments, which completely stops them from floating.

Package provides the command  that lets you typeset a caption without a floating environment. You have the full and absolute control about the placement of your figures and captions.

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 command within the float environment. LaTeX 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:

Note that the command flips its content horizontally.

Side captions
It is sometimes desirable to have a caption appear on the side of a float, rather than above or below. The package can be used to place a caption beside a figure or table. The following example demonstrates this for a figure by using a environment in place of the  environment. The package is newer and has more capabilities.

Unnumbered captions
In some types of documents (such as presentations), it may not be desirable for figure captions to start with Figure:. This is easy to suppress by just placing the caption text in the  environment, without enclosing it in a. This however means that there is no caption available for inclusion in a list of figures.

Caption Setup Rename
In case you want to rename your table caption from "Table" to something else, you can use the command. For example,

\usepackage{caption} \captionsetup[table] {name=New Table Name}

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 or  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 command also has an optional parameter,  which is used for the List of Tables or List of Figures. Typically the description is for the caption listing, and the  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:

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.

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. If you get an error when the label is inside the caption, use in front of the  command.

Wrapping text around figures
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. Note that may not come with the default installation of LaTeX; you might need to install additional packages. Noted also, wrapfig is incompatible with the enumerate and itemize environments

To use, you must first add this to the preamble:

This then gives you access to:

The lineheight is expressed as the number of lines of text the figure spans. LaTeX will automatically calculate the value if this option is left blank but this can result in figures that look ugly (with too much spacing). The LaTeX calculation is manually overridden by entering the number of lines you would like the figure to span. This option can't be entered in pt, mm, etc...

There are overall eight possible positioning targets:

The uppercase-character allows the figure to float, while the lowercase version means "exactly here".

The overhang of the figure can be manually set using the overhang option in pt, cm, etc...

The width is, of course, the width of the figure. An example:

You can also allow LaTeX to assign a width to the wrap by setting the width to 0pt:

Note that we have specified a size for both the 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.

The wrapfig package can also be used with user-defined floats with float package. See below in the section on custom floats.

Tip for figures with too much white space
You can use parameter to control additional space above and below the figure:

To control the horizontal space between the image and the text, use the parameter.

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. It specifies the height of the figure in the number of lines of text. Also remember that the environment adds some extra white space at its top and bottom; consider using the command  instead.

Another possibility is adding space within the float using the 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. Using tends to move the caption relative to the float while the  argument does not. Here is an example using the command, the code is exactly the one of the previous cases, we just added some negative vertical spaces to shrink everything up:

In this case, it may look too shrunk, but you can manage spaces the way you like. In general, it is best not to add any space at all: let LaTeX do the formatting work!

(In this case, the problem is the use of to center the image. The  environment adds extra space that can be avoided if  is used instead.)

Alternatively, you might use the package instead of the wrapfig package which produces a correct version without the excess white space out of the box without any hand-tuning.

There is also an alternative to : the package.

To remove the white space from a figure once for all, one should refer to the program pdfcrop, included in most TeX installations.

Subfloats
A useful extension is the package, which uses subfloats within a single float. The package ( package is deprecated ) is a useful alternative when used in conjunction with LaTeX templates (i.e. templates for journals from Springer and IOP, IEEETran and ACM SIG) that are not compatible with. These packages give 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 the package:



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:

If you intend to cross-reference any of the subfloats, see where the label is inserted; outside the -environment will provide the global caption.

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 are controlled by one of several commands, which are placed in between and :
 * A non-breaking space (specified by ~ as in the example above) can be used to insert a space in between the subfigs.
 * Math spaces:, , , and
 * Generic space:
 * Automatically expanding/contracting space:

Figures in multiple parts
Sometimes you need to divide up a figure over multiple floats, for instance because the figure is too big to fit on one page. In this case you can use continued figures using the  package.

Put this in your preamble:

Then you can use continued floats as follows.

Wide figures in two-column documents
If you are writing a document using two columns (i.e. you started your document with something like ), 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 ), 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 and. Those "starred" versions work 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 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. Default is. However, h still does not work.

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. The package 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.


 * 1) Add  to the preamble of your document
 * 2) Declare your new float using:, where:
 * 3) * type - the new name you wish to call your float, in this instance, 'program'.
 * 4) * placement - t, b, p, or h (as previously described in Placement), where letters enumerate permitted placements.
 * 5) * ext - the file name extension of an auxiliary file for the list of figures (or whatever). LaTeX writes the captions to this file.
 * 6) * outer counter - the presence of this parameter indicates that the counter associated with this new float should depend on outer counter, for example 'chapter'.
 * 7) The default name that appears at the start of the caption is the type. If you wish to alter this, use
 * 8) Changing float style can be issued with  (Works on all subsequent  commands, therefore, must be inserted before  to be effective).
 * 9) * - the normal style for LaTeX floats, but the caption is always below the content.
 * 10) * - the normal style for LaTeX floats, but the caption is always above the content.
 * 11) * - a box is drawn that surrounds the float, and the caption is printed below.
 * 12) * - the caption appears above the float, with rules immediately above and below. Then the float contents, followed by a final horizontal rule.

Float styles can also be customized as the second example below illustrates.

An example document using a new float type:

The is an environment already discussed in Special paragraphs. It is good for source code, but if you want to introduce a lot of code you might consider using the package described in Source Code Listings, 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.

Newly created floats with can also be used in combination with the  package from above. E.g. the following code creates a floating text box, which floats in the text on the right side of the page and is complete with caption, numbering, an index file with the extension .lob and a customization of the float's visual layout:

Caption styles
To change the appearance of captions, use the  package. For example, to make all caption labels small and bold:

The KOMA script packages have their own caption customizing features with e.g.,  and. However these definitions have limited effect on newly created float environments with the package.

Alternatively, you can redefine the command:

See this page for more information on counters. Finally, note that the package has long been deprecated.

Labels in the figures
There is a LaTeX package  to put LaTeX on top of included graphics, thus allowing to add TeX annotations to imported graphics. It defines a convenient interface to put TeX over included graphics, and allows for drawing a 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, and all LaTeX power is available for labels.

A very similar package, with somewhat different syntax, is. The link given also points to the packages and.

A much more complicated package which can be used in the same way is TikZ. TikZ is a front-end to a drawing library called pgf (which is used to make beamer for instance). It can be used to label figures by adding text nodes on top of an image node.

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.