14

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}

}
strpeter
  • 5,215
John
  • 1,953
  • figure is a floating environment which means it can always move away from TEXT. This means there is no way it can be put into a box together with TEXT. Look at the caption package. Among others it offers the \captionof command which allows to have a caption without a floating environment. – Stephan Lehmke Apr 30 '12 at 16:53

1 Answers1

19

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.

Gonzalo Medina
  • 505,128