Is there any package or a method to force LaTeX to keep floating environments like table
and figure
closer to where they are declared?
-
You may also try to relax the rules for placement of floating environments, allowing the page to look worse; among others LaTeX requires that there is enough text on the page if the page is not taken entirely by floats.– Jakub NarębskiOct 30, 2018 at 19:25
-
This doesn't answer the question, but: TeX is assuming that you want the environment to float around. This means that you can't write "This table:", because "This table" may move somewhere else. Instead, you should write "as in Table ##". But if you absolutely need the table/figure to appear where you want it, then the answers on this page are for you.– TeepeemmJun 19, 2019 at 18:42
8 Answers
Easing the float placement by options:
You could use more positioning options. Not just [h]
. If you wish to place the figure near, allow more positioning options, for instance by [htbp]
(here, top, bottom, page). Use a !
symbol to remove further restrictions. So, in many cases this is sufficient:
\begin{figure}[!htbp]
Useful package regarding float placement:
float
introduces a placement optionH
enforcing the placement exactly at that point.placeins
provides the command\FloatBarrier
to limit the floating of figures or tables. You could place such a barrier before and after a listing.afterpage
allows a more clever\clearpage
, putting the effect off until the page is full:\afterpage{\clearpage}
Completely avoiding a floating environment:
Package caption
allows to add a caption outside a floating environment, meaning at any place you want. Use \captionof{figure}{the caption}
(for figures)
Example:
text
\begin{minipage}{\linewidth}
\begin{center}
\includegraphics[width=.6\linewidth]{example-image}
\captionof{figure}{An example image not including a Wombat}
\end{center}
\end{minipage}
even more text
The minipage
keeps graphic and caption together, the center
environment add a bit of white space around the figure.
Further reading:
Because it's an important and not easy subject, there's a lot of material to be found, for instance in FAQ collections. There's an extensive document dealing with graphics inclusion, manipulation and placement:
- Using Imported Graphics in LaTeX and pdfLaTeX by Keith Reckdahl.
The interesting part for your question may be Part IV: The Figure Environment beginning on page 55.
There is also Frank Mittelbach's excellent answer describing the floating mechanism and related options in great detail:
-
1Too bad you can't just disable floats from being inserted in a certain portion of the paper...– SamBDec 10, 2010 at 21:15
-
25@SamB: You could use
\FloatBarrier
before and after that portion of the paper or\clearpage
(or variants) before and after.– Stefan Kottwitz ♦Dec 10, 2010 at 21:19 -
2
-
1@StefanKottwitz: I've added a link to Frank Mittelbach's float explanation, I hope you don't mind. If you do, please feel free to roll back.– JakeApr 5, 2012 at 20:26
-
3
Controlling floats isn't difficult once you understand what LaTeX is trying to do with them. Requisite FAQ link: https://texfaq.org/FAQ-floats
Three things, broadly speaking, will prevent floats from staying close to their definition:
underspecified position parameters. I recommend always using [htbp] until the document is complete.
graphics too large. Do you really need a graph 2/3 the page size?
too many graphics near each other. Once graphics start building up, it just isn't possible to keep the last one near its source location.
Above all, forget about formatting your floats until the document is absolutely finished. You'll produce better results and write the document faster that way.
-
68
-
7
-
1if anyone thinks the faq answer (will provides a link) doesn't say all that's needed, then please feel free to mail the via link at the bottom of the "faq answer page"; i try to keep up with what's said here, but i'm not omnipotent, apparently. Oct 27, 2013 at 13:05
-
The FAQ link is broken, and I couldn't find live FAQs when searching tex.ac.uk. Has this resource moved? Jun 1, 2020 at 15:09
-
Check out the float
package. It adds ability to include [H]
forced here float placement. You can also select this as automatic default with \floatplacement{figure}{H}
.
\documentclass{article}
% Example taken from https://tex.stackexchange.com/questions/297564
\usepackage{float} % here for H placement parameter
\begin{document}
Text before the table.
\begin{table}[H] % placement parameter H
\centering % if you want to center the table
\caption{Table showing \ldots}
\label{table:ExampleTable}
% Code for table
\end{table}
Text after the table.
\end{document}
-
2022-11-4 (Happy Thanksgiving): I decided to add an MWE that shows how the
float
package is used. Nov 24, 2022 at 21:33 -
try \begin{figure}[!htb]
, in nearly all cases it helps. If not then use:
\usepackage[section]{placeins}
it prevents placing floats before the section.
Fairly often all that you need is to declare tables and figures earlier, before the first point of reference (for example, one or two paragraphs before the point of reference, or before the previous subsection heading).
Unfortunately, it tends to be a little bit of trial and error. Some kinds of floats need to be declared earlier than others; for example, floats with placement [b]
are sometimes trickier than floats with [t]
.
If it is truly critical that your image / table gets placed where you have typed it in the document, then it isn't a float, and why not remove the float environment entirely ie, insert it directly with \captionof{figure}[...]{...}
for the captions.
-
-
1@einpoklum
\caption
by default only works insidefigure
,table
(and some other similar environments).\captionof
(requirescaption
orcapt-of
package) on the other hand lets you add a numbered caption outside these environments. Apr 19, 2016 at 12:51
As @asia1281 mentioned, the [h]
directive helps. I did not know of [h!]
before (thanks!), but reading around seems to suggest that it isn't quite forced as there may still be circumstances where LaTeX refuses to put the float right there.
Another thing that may help is one of \clearpage
and \cleardoublepage
. These force all unprocessed floats to be typeset and then a new page or two (with \cleardoublepage
and some classes). If you wait until your document is written and you are worrying only about apperance, you can put one of these commands at the end of the text on the very bottom of a page to force the outstanding floats to appear right thereafter. This is really best saved for final stages and needs to be redone (or at least checked) whenever the text is edited.
-
6
\clearpage
is useful in combination with theafterpage
package to avoid empty space at the end of a page.– Stefan Kottwitz ♦Aug 24, 2010 at 23:27 -
@Stefan Thanks! I'd not know of afterpage. That certainly makes the use of clearpage in these circumstances much easier.– vandenAug 25, 2010 at 2:51
An easy way to specify for LaTeX to place something is by adding an optional argument for position:
\begin{figure}[h]
where the [h]
means here, or
\begin{figure}[h!]
where the [h!]
is for forced here. Do these work?
-
2
h
means that you allow 'here', but by not specifyingt
,b
orp
you don't allow placement at 'top', 'bottom' or a float 'page'. Restricting toh
makes placement more difficult.– Stefan Kottwitz ♦Aug 24, 2010 at 23:25 -
6
-
@TH. - so does LaTeX always ignore
h
? that's what i deduced after reading this: robjhyndman.com/researchtips/latex-floats. and would you say adding!
is typically a bad idea? one might argue that LaTeX constraints were designed with optimum readability in mind. Aug 25, 2010 at 15:32 -
No, it doesn't always ignore h. What I meant was if you use
\begin{figure}[h]
, LaTeX will spit out a warning that says it's changed theh
toht
. I don't have an opinion on whether adding!
is a bad idea or not.– TH.Aug 25, 2010 at 20:29 -
2Adding ! will direct LaTeX to ignore some of its rules with respect to number of floats on the page or size restrictions, so it increases the chances that a float fitts into "h" or "t", see tex.stackexchange.com/questions/39017/… for a detailed discussion of all the rules Aug 6, 2013 at 19:37