# Colorboxes that break across pages

I am trying to build an environment/command that allows me to place comments in the text. The idea is to have colored box that can break across pages and use the \ttfamily font with hyphenation. I also want to use any structure (table, enumerate, itemize, etc.) inside it. Moreover the box width is variable and the maximum is \linewidth. My MWE is below.

\documentclass{article}

\usepackage{blindtext}
\usepackage[showframe]{geometry}

\usepackage{xcolor}
\usepackage{calc}
\usepackage{mdframed}
\usepackage{environ,varwidth}
\newsavebox\MyTempBox
\NewEnviron{mycomment}{%
\savebox\MyTempBox{%
\begin{varwidth}{\linewidth}
\BODY
\end{varwidth}}%
\begin{mdframed}
[topline=false,
rightline=false,
bottomline=false,
leftline=false,
innerleftmargin=1ex,
innerrightmargin=1ex,
innertopmargin=1ex,
innerbottommargin=1ex,
backgroundcolor=pink,
font=\ttfamily,
userdefinedwidth=\dimexpr\wd\MyTempBox\relax
]
\hyphenchar\font=\defaulthyphenchar\relax
\BODY
\end{mdframed}%
}%

\begin{document}

\begin{mycomment}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\end{mycomment}

\end{document}


So far it almost works, the problem is that it generates a lot of Overfull \hbox because the text does strictly not respect the box boundaries. If I put the second \BODY inside a minipage

\begin{minipage}{\linewidth}
\BODY
\end{minipage}


It works better, but I loose the break over pages feature. I think that a minipage that can break over pages can be the solution.

I am not attached to the mdframed package. For example,

\newcommand{\ccomment}[1]{%
\noindent\colorbox{pink}{\begin{varwidth}{\linewidth-1em}%
\ttfamily
\hyphenchar\font=\defaulthyphenchar\relax % enable hyphenation
#1
\end{varwidth}}}


Would be ok for me, but once more I cannot use it for long text that may break across pages.

Thanks.

-
The problem is the font \ttfamily. Without this font everything works well. Why have your comments to be in type writer? If they have to be type write may be package listings can help? –  Kurt Aug 28 '12 at 17:27

I'd simply use \RaggedRight from the ragged2e package:

\documentclass{article}

\usepackage{blindtext}
\usepackage[showframe]{geometry}
\usepackage{xcolor}
\usepackage{ragged2e}
\usepackage{mdframed}

\newenvironment{mycomment}
{\begin{mdframed}[topline=false,
rightline=false,
bottomline=false,
leftline=false,
innerleftmargin=1ex,
innerrightmargin=1ex,
innertopmargin=1ex,
innerbottommargin=1ex,
backgroundcolor=pink,
font=\ttfamily,
]\hyphenchar\font=\defaulthyphenchar\RaggedRight}
{\end{mdframed}\ttfamily\hyphenchar\font=-1 }

\begin{document}

\begin{mycomment}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\end{mycomment}

\end{document}


The final setting of \hyphenchar is to restore the default no hyphenation in the typewriter font (such assignments are always global).

For accommodating varwidth you have to supply the right line width; this of course needs the environ and varwidth packages.

\NewEnviron{mycomment}
\begin{varwidth}{\dimexpr\linewidth-2ex\relax}
\BODY
\end{varwidth}}%
\begin{mdframed}[topline=false,
rightline=false,
bottomline=false,
leftline=false,
innerleftmargin=1ex,
innerrightmargin=1ex,
innertopmargin=1ex,
innerbottommargin=1ex,
backgroundcolor=pink,
font=\ttfamily,
userdefinedwidth=\dimexpr\wd0+2ex\relax
]\hyphenchar\font=\defaulthyphenchar\RaggedRight\BODY
\end{mdframed}\ttfamily\hyphenchar\font=-1 }


With \hbadness=10000 you turn off spurious Underfull \hbox messages.

-
I need an extra argument that tells me the width of the box, because sometimes the text does not have the text width, like I have in my MWE. The use of \RaggedRight controls better the size of text, but it still gives me a lot of Overfull \hbox and if the text has less that text width I think it does not provide any result. –  cacamailg Aug 28 '12 at 18:47
@cacamailg I've added a version with varwidth –  egreg Aug 28 '12 at 19:34
I still see some Overfull \hbox but not as before. And you solution works well most of the time. Thanks! –  cacamailg Aug 29 '12 at 11:38