4

Not specific question:

What does \centering command specifically do?

MWE:

\documentclass[onecolumn]{IEEEtran}
\usepackage{graphicx,subcaption}

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

Specific question:

In the MWE previously shown there are two subfigures, which overflow the page margins. If the command \centering is "uncommented" suddenly the positioning becomes "smart" and won't allow overflow page margins. Is this a proper behavior of the command?, is it a bug?, is it a feature?

  • 1
    If you want to know what a comment does, you can do \show\centering which gives in this case \let \\\@centercr \rightskip \@flushglue \leftskip \@flushglue \parindent \z@ \parfillskip \z@skip – samcarter is at topanswers.xyz May 17 '16 at 23:07
  • 1
    @samcarter Is that likely to be enlightening, do you think? – cfr May 17 '16 at 23:16
  • 1
    It has something to do with IEEEtran.cls. Try with article.cls to see. – cfr May 17 '16 at 23:28
9

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:

enter image description here

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:

enter image description here

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:

enter image description here

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

  • 5
    note that the specified width is more than 1.21\textwidth as there is a word-space between the subfigures, which is why a linebreak is allowed there, compare with % after the end{subfigure} – David Carlisle May 18 '16 at 6:57
  • 1
    Splendid!, thank you so much for such a good explanation. – Hans May 18 '16 at 10:28
  • 1
    @DavidCarlisle You are right, I missed to add this to the answer but marked the space in the image with an red rule. I will add it to my answer, Thanks! – Mensch May 18 '16 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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