# Keeping tables/figures close to where they are mentioned

Is there any package or a method to force LaTeX to keep floating environments like table and figure closer to where they are declared?

-
At least for part 3 I recommend to open a separate question instead if mixing listings spacing with float placement issues. Edit: listings part removed –  Stefan Kottwitz Aug 24 '10 at 22:52

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 option H 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}

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:

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:

-
Too bad you can't just disable floats from being inserted in a certain portion of the paper... –  SamB Dec 10 '10 at 21:15
@SamB: You could use \FloatBarrier before and after that portion of the paper or \clearpage (or variants) before and after. –  Stefan Kottwitz Dec 10 '10 at 21:19
+1: what do you think about mentioning the flafter package? –  cmhughes Feb 28 '12 at 2:12
@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. –  Jake Apr 5 '12 at 20:26
H option in float package is very useful to me! –  updogliu Dec 7 '12 at 1:06

Controlling floats isn't difficult once you understand what LaTeX is trying to do with them. Requisite FAQ link: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=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.

-
+1 for "don't worry until you're nearly done". –  Reid Aug 8 '11 at 17:45
and another +1 for [htbp]. Makes hell of a difference from [ht]. –  mreq Mar 24 '13 at 21:31
if 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. –  wasteofspace Oct 27 '13 at 13:05

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

-
Thanks! That did it for me :) –  Didii Jul 27 '12 at 7:29

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].

-

try \begin{figure}[!htb], in nearly all cases it helps. If not then use:

\usepackage[section]{placeins}


it prevents placing floats before the section.

-
It worked (\usepacage), thanks @Herbert –  Mohsen Afshin Feb 28 '13 at 10:46

As @asia1281 mentioned, the [h] directive helps. I'd 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 you're 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.

-
\clearpage is useful in combination with the afterpage package to avoid empty space at the end of a page. –  Stefan Kottwitz Aug 24 '10 at 23:27
@Stefan Thanks! I'd not know of afterpage. That certainly makes the use of clearpage in these circumstances much easier. –  vanden Aug 25 '10 at 2:51

Another method (that I may have missed in the above answers) if you absolutely need the figure to end up exactly where you tell it to go is by using the float' package and then

\begin{figure}[H]

where the H' dictates the figure/table will be placed exactly where put in the tex document. It worked great for me while I was new with TeX and did not trust it to lay my document out for me :)

-
This has already been covered in one of the other answers. –  Werner Sep 13 '13 at 6:18

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.

-

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?

-
h means that you allow 'here', but by not specifying t, b or p you don't allow placement at 'top', 'bottom' or a float 'page'. Restricting to h makes placement more difficult. –  Stefan Kottwitz Aug 24 '10 at 23:25
Also LaTeX ignores you and changes it to [ht]. –  TH. Aug 25 '10 at 1:26
@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. –  asia1281 Aug 25 '10 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 the h to ht. I don't have an opinion on whether adding ! is a bad idea or not. –  TH. Aug 25 '10 at 20:29
Adding ! 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 –  Frank Mittelbach Aug 6 '13 at 19:37