[Tex/LaTex] Frame around text and figure

boxesfloats

Consider the following

TEXT TEXT TEXT TEXT Figure~\ref{fig:foo} TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT

\begin{figure}
\centering
\includegraphics[width=\textwidth]{foo}
\caption{This is foo.}
\label{fig:foo}
\end{figure}

Now I would like to put a frame around the text and the figure. Ofcourse one could remove the figure-environment and wrap everything with the fbox-environment but I need to keep the caption and label (the latter for dynamic referencing):

\fbox{

TEXT TEXT TEXT TEXT Figure~1 TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT

    \includegraphics[width=\textwidth]{foo}

}

I tried the following code but it seems not work:

\fbox{

\parbox[c]{\textwidth}{
TEXT TEXT TEXT TEXT Figure~\ref{fig:foo} TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{foo}
\caption{This is foo.}
\label{fig:foo}
\end{figure}

}

Best Answer

This attempt of yours

\fbox{

\parbox[c]{\textwidth}{
TEXT TEXT TEXT TEXT Figure~\ref{fig:foo} TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{foo}
\caption{This is foo.}
\label{fig:foo}
\end{figure}

}

won't produce the desired result for two reasons:

  1. You can't use a floating environment (figure or table or any other user defined floating object) inside a box (in this case, an \fbox).

  2. Blank lines (or equivalently, \par commands) inside an \fbox (or an \hbox) won't produce an end of paragraph.

As others have suggested, you can use the framed package to produce a frame surroundig some material. You cannot use figure inside framed, but that's not a problem: instead, you can use the standard \includegraphics command (perhaps inside a center environment) to include your image and you can give it a caption using the \captionof command provided either by the capt-of package or by the caption package. A little example:

\documentclass{article}
\usepackage[demo]{graphicx}% demo option just for the example
\usepackage{framed}
\usepackage{caption}

\begin{document}

\begin{framed}
TEXT TEXT TEXT TEXT Figure~\ref{fig:foo} TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
\begin{center}
  \includegraphics[width=\textwidth]{foo}
  \captionof{figure}{This is foo.}
  \label{fig:foo}
\end{center}
\end{framed}

\end{document}

enter image description here

More easily customizable frames can be obtained using the mdframed package.

Related Question