4

Hi a very basic question I'm sure, but is there a way to tell \fcolorbox in xcolor to draw the box of a certain width.

For example with \fbox I would do

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\framebox[1in]{text}

\fcolorbox{blue}{blue!40}{what do I do here?}

\end{document}

1 Answers1

7

There is no \framecolorbox macro, but you can easily build one with xparse:

\usepackage{xparse}
\NewDocumentCommand{\framecolorbox}{oommm}
 {% #1 = width (optional)
  % #2 = inner alignment (optional)
  % #3 = frame color
  % #4 = background color
  % #5 = text
  \IfValueTF{#1}
   {\IfValueTF{#2}
    {\fcolorbox{#3}{#4}{\makebox[#1][#2]{#5}}}
    {\fcolorbox{#3}{#4}{\makebox[#1]{#5}}}%
   }
   {\fcolorbox{#3}{#4}{#5}}%
 }

Complete example:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\NewDocumentCommand{\framecolorbox}{oommm}
 {% #1 = width (optional)
  % #2 = inner alignment (optional)
  % #3 = frame color
  % #4 = background color
  % #5 = text
  \IfValueTF{#1}
   {%
    \IfValueTF{#2}
     {\fcolorbox{#3}{#4}{\makebox[#1][#2]{#5}}}
     {\fcolorbox{#3}{#4}{\makebox[#1]{#5}}}%
   }
   {\fcolorbox{#3}{#4}{#5}}%
 }

\begin{document}
\framecolorbox{blue}{blue!40}{what do I do here?}

\framecolorbox[4cm]{blue}{blue!40}{what do I do here?}

\framecolorbox[4cm][l]{blue}{blue!40}{what do I do here?}

\framecolorbox[4cm][r]{blue}{blue!40}{what do I do here?}

\framecolorbox[4cm][s]{blue}{blue!40}{what do I do here?}
\end{document}

enter image description here

egreg
  • 1,121,712
  • You have the package xparse loaded twice. Why does it issue an Overfull \hbox when I use a width of say \textwidth? – azetina Aug 03 '14 at 23:23
  • @azetina Thanks for noting the double loading (innocuous, but bad practice). The \framecolorbox macro produces a box with some padding, as usual for \framebox, so you have to take into account \fboxsep and \fboxrule. – egreg Aug 04 '14 at 09:01