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ębski Oct 30 at 19:25

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}

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:

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
  • 17
    @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
  • 2
    +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
  • 3
    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.

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

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.

  • Can you elaborate on why a regular caption would not do? – einpoklum Mar 17 '16 at 20:34
  • @einpoklum \caption by default only works inside figure, table (and some other similar environments). \captionof (requires caption or capt-of package) on the other hand lets you add a numbered caption outside these environments. – Torbjørn T. Apr 19 '16 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.

  • 5
    \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

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 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
  • 5
    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. – aeroNotAuto 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
  • 2
    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

Another approach is - [H] (don't forget to add the userpackage):

Places the float at precisely the location in the LaTeX code. Requires the float package, i.e., \usepackage{float}. This is somewhat equivalent to !ht.

LaTeX Floats on wikibooks

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.