1

I have a particular problem aligning the caption and the figure. I attach for you the image of what latex is producing, plus the image file if somone needs to replicate the code. As you see the image with the caption below it are not completely aligned. The code for the MWE is:

\documentclass[11pt,a4paper]{article}
\usepackage[pdftex]{graphicx}
\usepackage{float}
\usepackage{booktabs,caption,fixltx2e}

\begin{document}
\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{image}
\vspace{-0.5cm}
\footnotesize {\caption{Comparison with US policies}}
\label{fig:Comparison with US policies}
\end{center}
\end{figure}
\end{document}

enter image description here

  • Could you show the output when you use \fbox{\includegraphicx[..]{...}} as well? It'll indicate whether the problem lies with suspected whitespace around the image, or elsewhere. – Werner Sep 13 '15 at 16:45
  • Sorry @Werner, I did not see your comment before posting my answer. – Philipp Imhof Sep 13 '15 at 17:00
  • It's generally best to use the pdfcrop program (or an alternative with the same functionality) first to get rid of redundant whitespace. It'll prevent this type of problems. – JPi Sep 13 '15 at 17:01
2

Try the following code:

\documentclass[11pt,a4paper]{article}
\usepackage[pdftex]{graphicx}
\usepackage{float}
\usepackage{booktabs,caption,fixltx2e}

\begin{document}
\begin{figure}[h!]
\centering
\fbox{\includegraphics[width=0.8\textwidth]{example-image-a}}
%\vspace{-0.5cm}
\footnotesize {\caption{Comparison with US policies}}
\label{fig:Comparison with US policies}
\end{figure}
\end{document}

The \fbox will make white space around the image visible.

I am sure there is white space, because you use \vspace{-.5cm} to compensate space below the picture, so there most probably is also some space on the sides.

With the example image, everything is fine.

Update

Now that the whitespace problem is confirmed, you have several options:

  • change the picture, e.g. crop it
  • change the picture's bounding box, this can be done by using appropriate parameters with \includegraphics-- see the documentation of the graphicx package
  • add whitespace at the end of the caption to manually correct the alignment; however, this is more quick and dirty than a real solution

What bothers you is not an error with LaTeX. It simply cannot know that some parts of your image are "undesired" and should not be considered when centering the caption. You must tell LaTeX what part of the image is important.

PS: Do not use \begin{center}...\end{center} in a figure, as this adds vertical spacing. Use \centering

PPS: Are you sure you need a floating figure? Most often when people write h!, they want the figure to be shown exactly where they put it. If it is not supposed to float, don't use a float.

  • unfortunatelly the solution you suggest what it does in my case is to align the caption with the box that appears surrounding the image. This, it first looks ungly, and second did not really fix the problem for me. I dropped the \vspace and did exactly what you suggest but again I have the problem. – user17880 Sep 13 '15 at 17:20
  • It was not meant to solve the alignment problem, but to show you why the alignment was bad. It's because LaTeX cannot know that some parts of the image are undesired. I'll add an update to my answer. – Philipp Imhof Sep 13 '15 at 17:22
  • I did the "crop" idea. I now understood the issue. Thanks – user17880 Sep 13 '15 at 17:47
1

The problem is that your original picture contains whitespace in the right:

enter image description here

This output was produced with image.png:

enter image description here

and your original code

\documentclass[11pt,a4paper]{article}
\usepackage[pdftex]{graphicx}
%\usepackage{float}
%\usepackage{booktabs,caption,fixltx2e}

\begin{document}
\begin{figure}[h!]
  \centering
  \includegraphics[width=0.8\textwidth]{image}
  \vspace{-0.5cm}
  \caption{Comparison with US policies}
  \label{fig:Comparison with US policies}
\end{figure}
\end{document}

leaving out the last packages loaded because you actually don't use them in the example. However, including them does not change the output. You can solve this problem for instance with pdfcrop, a neat Perl script by Heiko Oberdiek.

Aside from that the \footnotesize{...} construction does not make any sense. \footnotesize does not take arguments. It is a switch, thus would be used like {\footnotesize...} to reduce its effect to the given group. But in this case this would not work as \caption does not prduce any output. If you want a \footnotesized caption see Font size of Figure Caption Header.

  • Unfortunatelly in my case didn't work for some reason. I dropped the packadges you suggested but the problem was not fixed. – user17880 Sep 13 '15 at 17:16
  • 2
    As Ruben wrote, these packages do not actually have any influence on this specific output. He just dropped them, because they were not needed for this particular example, which is supposed to be a minimal working example. The problem cannot be fixed without altering the image or giving appropriate bounding box information to \includegraphics, as there actually is no problem: LaTeX correctly aligns the caption with the image, not knowing that the whitespace makes things look odd. – Philipp Imhof Sep 13 '15 at 17:30

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.