2

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:

result

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.

colorbox suddenly much too small

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.

Christian
  • 19,238
  • have you seen Defining a new type of floating environment and how about the mdframed environment? – cmhughes Mar 07 '13 at 23:50
  • @cmhughes I'm having a look. Seems like the mdframed package might be useful to get rid of the strange lrbox/colorbox combination I'm using. – Christian Mar 07 '13 at 23:59
  • @cmhughes apparently mdframed does 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
  • @Christian ok, no problem, glad it worked out :) Looks like it'll be closed as a duplicate then :) – cmhughes Mar 08 '13 at 00:25

0 Answers0