How to use figure inside a minipage?

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:

enter image description here


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}

Two figures side by side using minipages


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}

example 1

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}

example 2

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