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 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.

share|improve this question
4  
You have an accept rate of 0%. May I suggest you go to your questions and upvote and accept these one which were usefull for you? That gives you a real good chance to get more answers ... –  Kurt Aug 28 '12 at 16:28
    
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
add comment

1 Answer

up vote 4 down vote accepted

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}
  {\sbox0{\ttfamily\hbadness=10000
   \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.

share|improve this answer
    
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
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.