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?
Best Answer
Figure
is a floating environment andminipage
is, unfortunately, not. Therefore, if you put a floating object inside a non-floatingminipage
, you will get an error. But the other way around is possible--you can put aminipage
inside afigure
environment:Another method is to avoid using
figure
entirely. This can be done with help of thecaption
package (with itscaptionof
facility, so that you can have a caption for the figure):The total mwe will be:
The result will be: