# Subfloat caption in wrong position

My subfloat command is standard and simple:

\documentclass[12pt]{article}
\usepackage{graphicx,subfig,caption}
\begin{document}

\begin{figure}[h!]
\centering
\subfloat[illegitimate]{\includegraphics[width=4.5cm]{QR.eps}}
\subfloat[legitimate]{\includegraphics[width=4.5cm]{anti02.eps}}
\caption{QR code}
\end{figure}

\end{document}


And I end up getting something like this, with the captions shifted away to the right:

p i c t u r e 1     p i c t u r e 2

(a) illegitimate     (b) legitimate


Why are the captions shifted so far away instead of staying under the pictures? The pictures are of equal and appropriate sizes and there're plenty of space to move around!

Edit: I checked out the log file, which seems to say something's wrong with the picture anti02. What could be wrong, please?

LaTeX Warning: Label ' multiply defined.

) ABD: EveryShipout initializing macros
(E:\CTEX\MiKTeX\tex\latex\amsfonts\umsa.fd)
(E:\CTEX\MiKTeX\tex\latex\amsfonts\umsb.fd) [1] [2]
(E:\CTEX\MiKTeX\tex\latex\base\omscmr.fd) [3] [4] [5] [6] <QR.eps>
Underfull \hbox (badness 10000) in paragraph at lines 291--291
[]

Overfull \hbox (233.31259pt too wide) in paragraph at lines 291--291
[]
<anti02.eps>
Underfull \hbox (badness 10000) in paragraph at lines 293--293
[][]

Overfull \hbox (233.31259pt too wide) in paragraph at lines 293--293
[]
[7] [8] [9] [10] [11] [12] ("C:\Users\eric\Desktop\number guessing——test.aux"
)


2nd Edit: I removed this command:

\setcaptionwidth{5in}


in preamble and it worked right. But why?

-
It works fine for me. What versions of subfig and caption are you using? –  Jake Mar 29 '12 at 14:12
@Jake: I don't know. I downloaded the whole thing as a package. How could this matter? –  Eric Mar 29 '12 at 14:16
Maybe you're using an old version of one of the packages that had a bug in it. If you add \listfiles to your preamble, the versions of the packages will be printed near the end of the .log file. I have subfig.sty 2005/06/28 ver: 1.3 and caption.sty 2011/11/10 v3.2e. Can you also try using \usepackage[demo]{graphicx} to load the graphicx package and see whether you still get the incorrect results? –  Jake Mar 29 '12 at 14:20
Does the problem occur with the exact code you just posted? –  Gonzalo Medina Mar 29 '12 at 14:20
@Jake: Yes they are exactly the same versions as yours. \usepackage[demo]{graphicx} still gets to same the wrong positions. –  Eric Mar 29 '12 at 14:38

If I modify your MWE so it starts with

\documentclass[12pt,draft]{article}


as I don't have your eps files I get the following

which shows that it works. If you get the same, then remove the draft again and try outside of the figure, just in normal text

\fbox{\includegraphics[width=4.5cm]{QR.eps}}


If the box does not surround the figure, it means that the bounding box in the file is incorrect and so the dimensions of the image are not as they appear. if that is the case you can fix the eps file or supply a corrected bounding box using the bb= 1 2 3 4 key in the optional argument to \includegraphics` using the correct values not 1 2 3 4!

-
Thank you for the answer. I was being unclear because that code is just part of a larger article. The real problem is this command \setcaptionwidth{5in} in the preamble. Please see the second edit. –  Eric Mar 29 '12 at 16:27
Ah well what do you expect to happen? you have requested two captions each 5in wide, but the page is not that wide so they can't fit. –  David Carlisle Mar 29 '12 at 16:36