TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
3  
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
up vote 52 down vote accepted

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

share|improve this answer
3  
My first use of mwe package @Martinscharrer – Harish Kumar May 11 '12 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. – Martin Scharrer May 11 '12 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. – 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}

Two figures side by side using minipages

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.