# Figure inside a minipage [duplicate]

Possible Duplicate:
How to use figure inside a minipage?

For a kind of report, I have to write each section in a boxed space. To do that, I use the following :

\newcommand{\textbox}[1]{
\hfill
\centering
\fbox{
\begin{minipage}{0.95\textwidth}
\vspace{0.2cm}
#1
\end{minipage}
\newline
}
\vspace{1cm}
}


And the use is simply :

\textbox{
This is my first part.
}
\textbox{
This is my second part.
}


How to put figures inside these boxes ? If I try :

\textbox {
This is my third part.
\begin{figure}
\includegraphics[width=9cm]{myfigure.png}
\vspace{-12pt}
\caption{My caption.}
\label{fig:myfigure}
\end{figure}
}


It fails with the following error : LaTeX Error : Float(s) lost.

How to solve this problem ?

-

## marked as duplicate by lockstep, Stefan Kottwitz♦Oct 9 '12 at 13:53

You can not have a floating environment inside a non-floating box. So you can't have figure you can have \includegraphics and if you use the capt-of package you can have a labeled caption \captionof{figure}{... Or you can just box the contents of the figure and allow the box itself to float. –  David Carlisle Oct 9 '12 at 12:53
unrelated you need to put a % after every { and } in your definition otherwise the content is not centred in the box as you have varying numbers of word spaces either side. using \hfill and \centering together is rather odd. They will counter-act each other just use \centering and use \par not \newline at the end. –  David Carlisle Oct 9 '12 at 12:56

As told in the linked answer by lockstep, you can't put figure inside a minipage as the former is a float and latter is not. The solution is to define a separate figurebox in addition to textbox as shown in the MWE. Also you have to add % at appropriate places to avoid spurious spaces.

\documentclass{article}
\usepackage{graphicx}
\newcommand{\textbox}[1]{%
\par
\noindent
\fbox{%
\begin{minipage}{0.95\textwidth}%
\vspace{0.2cm}
\centering
#1
\end{minipage}%
}
\vspace{1cm}
}%
%
\newcommand{\figurebox}[4]{%
\begin{figure}[htb]%
\fbox{%
\centering
\begin{minipage}[c]{0.95\textwidth}
\centering #1\par
\includegraphics[width=3.0in]{#2}
\caption{#3}
\label{#4}
\end{minipage}%
}%
\end{figure}%
}%

\begin{document}
\noindent
\textbox{This is my first part.}%
\textbox{This is my second part.}
%% syntax is \figurebox{text}{figure file name}{caption}{label}
\figurebox{This is my third part}{example-image-a}{My figure}{fig:first}
\end{document}


Option -2

If you want to use the same textbox command for figures also, then you have to avoid the float figure To get the captions you can use caption or capt-of packages.

\documentclass{article}
\usepackage{graphicx,capt-of}
\newcommand{\textbox}[1]{%
\par
\noindent
\fbox{%
\begin{minipage}{0.95\textwidth}%
\vspace{0.2cm}
\centering
#1
\end{minipage}%
}
\vspace{1cm}
}%

\begin{document}
\noindent
\textbox{This is my first part.}%
\textbox{This is my second part.}
\textbox {%
This is my third part.
\includegraphics[width=9cm]{example-image-a}
\vspace{-12pt}
\captionof{figure}{My caption.}
\label{fig:myfigure}
}%

\end{document}
`
-