Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

}
share|improve this question
    
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
add comment

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
add comment

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.