2

I am going pretty nuts here. The little LaTeX knowledge I have has reached its limits, and I can't find any clues anywhere on the net. So some input would be greatly appreciated!

The Problem
When using subfloats (with the subfig package) to include graphics at the full text/column width, the graphics seem to be shifted to the right slightly, extending over and out of the text area.
I first noticed this when preparing a two-column manuscript where I needed two figures under each other with subcaptions.

Here is an example:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}

\begin{figure}
    \centering
    \includegraphics[width=\textwidth,height=0.1\textwidth]{dummy.pdf}
    \caption{Caption 1}
\end{figure}

\begin{figure}
    \centering
    \subfloat[Subcaption 2a.]{
        \includegraphics[width=\textwidth,height=0.1\textwidth]{dummy.pdf}
    }\\
    \subfloat[Subcaption 2b.]{
        \includegraphics[width=\textwidth,height=0.1\textwidth]{dummy.pdf}
    }
    \caption{Caption 2}
\end{figure}

\blindtext

\end{document}

(Using width=\columnwidth or width=\linewidth results in the same behavior.)

Note that this does not happen when using smaller widths (e.g. width=0.9\textwidth). Then, everything works as it should.

The Question
How can I fix this in a sensible manner?

I could of course reduce the width of the subfloat graphics. But I am a sucker for consistency and my graphics basically show the same thing with small changes, so that it is somewhat apparent when the sizing changes all of the sudden. I'd also want to avoid rescaling all graphics, and would prefer if they span the entire text/column width.

1 Answers1

1

You are introducing some spurious white-space after the opening brace before including the image.

It should be:

% arara: pdflatex

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}
\usepackage{blindtext}

\begin{document}    
\begin{figure}
    \centering
    \includegraphics[width=\textwidth,height=0.1\textwidth]{dummy.pdf}
    \caption{Caption \thefigure}
\end{figure}    
\begin{figure}
    \subfloat[Subcaption \thefigure a.]{%
        \includegraphics[width=\linewidth,height=0.1\textwidth]{dummy.pdf}
        }

    \subfloat[Subcaption \thefigure b.]{%
        \includegraphics[width=\linewidth,height=0.1\textwidth]{dummy.pdf}
        }
    \caption{Caption \thefigure}
\end{figure}    
\blindtext  
\end{document}

enter image description here

Please note that setting a width and a height for an image is not a very good idea as you might distort your figure. Just set the width and crop the image to the desired height. I left it in above code, as it is quite practical in case of using the [demo] option of graphicx.

If you want to set a height as well, take a look on the input on this post!

LaRiFaRi
  • 43,807
  • Awesome, I was not aware that I was introducing white spaces with the way I typeset my documents. Thanks a lot! (Looks like I'll be spending my afternoon correcting this in all my LaTeX docs...) – Severin Sadjina May 03 '16 at 10:33
  • @SeverinSadjina To be honest, I just put a % after every opening brace which is followed by a line break. Not always needed, but never (until now) disturbing. – LaRiFaRi May 03 '16 at 10:36