Well, it seems there is a bug (or is it a feature?) in your used class IEEEtran
. Or it results in the using of package subfigure
, which called package caption
. And that package caption
results in the following warning you should not oversee:
Package caption Info: Unknown document class (or package),
(caption) standard defaults will be used.
)
So let me ask you: Do you really need class IEEEtran
? If not, please another one, for example article
or scrartcl
(KOMA-Script).
Please have a look to the two lines from your given code:
\begin{subfigure}[b]{.7\textwidth}
\begin{subfigure}[b]{.51\textwidth}
with \textwidth
should be the width of the text you can print. I added package showframe
to visualize the typing area.
With a little bit calculating (0.7+0.51=1.21) you can see, that both subfigures together are wider (0.21\textwidth) than they should be to fit both the text width. In your class IEEEtran
you see an error in your case, because if the images can not be placed side by side inside the textwidth they should be placed one below the other (that you can see, if you try your code with class article
for example). Here your class/subfigure
combination fails ...
(BTW: have you tried to use 0.45\textwidth
for both images?)
Command \centering
(the same without the blank line of environment center
) wants to center the image(s) inside the given text width. With \centering
the images are centered and placed one below the other, because both together can not fit into text width.
MWE:
\documentclass[onecolumn]{IEEEtran} % article IEEEtran =================
\usepackage{graphicx,subcaption}
\usepackage{showframe} % to visualize typing area ======================
\begin{document}
\begin{figure}
%\centering
\begin{subfigure}[b]{.7\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{A subfigure}\label{fig:1a}
\end{subfigure}
\begin{subfigure}[b]{.51\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{Another subfigure}\label{fig:1c}
\end{subfigure}
\caption{A figure}\label{fig:1}
\end{figure}
\end{document}
Result of MWE with class IEEEtran
without centering:
You see both images are centered (distance a above and below), but the image 2 goes outside. This centering is only showed, because there is no other text on the page. Try it out with adding \blindtext
from package blindtext
or similar ...
Now please see the two lines code:
\end{subfigure}
\begin{subfigure}[b]{.51\textwidth}
That code results in a space between both images (and this space allows a linebreak, if needed). I have marked this space with an vertical red rule in the image. If you do not want the space (and the possible linebreak here) use the code (please try this by your own, add the %
into the MWE and compile to compare the resulting pdf for both classes with the other results):
\end{subfigure}% <====================================================
\begin{subfigure}[b]{.51\textwidth}
The %
behind the }
comments the rest of the line and therefore LaTeX does not see one (or more) spaces with the result, that you will have no space between both images. With the space the total width is a little bit longer than 1.21\textwidth
, that's the reason I suggested earlier 0.45\textwidth
for both images. Then the images fits with space the textwidth. (Thanks @DavidCarlisle for his comment!)
Now with IEEEtran
and \centering
:
Now both images are one below the other and both are centered (see red a's and b's in the image).
To compare with the first image now the result with class article
and commented \centering
:
Now the images are left justified (commented centering command!), because it was detected that both images can not placed inside the given \textwidth
. BTW you see the difference in both typing areas?
A last remark: class IEEEtran
is usually used to have two columns to print (see the related message in the log file). If you are not forced to use class IEEEtran
use better another one. Start with standard class article
or scrartcl
(KOMA-Script article).
\show\centering
which gives in this case\let \\\@centercr \rightskip \@flushglue \leftskip \@flushglue \parindent \z@ \parfillskip \z@skip
IEEEtran.cls
. Try witharticle.cls
to see.