Documenti di Didattica
Documenti di Professioni
Documenti di 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. Specifier
h t b p ! H
Permission Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot) Position at the top of the page. Position at the bottom of the page. Put on a special page for floats only. Override internal parameters Latex uses for determining "good" float positions. 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
wikibooks.org//Floats,_Figures_and_
1/9
10/25/2010 LaTeX/Floats, Figures and Captions - Wi caption from Figure to Example, use \renewcommand{\figurename}{Example} in the figure contents.
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.
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}
wikibooks.org//Floats,_Figures_and_
2/9
10/25/2010
\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}
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.
wikibooks.org//Floats,_Figures_and_
3/9
10/25/2010
\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}
\begin{wrapfigure}{r}{0.5\textwidth} \begin{center}
wikibooks.org//Floats,_Figures_and_
4/9
10/25/2010 LaTeX/Floats, Figures and Captions - Wi \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! 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 [1] (http://tug.ctan.org/texarchive/macros/latex/contrib/floatflt/) - for documentation see [2] (http://www.ctan.org/texarchive/macros/latex/contrib/floatflt/floatflt.pdf) .
Subfloats
A useful extension is the subfig package [3] (http://www.ctan.org/texarchive/macros/latex/contrib/subfig/subfig.pdf) , 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:
wikibooks.org//Floats,_Figures_and_
5/9
10/25/2010
\usepackage{subfig}
\begin{figure} \centering \subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth \subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth \subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth \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. 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.
subfig
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}
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
wikibooks.org//Floats,_Figures_and_
6/9
10/25/2010 LaTeX/Floats, Figures and Captions - Wi 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!"); } } \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 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:
wikibooks.org//Floats,_Figures_and_
7/9
10/25/2010 \documentclass{article}
% have hyperref package before float in order to get strange errors with .\theHfloatbox \usepackage[pdftex]{hyperref} \usepackage{float} %allows use of "@" before \begin{document} \makeatletter % this creates a custom and simpler ruled box style \newcommand\floatc@simplerule[2]{{\@fs@cfont #1 #2}\par} \newcommand\fs@simplerule{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc \def\@fs@pre{\hrule height.8pt depth0pt \kern4pt}% \def\@fs@post{\kern4pt\hrule height.8pt depth0pt \kern4pt \relax \def\@fs@mid{\kern8pt}% \let\@fs@iftopcapt\iftrue} % 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 the model fit $R^2$. It offers some protection against the sampling bias. \caption{Bootstrapping} \end{floatbox} \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 [4] (http://www.komascript.de/) have their own caption customizing features with e.g. and \setcapwidth. However these definitions have limited effect on newly created float environments with the wrapfig package.
\captionabove, \captionformat
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
wikibooks.org//Floats,_Figures_and_
8/9
10/25/2010 LaTeX/Floats, Figures and Captions - Wi 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 (http://www.andyroberts.net/misc/latex/index.html) with permission from the author.
References
1. http://www.ctan.org/tex-archive/macros/latex/contrib/float/ 2. http://www.tex.ac.uk/cgi-bin/texfaq2html?label=2colfltorder Retrieved from "http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions" This page was last modified on 17 October 2010, at 23:29. Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of Use for details. Privacy policy About Wikibooks Disclaimers
wikibooks.org//Floats,_Figures_and_
9/9