Thursday, September 24, 2009

Floating figures

One of the most frequently asked questions about LaTeX seems to be: How does one put a figures right here? The answer is, of course, very simple: Don't put material you don't want to float in a floating environment. This is the LaTeX equivalent of, "Doctor, it hurts when I do this." Shortly after giving this answer, the questioner usually responds that she had no idea that \includegraphics—you are using \includegraphics for including your graphics, aren't you?—could be used outside of the figure environment. There is nothing special about \includegraphics from TeX's point of view: It simply creates a box, and everything is a box (unless it's glue, or a penalty, or a whatsit, or a...). Want to stick a graphic right here? No problem, \includegraphics{foo} will insert foo.eps if you're running latex or foo.{jpg,png,pdf} if you're running pdflatex. Well that's simple, but something's missing. In fact, two things are missing. First, one cannot simply use \caption to produce a caption for this graphic. Second, one cannot reference this graphic using \label and \ref since this requires, among other things, a \caption. Here's the metric I use when deciding whether to make a graphic floating or not. If I want to give it a caption or reference it in text, then I make it float and give it a caption. If I don't reference it in text and it has no caption, then I might not let it float since it might seem out of place if floating. Of course, there are very few reasons why one might want a graphic in a scholarly publication that isn't referenced. This is a fairly simple rule to follow and solves the problem. At this point, there are two things I feel I should mention. The first is that there is a package that will allow one to place a figure environment right here; however, using that violates my rule, so I'll say nothing more about it. The second thing to mention is that LaTeX2e has a bug in its figure placement algorithm when using the twocolumn documentclass option (at least for the standard classes) and the figure* environment. This bug can cause two column figures to be placed out of order with respect to single column figures. The fix for this is very simple: place \usepackage{fixltx2e} into the preamble of your document (between \documentclass and \begin{document}). Actually, the package does more than just fix that bug and should probably be used in every LaTeX document.

No comments:

Post a Comment