Floats, figures and captions | Getting to Grips with LaTeX (2024)

Floats, figures and captions

by Andrew Roberts

In the previoustutorial, the importing of graphics was introduced. However, justhaving a picture stuck in-between paragraphs does not look professional.For starters, we want a way of adding captions, and to be able tocross-reference. What we need is a way of defining figures. Itwould also be good if LaTeX could apply similar principles to when itarranges text to look its best, to arranging pictures too. This iswhere floats come into play.

Floats

Floating elements refer to anything within a document thatcannot be broken over a page. This basically boils down totables and figures. They require special treatment, and theconcept of floating was the solution for a robustmethod for dealing with such elements, whilst keeping thedocument presentation as `nice' as possible.

The most common problem is that there is not enough space on theremainder of a given page to fit the specified figure. Toovercome this, LaTeX will float this over to the next page,whilst filling the current page with body text. Contrast this towhat happens in MS Word, for example. If a image is added butis too large to fit on the current page, it will position it onthe next page, but, will leave a large gap, instead ofrearranging subsequent text to fill the space. It requires a lotof manual tweaking to rectify, whereas LaTeX takes careof all of this automatically.

Figures

To create a figure, you must use the figure environment (tricky, eh?!).

\begin{figure}[placement specifier]
... figure contents ...
\end{figure}

In the previous section, I was saying how floats are used toallow LaTeX to handle figures, whilst maintaining the bestpossible presentation. However, there may be times when youdisagree, and a typical example is with its positioning offigures. The placement specifier parameter exists as acompromise, and its purpose is to give the author a greaterdegree of control over where certain floats are placed.

SpecifierPermission
hPlace the float here, i.e., at the same point it occurs inthe source text.
tPosition at the top of the page.
bPosition at the bottom of the page.
pPut on a special page for floats only.
!Override internal parameters LaTeX uses for determining `good' float positions.

What you do with these placement permissions isto list which of the options that you wish to make available to LaTeX.These are simply possibilities, and LaTeX will decide when typesettingyour document which of your supplied specifiers it thinks is best.

Tables

Although tables have already been covered, it was only theinternal syntax that was discussed. The tabularenvironment that was used to construct the tables is not a floatby default. Therefore, for tables you wish to float, wrap thetabular environment within a tableenvironment, like this:

\begin{table} \begin{tabular}{...} ... table data ... \end{tabular}\end{table}

You may feel that it is a bit long winded, but such distinctionsare necessary, because you may not want all tables to be treatedas a float.

Captions

It is always good practise to add a caption to any figure ortable. Fortunately, this is very simple in LaTeX. All you needto do is use the \caption{text}command within the float environment. Because of how LaTeXdeals sensibly with logical structure, it will automaticallykeep track of the numbering of figures, so you do not need toinclude 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 captioncommand after the actual contents of the float (but still withinthe environment). If you place it before, then the caption willappear above the float. Try out the following example todemonstrate this effect (See caption.tex and caption.pdf)

\begin{figure} \caption{A picture of a tucan.} \begin{center} \includegraphics{tucan.eps} \end{center}\end{figure}\begin{figure} \begin{center} \reflectbox{\includegraphics{tucan.eps}} \end{center} \caption{A picture of the same tucan looking the other way!}\end{figure}

Labels and Cross-referencing

This is a good opportunity to introduce labels. Theirpurpose within LaTeX is to act as a marker, which can then bereferenced to at any point within your document. It is verycommon to refer to each of your figures within the body of text.However, you don't want to be keeping track of the numbers, soyou can use a label instead, and let LaTeX replace it with thecorrect figure number.

To add a label, you must embed the following command:\label{marker}.

To then reference a label:\ref{marker}.

Alternatively, to get a page reference: \pageref{marker}.

\begin{figure} \begin{center} \reflectbox{\includegraphics{tucan.eps}} \end{center} \caption{A picture of the same tucan looking the other way!} \label{toucan}\end{figure}Figure~\ref{toucan} shows a photograph of a toucan.

When a label is declared within a float environment, the\ref will return the respective fig/table number(although, it must occur after the caption). Whendeclared outside, it will give the section number.

The tilde (~) in the above example is a special symbol withinLaTeX. It represents a non-breaking space. It is useful herebecause it keeps 'Figure' and what ever number it \ref refers toas a single unit, and won't get split over lines or pages.

Wrapping figures

Although not normally the case in academic writing, an authormay prefer that some floats do not break the flow of text, butinstead allow text to wrap around it. (Obviously, this effectonly looks decent when the figure in question is significantlynarrower than the text width.) The packagewrapfig was developed for this very task.

To use wrapfig, you must first add\usepackage{wrapfig} to the preamble. This then givesyou access to the\begin{wrapfigure}{alignment}{width} command. Alignment can beeither l for left, or r for right. The width is obviously the width of the figure. Anexample:

