The framed
or mdframed
packages allow you to put text in frames. Here's a way to do what you want with mdframed
.
\documentclass{article}
\usepackage{caption}
\usepackage{listings}
\lstset{extendedchars=true,
tabsize=3,
frame=none,
showspaces=false,
boxpos=c,
float=h,
escapechar=\%
}
\usepackage[style=1]{mdframed}
\newcounter{boxctr}
\DeclareCaptionType{boxx}
\usepackage{needspace}
\newenvironment{Boxx}[2]{%
\captionsetup{type=boxx}
\begin{mdframed}[%
linewidth=1pt,
roundcorner=10pt,%
leftmargin=.05\textwidth,
rightmargin=.05\textwidth,
skipabove=.7\baselineskip]%
\refstepcounter{boxctr}%
\label{#1}%
\gdef\boxxcaption{\caption{#2}}
\needspace{2\baselineskip}
}{%
\end{mdframed}%
\boxxcaption%
}
\usepackage{lipsum}
\begin{document}
This is a reference to Box~\ref{box:one}!
\vspace{1.5cm}
\lipsum[1-4]
\begin{Boxx}{box:one}{This is a caption}
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\end{Boxx}
\lipsum[1]
\begin{Boxx}{box:two}{This is also a caption}
\begin{lstlisting}
Also works with listings
\foo is escaped!
\end{lstlisting}
\end{Boxx}
\lipsum[2]
\end{document}
Boxes take two extra arguments: a label and a caption, just like the environment defined in the question.
They use the caption
package's captioning magic. So this should make doing lists of boxes easier. The label should stick with the bottom of the box now. This might lead to some ugly whitespace, but other than making the boxes float, this is probably as good as it gets.
You can define your own \floatstyle
. In this case I took the definition from the float
package and adjusted it to use the mdframed
package environment to draw the frame.
Here is a comparison of using \floatstyle{boxed}
and \floatstyle{myRoundBox}
:

Note:
- I took the liberty to add a background color, and
tikzsetting
to illustrate some of the customization that are possible -- see the mdframed
documentation for more options.
- Updated to incorporate Torbjørn T.'s suggestion of adding a
\vspace{\abovecaptionskip}
in \def\@fs@mid{}
as per How to frame a figure in Lyx.
Code:
\documentclass{article}
\usepackage{graphicx}
\usepackage{float}
\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{myFigureBoxStyle}{backgroundcolor=yellow!10,, roundcorner=25pt,tikzsetting={draw=blue, line width=1pt}}%
\makeatletter
\newcommand\fs@myRoundBox{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain
\def\@fs@pre{\begin{mdframed}[style=myFigureBoxStyle]}%
\def\@fs@mid{\vspace{\abovecaptionskip}}%
\def\@fs@post{\end{mdframed}}\let\@fs@iftopcapt\iffalse}
\makeatother
\floatstyle{myRoundBox}
\restylefloat{figure}
\begin{document}
\floatstyle{boxed}
\restylefloat{figure}
\section{Using boxed}
\begin{figure}[h]
\centering
\includegraphics[width=0.35\textwidth]{../images/EiffelTall}
\caption{Some caption}
\end{figure}
\floatstyle{myRoundBox}
\restylefloat{figure}
\section{Using myRoundBox}
\begin{figure}[h]
\centering
\includegraphics[width=0.35\textwidth]{../images/EiffelTall}
\caption{Some caption}
\end{figure}
\end{document}
Best Answer