Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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.
Figures
To create a figure that floats, use the figure environment.
\begin{figure}[placement specifier]
... figure contents ...
\end{figure}
The previous section mentioned 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.
LaTeX/Floats, Figures and Captions 2
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)
! Override internal parameters Latex uses for determining "good" float positions.
H [1]
Places the float at precisely the location in the LaTeX code. Requires the float package, 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} in the figure contents.
\floatstyle{boxed}
\restylefloat{figure}
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.
LaTeX/Floats, Figures and Captions 3
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}
\end{document}
\documentclass[12pt]{article}
\usepackage{graphicx}
\begin{document}
\listoffigures
\section{Introduction}
\begin{figure}[hb]
\centering
\includegraphics[width=4in]{gecko}
\caption[Close up of \textit{Hemidactylus} sp.]%
{Close up of \textit{Hemidactylus} sp., which is
part the genus of the gecko family. It is the
second most speciose genus in the family.}
\end{figure}
\end{document}
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}
LaTeX/Floats, Figures and Captions 5
\usepackage{wrapfig}
\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, of
course, 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"
LaTeX/Floats, Figures and Captions 6
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!
The wrapfig package can also be used with user-defined floats with float package. See below in the section on
custom floats.
\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, it is best not to add any
space at all: let LaTeX do the formatting work!
Alternatively you might use the picins package instead of the wrapfigure 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 wrapfig: the package floatflt [2] - for documentation see [3].
Subfloats
A useful extension is the subfig package [4], 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:
\usepackage{subfig}
\begin{figure}
\centering
\subfloat[A
LaTeX/Floats, Figures and Captions 7
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:
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}
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 is [tbp]. However, h still does not work.
To prevent the figures from being placed out-of-order with respect to their "non-starred" counterparts, the package
fixltx2e [5] should be used (e.g. \usepackage{fixltx2e}).
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.
1. Add \usepackage{float} to the preamble of your document
2. 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'.
3. The default name that appears at the start of the caption 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, must be inserted before \newfloat to 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.
Float styles can also be customized as the second example below illustrates.
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!");
LaTeX/Floats, Figures and Captions 9
}
}
\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.
Newly created floats with newfloat can also be used in combination with the wrapfig 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:
\documentclass{article}
\usepackage{float}
% this code block defines the new and custom floatbox float environment
\floatstyle{simplerule}
\newfloat{floatbox}{thp}{lob}[section]
\floatname{floatbox}{Text Box}
\begin{document}
\begin{floatbox}{r}{}
\textit{Bootstrapping} is a resampling technique used
for robustly estimating statistical quantities, such as
LaTeX/Floats, Figures and Captions 10
\end{document}
Caption Styles
To change the appearance of captions, use the caption package. For example, to make all caption labels small and
bold:
\usepackage[small,bf]{caption}
The KOMA script packages [6] have their own caption customizing features with e.g. \captionabove, \captionformat
and \setcapwidth. However these definitions have limited effect on newly created float environments with the
wrapfig package.
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 [9] with permission from the author.
References
[1] http:/ / www. ctan. org/ tex-archive/ macros/ latex/ contrib/ float/
[2] http:/ / tug. ctan. org/ tex-archive/ macros/ latex/ contrib/ floatflt/
[3] http:/ / www. ctan. org/ tex-archive/ macros/ latex/ contrib/ floatflt/ floatflt. pdf
[4] http:/ / www. ctan. org/ tex-archive/ macros/ latex/ contrib/ subfig/ subfig. pdf
[5] http:/ / www. tex. ac. uk/ cgi-bin/ texfaq2html?label=2colfltorder
[6] http:/ / www. komascript. de/
[7] http:/ / www. math. uni-leipzig. de/ ~matveyev/ lpic/
[8] http:/ / www. ctan. org/ tex-archive/ help/ Catalogue/ entries/ pinlabel. html
[9] http:/ / www. andy-roberts. net/ misc/ latex/ index. html
Article Sources and Contributors 11
License
Creative Commons Attribution-Share Alike 3.0 Unported
http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/