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.

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?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

\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}
share|improve this answer
    
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
1  
@CountZero: Please break my MWE above to illustrate the problem. –  In PSTricks we trust 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}. –  In PSTricks we trust Feb 10 '13 at 17:32
1  
@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}

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

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

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.