1

I want to typset some text with colored background and outline, that spans the whole \textwidth. I tried to get this with a colorbox surrounding a parbox. It works in some way, but the colorbox is always a bit bigger than the parbox, causing the colored fill to overlap the outline a bit: colored fill overlaps outline

Is there any way to fix this? I didn't find one. The corresponding code:

\newcommand{\information}[1]{\colorbox{DarkOliveGreen2}{\fboxsep4mm\framebox[\textwidth][l]{\parbox{\linewidth}{\textbf{foo: } #1}}}}

Any ideas are greatly appreciated. Thanks!

Edit: As supposed in the comments: I already tried removing or changing the \fboxsep-value, but it doesn't change the behavior. Just the space between the text and the border of the boxes tightens, and I want that space...

4
  • 2
    You forgot the \fboxsep value, i.e. it's too large
    – user31729
    Commented Jan 27, 2016 at 13:43
  • you use \fboxsep4mm so you can change it
    – touhami
    Commented Jan 27, 2016 at 13:44
  • You say you didn't find anything but where did you look:-) any documentation that tells you about \colorbox should tell you that it uses \fboxsep Commented Jan 27, 2016 at 13:45
  • The fboxsep before the framebox sets the padding for the frame you need to set it (to 0pt) before the colorbox if you don't want extra padding for that. Commented Jan 27, 2016 at 13:51

2 Answers 2

4

The space between the box edges and the real content is ruled by \fboxsep as for the usual \fbox command.

Since \fboxsep4mm appears explicitly in the \information macro definition there's no chance to change the \fboxsep value before. 4mm seems to be definitely too much, but this is a matter of taste, of course.

In my point of view, there's no need of using an additional \framebox command, since \fcolorbox is also available.

\documentclass{article}

\usepackage[x11names]{xcolor}
\begin{document}


\newcommand{\information}[2][0pt]{%
  \fboxsep=#1%
  \par\noindent%
  \fcolorbox{black}{DarkOliveGreen2}{%
    \parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{\textbf{foo: }#2}%
  }% Reduce the box size by 2 \fboxsep and 2\fboxrule widths. 
}


\information[2pt]{Foo}

\end{document}

enter image description here

4
  • Overfull \hbox (17.0pt too wide) in paragraph at lines 13--14 (15pt indent and 2pt you just added with fboxsep :( Commented Jan 27, 2016 at 13:48
  • post early to keep egreg out? worth a try:-) Commented Jan 27, 2016 at 13:49
  • he will if you don't set fboxsep to 0pt on the outside and 4mm on the inside:-) Commented Jan 27, 2016 at 13:52
  • @LukeLR: Well, smarter? .... depends.. Anyway, I would suggest to use tcolorbox or mdframed rather than using \colorbox, but the later requires a smaller package, that's an advantage of \colorbox of course
    – user31729
    Commented Jan 27, 2016 at 14:11
4

You need to control the padding of \framebox and \colorbox separately:

\documentclass{article}

\usepackage[x11names]{xcolor}
\begin{document}


\newcommand{\information}[1]{{%
\setlength{\fboxsep}{0pt}%
\par\noindent
\colorbox{DarkOliveGreen2}{%
\setlength{\fboxsep}{4mm}%
\framebox[\textwidth][l]{\parbox{\linewidth}{\textbf{foo: } #1}}}}}


\information{Foo}

\end{document}
1
  • I followed your comment above the original post and used the following code: \newcommand{\information}[1]{\fboxsep0mm\colorbox{DarkOliveGreen2}{\fboxsep4mm\framebox[\textwidth][l]{\parbox{\linewidth}{\textbf{foo: } #1}}}} That did the trick! The idea to control the seps independently fixed it! Thanks!
    – LukeLR
    Commented Jan 27, 2016 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .