# 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 floating environment and minipage is, unfortunately, not. Therefore, if you put a floating object inside a non-floating minipage, you will get an error. But the other way around 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 using figure entirely. This can be done with help of the 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 – user11232 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. – user11232 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
• @ThomasAhle, better use subfigure package for that purpose. – Ricardo Cruz Jan 26 '15 at 16:53
• hmm .. this didn't work for me. – demented hedgehog Dec 18 '17 at 0:22
• @dementedhedgehog Did you notice the subtle use of the parameter [H] following the environment declaration of the figures? – ZenLulz May 31 '18 at 23:32
• I may have missed it... it was a while ago I can't go back and test. – demented hedgehog Jun 1 '18 at 1:00

You could try this for 2 figures side by side:

\begin{figure}[htb]
\begin{minipage}[t]{.45\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\subcaption{Image 1.}\label{fig:1}
\end{minipage}
\hfill
\begin{minipage}[t]{.45\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\subcaption{Image 2.}\label{fig:2}
\end{minipage}
\label{fig:1-2}
\caption{Title.}
\end{figure}


Or this, for 3 figures side by side:

\begin{figure}[htb]
\begin{minipage}[t]{.3\textwidth}
\centering
\includegraphics[width=\textwidth]{images/1.png}
\subcaption{Image 1.}\label{fig:1}
\end{minipage}
\hfill
\begin{minipage}[t]{.3\textwidth}
\centering
\includegraphics[width=\textwidth]{images/2.png}
\subcaption{Image 2.}\label{fig:2}
\end{minipage}
\hfill
\begin{minipage}[t]{.3\textwidth}
\centering
\includegraphics[width=\textwidth]{images/rtk3.png}
\subcaption{Image 3.}\label{fig:3}
\end{minipage}
\label{fig:1-2-3}
\caption{Title.}
\end{figure}


All minipages are aligned by the top of the figure once subcaptions could mess bottom alignment.