\begin{wrapfigure}{r}{40mm} \begin{center} \includegraphics{toucan.eps} \end{center} \caption{The Toucan}\end{wrapfigure}
Floats, figures and captions | Getting to Grips with LaTeX (1)

Note that the wrapfigure environment is in factnon-floating. This means that it may require some care in ensuringthat your inserted figure doesn't run over page breaks.

Subfigures

A useful extension is the subfigure package.Unfortunately, only version 2.0 is installed on the SoC network,but a newer, much improved (in terms of extra features) version2.1.4 is available, so be careful if you see examples of thispackage that use the latest version. Anyway, thisgives the author the ability to have figures within figures.Subfigures have their own caption, and an optional globalcaption is also allowed. An obvious application would be toillustrate the before and after effects of a given operation. Anexample will best illustrate the usage of this package.

\begin{figure}[htp] \begin{center} \subfigure[Original image]{\label{fig:edge-a}\includegraphics[scale=0.75]{toucan.eps}} \subfigure[After Laplace edge detection]{\label{fig:edge-b}\includegraphics[scale=0.75]{laplace_toucan.eps}} \\ \subfigure[After Sobel edge detection]{\label{fig:edge-c}\includegraphics[scale=0.75]{sobel_toucan.eps}} \end{center} \caption{Various edge detection algorithms} \label{fig:edge}\end{figure}

Floats, figures and captions | Getting to Grips with LaTeX (2)

You will notice that the figure environment is set up as usual.For each subfigure, you need to use the\subfigure[sub caption]{...figure ...} command. If you intend tocross-reference any of the subfigures, see where the label isinserted on the third example. \caption will providethe global caption. subfigure will arrange the figuresside-by-side providing they can fit, otherwise, it willautomatically shift figures below. This effect can be addedmanually, by putting the newline command (\\) before the figureyou wish to move to a newline.

NB For subtables, there is a different \subtable commandspecifically for such types, but it works in much the same way.

Custom Floats

If tables and figures are not adequate for your needs, then youalways have the option to create your own! A good example ofsuch an instance would be a document that gives lots of sourcecode examples of a programming language. One might thereforewish to create a program float. The float package isyour friend for this task. All commands to set up the newfloat must be placed in the preamble, and not within thedocument.

  1. Add \usepackage{float} to the preamble of yourdocument
  2. Declare your new float using: \newfloat{type}{placement}{ext}, where
    • type - the new name you wish to call your float, inthis instance, 'program'.
    • placement - t, b, p, or h (as previouslydescribed.
    • ext - the file name extension of anauxiliary file for the list of figures (or whatever).LaTeX writes the captions to this file.
  3. The default name that appears at the start of thecaption is the type. If you wish to alter this use,\floatname{type}{floatname}
  4. Changing float style can be issued with \floatstyle{style}(Works on all subsequent \newfloat commands, therefore, mustbe inserted before \newfloat to be effective.
    • plain - the normal style for LaTeX floats,i.e., nothing!
    • boxed - a box is drawn that surrounds thefloat, and the caption is printed below.
    • ruled - the caption appears above thefloat, with rules immediately above and below. Then thefloat contents, followed by a final horizontal rule.

An example document using a new program floattype:

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

Floats, figures and captions | Getting to Grips with LaTeX (3)

The verbatim environment is an environment that isalready part of LaTeX. Although not introduced so far, its nameis fairly intuitive! LaTeX will reproduce everything you giveit, including new lines, spaces, etc. It is ideal for sourcecode.

Boxed or ruled floats

The float package is perfect for styling your own custom floats, butwhat if you want similar styles for the existing table or figure floats? The floatpackage can also be used here.

The float package has a couple of commands specifically designed to restyle LaTeX'sexisting figure and table environments. This can be activated in your document premable. For example,to set a document to have boxed figures and boxed tables:

\usepackage{float}\floatstyle{boxed}\restylefloat{figure} % To re-style the 'figure' float\restylefloat{table} % To re-style the 'table' float.

Summary

That concludes all the fundamentals of floats. You willhopefully see how much easier it is to let LaTeX do all the hardwork and tweak the page layouts in order to get your figures inthe best place. As always, the fact that LaTeX takes care of allcaption and reference numbering is a great time saver. Don'tforget to have a look at the example .tex files below.

Files:caption.tex |caption.pdf |wrapping.tex |wrapping..pdf |subfigure.tex |subfigure.pdf |new_float.tex |new_float.pdf |toucan.eps |laplace_toucan.eps |sobel_toucan.eps
Useful resources:Subfigure v2.0Documentation |Float package documentation

Floats, figures and captions | Getting to Grips with LaTeX (2024)

References

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6214

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.