3

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?

0

2 Answers 2

5

\fboxrule must be treated as a length macro!

enter image description here

\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}
5
  • 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
    Commented Feb 10, 2013 at 17:23
  • 1
    @CountZero: Please break my MWE above to illustrate the problem. Commented Feb 10, 2013 at 17:26
  • Your MWE is perfectly fine, I'm just very curious why in my MWE things went wrong?
    – Count Zero
    Commented Feb 10, 2013 at 17:30
  • @CountZero: Because of your wrong setting \renewcommand{\fboxrule}{0.9pt}. Commented Feb 10, 2013 at 17:32
  • @CountZero Garbage Collector explained that your document contains a serious mistake: trying to \renewcommand a length. If you correct this, you gon't get any mistakes. I don't think it makes much sense to analyze where and why user mistake bubbled to a mistake related to some package, as long as it's obviously a user mistake.
    – yo'
    Commented Feb 10, 2013 at 17:32
4

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}

enter image description here

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}

enter image description here

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}

enter image description here

2
  • 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
    Commented Feb 10, 2013 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
    Commented Feb 10, 2013 at 18:00

You must log in to answer this question.

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