# Why does xcolor not like \fbox (or vice versa)?

This document would not compile and a rather cryptic ! Illegal unit of measure (pt inserted) pops up. Here's the MnWE:

\documentclass{article}

\usepackage{xcolor}

\renewcommand{\fboxrule}{0.9pt}

\begin{document}
\fbox{\colorbox{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}}
\end{document}


The funny thing that puzzles me is that it turns immediately into a working example when I use color instead of xcolor. What does xcolor break? Why is the error related to units? What has the color got to do with it?

-

\fboxrule must be treated as a length macro!

\documentclass{article}

\usepackage{xcolor}

\setlength{\fboxrule}{0.9pt}
% the following is also fine.
% \relax is optional here.
% \fboxrule=0.9pt\relax

\begin{document}
\fbox{\colorbox{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}}
\end{document}

-
Okay, I get that, but still: why? Why did it work and why did xcolor break it and not some other random package? –  Count Zero Feb 10 '13 at 17:23
@CountZero: Please break my MWE above to illustrate the problem. –  please stop stalking Feb 10 '13 at 17:26
Your MWE is perfectly fine, I'm just very curious why in my MWE things went wrong? –  Count Zero Feb 10 '13 at 17:30
@CountZero: Because of your wrong setting \renewcommand{\fboxrule}{0.9pt}. –  please stop stalking Feb 10 '13 at 17:32
@tohecz: Okay, fine! Answer accepted. :) –  Count Zero Feb 10 '13 at 17:34

If you're using xcolor then there is the \fcolorbox command at your disposal

Try:

\fcolorbox{black}{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}


MWE:

\documentclass{article}

\usepackage{xcolor}

\setlength{\fboxrule}{0.9pt}
\setlength{\fboxsep}{3pt}
\pagestyle{empty}

\begin{document}

\fcolorbox{black}{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}

\end{document}


Note that \fboxrule and \fboxsep are lengths and not commands. To change their values, you need to use \setlength{\fboxrule}{<dim>}

Also, to avoid the messiness of \rule...\hspace...<content>\hspace... you could define your own command as

\newcommand{\myfbox}[1]{{\setlength{\fboxsep}{1em}%
\fcolorbox{black}{yellow}{#1}%
}}


I've used {{...}} to ensure that \setlength only changes the value of \fboxsep for the purposes of the new command.

MWE:

\documentclass{article}

\usepackage{xcolor}

\setlength{\fboxrule}{0.9pt}
\setlength{\fboxsep}{3pt}
\pagestyle{empty}

\newcommand{\myfbox}[1]{{\setlength{\fboxsep}{\dimexpr1em+3pt\relax}%
\fcolorbox{black}{yellow}{#1}%
}}

\begin{document}

\myfbox{\textbf{Random Text}} \fbox{\textbf{Random Text}}

\fcolorbox{black}{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}

\end{document}


You could go so far as to do:

\newcommand{\myfboxB}[1]{\fcolorbox{black}{yellow}
{\raisebox{0pt}%%                                   don't raisebox
[\dimexpr-1.25ex+4ex\relax]%%             change the height of box
[\dimexpr1.25ex\relax]%%                  change the depth of box
{\makebox[\dimexpr\width+2em\relax]{#1}%% change width of box
}}}


which will reproduce a box looking just like yours.

\documentclass{article}

\usepackage{xcolor}

\setlength{\fboxrule}{0.9pt}
\setlength{\fboxsep}{3pt}
\pagestyle{empty}

\newcommand{\myfbox}[1]{{\setlength{\fboxsep}{\dimexpr1em+3pt\relax}%
\fcolorbox{black}{yellow}{#1}%
}}

\newcommand{\myfboxB}[1]{\fcolorbox{black}{yellow}
{\raisebox{0pt}%%                                   don't raisebox
[\dimexpr-1.25ex+4ex\relax]%%             change the height of box
[\dimexpr1.25ex\relax]%%                  change the depth of box
{\makebox[\dimexpr\width+2em\relax]{#1}%% change width of box
}}}

\begin{document}

\myfbox{\textbf{Random Text}} \fbox{\textbf{Random Text}}

\fcolorbox{black}{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}
\myfboxB{\textbf{Random Text}}

\end{document}


-
Thanks, great answer! Unfortunately I can accept only 1 answer and the document I am working with is rather long with lots of \fboxes all over the place. I'm too lazy to clean that up now. Anyway, definitely: +1! –  Count Zero Feb 10 '13 at 17:59
No problem. I just thought I could show you a bit more what you could do and how you can make your file a bit easier to read. –  A.Ellett Feb 10 '13 at 18:00