107

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?

1

3 Answers 3

111

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

4
  • 3
    My first use of mwe package @Martinscharrer
    – user11232
    Commented May 11, 2012 at 5:16
  • 1
    Thanks for using it! Note that you can use the images without loading the package. Using graphicx here directly might actually be better. Commented May 11, 2012 at 6:11
  • 1
    @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
    Commented May 11, 2012 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. Commented Jan 25, 2013 at 17:48
61

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

7
  • 1
    This is great. It just needs to also align the captions when the images don't share their height Commented Sep 19, 2013 at 18:37
  • 1
    @ThomasAhle, better use subfigure package for that purpose. Commented Jan 26, 2015 at 16:53
  • 1
    hmm .. this didn't work for me. Commented Dec 18, 2017 at 0:22
  • 2
    @dementedhedgehog Did you notice the subtle use of the parameter [H] following the environment declaration of the figures?
    – Jämes
    Commented May 31, 2018 at 23:32
  • 1
    For me the trick was to use the package pgf. I.e. \usepackage{pgf}.
    – Augustin
    Commented Oct 10, 2018 at 17:50
16

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .