# How to use figure inside a minipage?

I'm trying to include an image, and keep it centered, and prevent from wrapping from the next page. The following code works for that:

\begin{minipage}{\textwidth}
\begin{center}
Caption for image

\includegraphics[scale=0.5]{myimage.png}
\end{center}
\end{minipage}


I'd like this image to be in a figure, with a caption. The following doesn't compile:

\begin{minipage}{\textwidth}
\begin{figure}
\includegraphics[scale=0.5]{myimage.png}
\caption{Caption for image}
\label{fig:sample_figure}
\end{figure}
\end{minipage}


I get the errors:

! LaTeX Error: Not in outer par mode.
! Undefined control sequence.


How can I fix this?

-
figure is a float, it must be outside any other environment (not counting document of course). Also check Keeping tables/figures close to where they are mentioned –  Martin Scharrer May 11 '12 at 6:12

Figure is a floating environment and minipage is, unfortunately not. Therefore, if you put floating object inside a non-floating minipage, the error will be thrown on you. But the other way round is possible-- you can put a minipage inside a figure environment:

\begin{figure}
\centering
\begin{minipage}[c]{\textwidth}
\centering
\includegraphics[width=3.0in]{example-image-a}
\caption{Caption for image}
\label{fig:sample_figure}
\end{minipage}
\end{figure}


Another method is to avoid figure totally. This can be done with help of caption package (with its captionof facility, so that you can have a caption for the figure):

.....in preamble
\usepackage{caption}

......in document body

\begin{minipage}[c]{\textwidth}
\centering
\includegraphics[width=3.0in]{example-image-a}
\captionof{figure}{Caption for image}
\label{fig:sample_figure}
\end{minipage}


The total mwe will be:

\documentclass{article}
\usepackage{mwe} % new package from Martin scharrer
\usepackage{caption}
\begin{document}

\begin{figure}
\centering
\begin{minipage}[c]{\textwidth}
\centering
\includegraphics[width=3.0in]{example-image-a}
\caption{Caption for image}
\label{fig:sample_figure}
\end{minipage}
\end{figure}

\noindent
\begin{minipage}[c]{\textwidth}
\centering
\includegraphics[width=3.0in]{example-image-a}
\captionof{figure}{Caption for image}
\label{fig:sample_figure}
\end{minipage}

\end{document}


The result will be:

-
My first use of mwe package @Martinscharrer –  Harish Kumar May 11 '12 at 5:16
Thanks for using it! Note that you can use the images without loading the package. Using graphicx here directly might actually be better. –  Martin Scharrer May 11 '12 at 6:11
@MartinScharrer: Well, I am new to mwe. Slowly I hope to get accustomed to it. Thanks for a great package and also for the short cuts for CTAN packages and texdoc.net. They are very useful. –  Harish Kumar May 11 '12 at 6:17
This is probably what the OP actually wants... bit it is in fact possible to do what the OP asks for. See my answer. –  cheshirekow Jan 25 '13 at 17:48

You can put a figure inside a minipage if you use the "float" package. The following will put two figures side by side. If you caption them, the captions will be side by side.

\usepackage{float}

...

\begin{minipage}{\linewidth}
\centering
\begin{minipage}{0.45\linewidth}
\begin{figure}[H]
\includegraphics[width=\linewidth]{figures/dummy}
\caption{This is the first figure}
\end{figure}
\end{minipage}
\hspace{0.05\linewidth}
\begin{minipage}{0.45\linewidth}
\begin{figure}[H]
\includegraphics[width=\linewidth]{figures/dummy}
\caption{This is the second figure}
\end{figure}
\end{minipage}
\end{minipage}


-
This is great. It just needs to also align the captions when the images don't share their height –  Thomas Ahle Sep 19 '13 at 18:37