[Tex/LaTex] Caption issue when placing figure side by side

captionsfloatsminipagepositioning

I placed 2 figures side by side using the minipage method from this post, and the figures look perfectly fine. However, the caption of the right figure stick out into the margin, why is that and how can I fix it?

enter image description here

My code is like this:

\begin{figure}[htbp]
  \centering
  \begin{minipage}{.5\textwidth}
    \centering
    \includegraphics[width=6.6cm]{figure1.png}
    \caption{Caption for figure 1.}
    \label{fig:fig1}
  \end{minipage}%
  \begin{minipage}{.5\textwidth}
    \centering
    \includegraphics[width=6.6cm]{figure2.png}
    \caption{Caption for figure 2, but longer.}
    \label{fig:fig2}
  \end{minipage}
\end{figure}

Best Answer

Here's the simplest method to achieve what you want:

\documentclass{article}
\usepackage[demo]{graphicx} % [demo] is just for the example
\begin{document}
\begin{figure}
\centering
\begin{minipage}[t]{.45\textwidth}
\centering
\includegraphics[width=.8\textwidth,height=4cm]{a}
\caption{A not so wide caption}
\end{minipage}\hfill
\begin{minipage}[t]{.45\textwidth}
\centering
\includegraphics[width=.8\textwidth,height=4cm]{a}
\caption{A caption that should be wider than the figure, but so wide that it should wrap}
\end{minipage}
\end{figure}
\end{document}

Here it's assumed that the two pictures have the same height, so that correct alignment is obtained for the captions.

enter image description here

Related Question