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.

I'd like to frame the text between \startfoo and \stopfoo, but there is a float in it.

\documentclass{article}
\begin{document}

\startfoo

\begin{figure}[h]
some float with a \caption{...}
\end{figure}

\stopfoo
\end{document}

I've thought of using fancybox:

\documentclass{article}
\usepackage{fancybox}
\begin{document}

\begin{Sbox}
\begin{minipage}{\hsize}
\begin{figure}[h]
some float with a \caption{...}
\end{figure}
\end{minipage}
\end{Sbox}

\fbox{\TheSbox}
\end{document}

But this breaks due to the float inside the minipage, right? What can I do? The markup is fixed, I can't change it without very much labor.

share|improve this question
2  
You need to decide if you want to have a float or not. You can't catch a float in a box, it wouldn't be a float then. Simply place the figure in a center or similar environment and use \captionof{figure}{<caption text>} from the caption or (much smaller) capt-of package, instead of the figure environment, as shown in Label and caption without float. –  Martin Scharrer Sep 14 '11 at 10:26
    
What do you mean with "The markup is fixed"? Is the document generated automatically or are you including external files you can't change? In that case redefining figure locally would be an option. –  Martin Scharrer Sep 14 '11 at 10:34
    
Of course float and box contradict, but that's the way the markup currently is. So I've tried to renewcommand figure and endfigure to {}, but I don't get the caption to work: pastebin.com/d6bYHNzt –  topskip Sep 14 '11 at 10:41
    
@Martin Scharrer: the text is already there and used in other places as well, and I don't want to use all other style files "just" for this application –  topskip Sep 14 '11 at 10:43
add comment

2 Answers 2

up vote 8 down vote accepted

You need to redefine the float environment then if you don't want to change the code of the content. The frame can be added by the mdframed package (or if you don't need to support page breaks also by my adjustbox package: \begin{adjustbox}{fbox} <content> \end{adjustbox}).

\documentclass{article}

%\usepackage{capt-of}% or caption
\usepackage{mdframed}

\let\origfigure\figure
\let\endorigfigure\endfigure

\makeatletter
\newenvironment{figurehere}[1][]{%
   \center
   \def\@captype{figure}%  see note below
}{%
   \endcenter%
}

\newcommand{\startfoo}{%
    \par\medskip
    \begin{mdframed}[linewidth=1pt]%
    \let\figure\figurehere
    \let\endfigure\endfigurehere
    \ignorespaces
}
\newcommand{\stopfoo}{%
    \unskip
    \end{mdframed}%
    \par\medskip
}
\makeatother

\begin{document}
text before text before text before text before

\startfoo

text at begin of framed box
text at begin of framed box
text at begin of framed box

\begin{figure}[h]
some float with a \caption{...}
\end{figure}

text at end of framed box
text at end of framed box
text at end of framed box

\stopfoo

text after text after text after text after text after

\end{document}

Result

Note that is NEVER a good idea to use {figure}[h] (or [H], [h!]). It is against the float concept. Always use an environment like center with \captionof instead.

In this case it is not possible to redefine \caption like \def\caption{\captionof{figure}}, because \captionof is \def\@captype{#1}\caption and would call itself then. Instead I directly set \@captype to figure and so the normal \caption can be used.

share|improve this answer
    
Following up on your discusion: Is it possible that the framed box is long enough such that the figure floats only in that restricted area ? I mean can we come up with such environments like the boxes in the magazines which support the main story? –  percusse Sep 14 '11 at 11:04
    
@precusse: I'm writing on a TikZ based package which will draw frames like this without requiring to box the content, then you can simply use \FloatBarrier from the placeins package or a simple \clearpage if appropriate, to force the figure placing at the end (if it wasn't placed already). However, I would simply avoid the whole float-in-framed-box thing. –  Martin Scharrer Sep 14 '11 at 11:12
    
Wow, that is something we will look forward to it, then. So I can use it directly in my thesis. ;) –  percusse Sep 14 '11 at 11:46
add comment

If all you're trying to achieve is to place a framebox around a float (but not its caption), you could use the float package and issue the following instructions in the preamble:

\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}

and later on you can create figures as usual.

share|improve this answer
    
OK, I see that the question was not clear enough. There is more text in the framed environment besides the float. The float package/floatstyle will come in handy with another project - thanks! –  topskip Sep 14 '11 at 11:29
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.