I'm trying to write an environment that creates a new float type for text boxes. These are used for example in some books to provide interesting tidbits that aren't really part of the main line of argument.
I'm using memoir features for this (the \newlistof/\newfloat stuff obviously but also vminipage) but any solution that can do without is of course just as welcome.
So this is what I got so far
\documentclass{memoir}
\usepackage{xcolor}
\makeatletter
\newcounter{boxcounter}
\setcounter{boxcounter}{0}
\newcommand{\elaboxname}{Box}
\newcommand{\listelaboxname}{List of Boxes}
\newlistof{listofboxes}{box}{\listelaboxname}
\newfloat{elabox}{box}{\elaboxname}
\newfixedcaption{\felaboxcaption}{box}
\newlistentry{elabox}{box}{0}
\definecolor{BoxColor}{rgb}{0.91, 0.91, 0.91}
\newenvironment{digression}[2][tp]%
{\begin{elabox}[#1]{}\begin{lrbox}{\@tempboxa}\begin{vminipage}{\linewidth}%
\refstepcounter{boxcounter}\textbf{\elaboxname~\arabic{boxcounter}: #2}\par\aftergroup\@afterindentfalse
\aftergroup\@afterheading}%
{\end{vminipage}\end{lrbox}\colorbox{BoxColor}{\usebox{\@tempboxa}}\end{elabox}}
\makeatother
\usepackage{lipsum}
\begin{document}
\lipsum[2-7]
\begin{digression}{By the Way}\label{box:BTW}
\lipsum[8-9]
\end{digression}
\lipsum[10] Also see Box~\ref{box:BTW}.
\lipsum[11-12]
\end{document}
which looks like this:

Not completely awful but not good either. The box is too wide and the text inside the box has no margin. Use microtype and the hyphens will almost touch the border.
To solve this sounds simple enough but somehow I'm stuck. I tried to put another minipage around the existing one like so:
{\end{vminipage}\end{lrbox}\colorbox{BoxColor}{\centering\begin{vminipage}{\linewidth}\usebox{\@tempboxa}\end{vminipage}}\end{elabox}}
and make the inner one smaller. This completely messes things up and I have no idea why.

I tried saving the old \linewidth into a new length but that didn't change anything.
Bonus points (literally) if someone can come up with a way to create a box environment that can span more than one page if need be.
mdframedenvironment? – cmhughes Mar 07 '13 at 23:50mdframedpackage might be useful to get rid of the strangelrbox/colorboxcombination I'm using. – Christian Mar 07 '13 at 23:59mdframeddoes solve all my problems. I can just use{\begin{elabox}[#1]{}\begin{mdframed}[backgroundcolor=black!10,hidealllines=true]\refstepcounter{boxcounter}\textbf{\elaboxname~\arabic{boxcounter}: #2}\par\aftergroup\@afterindentfalse \aftergroup\@afterheading}{\end{mdframed}\end{elabox}}and everything looks fine. Didn't get the frametitle to work with the counter but the manual way is fine by me. This can be marked as a duplicate or you can turn your comment into an answer, I don't care. – Christian Mar 08 '13 at 00:20