How to influence the position of float environments like figure and table in LaTeX?
This is a general question and should collect useful answers for all users. I hope we can use this as a reference
To answer this question one has to first understand the basic rules that govern LaTeX's standard placement of floats. Once these are understood, adjustments can be made, for example, by modifying float parameters, or by adding certain packages that modify or extend the basic functionality.
LaTeX floats terminology
Each float in LaTeX belongs to a class. By default, LaTeX knows about two classes, viz.,
One important aspect of the float placement algorithm is that LaTeX never violates the order of placement within a class of floats. E.g., if you have figure1, table1, figure2 in a document, then figure1 will always be placed before figure2. However, table1 (belonging to a different float class) will be placed independently and hence can appear before, after, or between the figures.
LaTeX knows about 2 float areas within a column where it can place floats: the top area and the bottom area of the column. In two-column layout, it also knows about a top area spanning the two columns. There is no bottom area for page-wide floats in two-column mode
In addition, LaTeX can make float columns and float pages, i.e, columns and pages which contain only floats.
Finally LaTeX can place floats inline into the text (if so directed).
Float placement specifiers
To direct a float to be placed into one of these areas, a float placement specifier has to be provided as an optional argument to the float. If no such optional argument is given then a default placement specifier is used (which depends on the float class as mentioned above). A float placement specifier can consist of the following characters in any order:
The order in which these characters are stated does not influence how the algorithm tries to place the float! This is one of the common misunderstandings, for instance when people think that
However, if a letter is not present then the corresponding area will not be tried at all.
Float algorithm parameters
There are about 20 parameters that influence the placement. Basically they define
Float reference point
The point where the float is placed in the source document affects the placement of the float in the output because it determines when LaTeX sees the float for the first time. It's important to understand that if a float is placed in the middle of a paragraph, this reference point is the next line break, or page break, in the paragraph that follows the actual placement in the source.
Basic behavioral rules of LaTeX's float mechanism
With this knowledge, we are now ready to delve into the algorithm's behavior.
First we have to understand that all of LaTeX's typesetting algorithms are designed to avoid any sort of backtracking. This means that LaTeX reads through the document source, formats what it finds and (more or less) immediately typesets it. The reasons for this design choice were to limit complexity (which is still quite high) and also to maintain reasonable speed (remember that this is from the early eighties).
For floats, this means that the algorithm is greedy, i.e., the moment it encounters a float it will immediately try to place it and, if it succeeds, it will never change its decision. This means that it may choose a solution that could be deemed to be inferior in the light of data received later on.
For example, if a figure is allowed to go to the top or bottom area, LaTeX may decide to place this figure in the top area. If this figure is followed by two tables which are only allowed to go to the top, these tables may not fit anymore. A solution that could have worked in this case (but wasn't tried) would have been to place the figure in the bottom are and the two tables in the top area.
The basic sequence
So here is the basic sequence the algorithm runs through:
Detailed placement rules when encountering a float
Whenever LaTeX encounters a float environment in the source, it will first look at the holding queue to check if there is already a float of the same class in the queue. If that happens to be the case, no placement is allowed and the float immediately goes into the holding queue.
If not, LateX looks at the float placement specifier for this float, either the explicit one in the optional argument or the default one from the float class.
This ends the processing when encountering a float in the document.
Emptying the holding queue at the page boundary
After a page has been finished, LaTeX looks at the holding queue and attempts to empty it out as best as possible. For this it will first try to build float pages.
Any floats participating in a float page (or column) must have a
If the float can go there, it will marked for inclusion on the float page, but the processor may still abort the attempt if the float page will not get filled "enough" (depending on the parameter settings for float pages). Only at the very end of the document, of when a
Creation of float pages continues until the algorithm has no further floats to place or if it fails to produce a float page. In the latter case, all floats that have not been placed so far are then considered for inclusion in the top and bottom areas of the next page (or column).
The process there is the same as the one described above, except that
Any float that couldn't been placed then is put back to the holding queue, so that when LaTeX is ready to look at further textual input from the document the holding queue may already contain floats. A consequence of this is that a float encountered in the document may immediately get deferred just because an earlier float of the same float class is already on hold.
Details on the parameters that restrict/influence the placement
There are four counters that control how many floats can go into areas:
The size of the areas are controlled through parameters (changed with
The space that separates floats within an area, as well as between float areas and text areas, is defined through the following parameters (all of which are rubber lengths, i.e., can contain some stretch or shrink components). Their defaults depend on the document font size and change when class options like
For inline floats (that have been placed "here") the separation to the surrounding text is controlled by
In case of float pages or float columns (i.e., a page or a column of a page containing only floats) parameters like
Consequences of the algorithm
A float may appear in the the document earlier than its placement in the source
The placement of the float environment in the source determines the earliest point where it can appear in the final document. It may move visually backward to some degree as it may be placed in the top area on the current page. It can, however, not end up on an earlier page as the surrounding text due to the fact that LaTeX does no backtracking an the earlier pages have already been typeset.
Thus normally a float is placed in the source near its first call-out (i.e., text like "see figure 5") because this will ensure that the float appears either on the same page as this text or on a later page. However, in some situations you may want to place a float on the preceding page (if that page is still visible from the call-out). This is only possible by moving the float in the source.
Double-column floats are always deferred first
When LaTeX encounters a page-wide float environment (indicated by a
Thus, in order to place such a float onto the current page, one has to manually move it to an earlier place in the source -- before the start of the current page. If this is done, obviously any further change in the document could make this adjustment obsolete; hence, such adjustments are best done (if at all) only at the very last stage of document production --- when all material has been written and the focus is on fine-tuning the visual appearance.
There is no bottom float area for double-column floats
This isn't so much a consequence of the algorithm but rather a fact about it. For double-column floats the only possible placements are the top area or a float page. Thus if somebody adds an
All float parameters (normally) restrict the placement possibilities
This may be obvious but it is worth repeating: any float parameter defines a restriction on LaTeX's ability to place the floats. There is always a way to set a parameter in such a way that it does not affect the placement at all. Unfortunately, in doing so one invites rather poorly looking placements.
By default LaTeX has settings that are fairly liberal. For example, for a float page to be accepted the float(s) must occupy at least half of the available page. Expressed differently, this means that such a page is allowed to be half empty (which is certainly not the best possibe placement in most cases).
What often happens is that users try to improve such settings and then get surprised when suddenly all floats pile up at the end of the document. To stay with this example: if one changes the parameter
For this reason it is best not to meddle with the parameters while writing a document or at least not to do so in a way that makes it more difficult for the algorithm to place a float close to its call-out. For proof-reading it is far more important to have a figure next to the place it is referenced then to avoid half-empty pages. Possibilities for fine-tuning an otherwise finished document are discussed below.
Another conclusion to draw here is that there are dependencies between some of the float parameters; it is important to take these dependencies into account when changing their values.
"Here" really just means "here if it fits"
... and often it doesn't fit. This is somewhat surprising for many people, but the way the algorithm has been designed the
Float specifiers do not define an order of preference
As mentioned above, the algorithm tries to place floats into available float areas in a well-defined order that is hard-wired into the algorithm: "here", "top", "bottom" and -- on page boundaries -- first "page" and only if that is no longer possible "top" followed by "bottom" for the next page.
Relation of floats and footnotes
This is not exactly a consequence of the algorithm but one of its implementation: Whenever LaTeX tries to decide on a placement for a float (or a marginpar!) it has to trigger the output routine to do this. And as part of this process any footnotes on the page are removed from their current place in the galley and are collected together in the
However, if for some reason a page is finally taken at an earlier point then the footnotes will show up on the wrong page or column. This is a fairly unlikely scenario but if it happens check if there is a float near the chosen pagebreak and either move the float or guide the algorithm by using explicit page breaks. and example can be found in this question.
In fact that particular case is worth highlighting: Do not place a float directly after a heading, unless it is a heading that always starts a page. The reason is that headings normally form very large objects (as they prevent page breaks directly after it). However placing a float in the middle of this means that the OR gets triggered before LaTeX makes its decision where to break and any footnotes get moved into the wrong place
Documentation of the algorithm
As requested here is some information on existing documentation. The algorithm and its implementation is documented in the file
This documentation is an interesting historical artifact. Parts of it show semi-formatted pseudo code which dates back to LaTeX2.09; in other words it is from the original documentation by Leslie Lamport. The actual code is documented using doc style and in parts is more or less properly documented (from scratch) and dates back to 1994 or thereabout when Chris Rowley and myself adjusted and extended the original algorithm for LaTeX2e. It also fairly openly documents the various issues with the algorithm and/or its implementation --- in many cases we didn't dare to alter it because of the many dependencies and, of course, because of the danger to screw up too many existing documents that implicitly rely on the current behavior for good or worse. Near the end you'll find a list of comments compiled back then on the algorithm, but there are also comments, questions, and tasks (? :-) sprinkled throughout the documentation of the code.
One interesting aspect of this file (that I forgot all about) is that it contains all code necessary to trace the behavior of the algorithm in real life. Unfortunately, I never made this officially available or so it seems. It would probably need a good amount of cleanup and better formatting of the tracing output it produces to be usable for the general public. But even in its current form it does give some interesting insight in the behavior of the algorithm and how certain decisions come about.
So if somebody wants to play with it or wants to trace some strange float placements, then all that is necessary to do (fingers crossed) is to make a short file
Run through LaTeX this will produce the style file
How to address specific issues
to be added later (and/or perhaps reference to other questions)
Info For the moment I guess I'm done. The last section will need material but during the next days I'm certainly not able to provide it -- so if anybody feels like fixing my English ... please go ahead. It might be also a good approach to collect here references to other questions addressing specific topics. Info End
|show 4 more comments|
There are several possibilities for controlling float placement. The question I see most here is along the lines of "How do I insert an image/table at the point I list it in the source document?".
First I think it is important to note that you don't need to use floats. An
If a float environment is required, but the "amount of float" has to be limited to keep the content relatively close to the point where it was defined in the source, then the
Finally, if the content should be placed in the exact place it was defined in the source document, then the
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.