此文档无法编译,并! Illegal unit of measure (pt inserted)
弹出一个相当神秘的提示。以下是 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}
让我感到困惑的是,它立即变成了在职的例如,当我使用color
而不是 时xcolor
。 什么会xcolor
中断? 为什么错误与单位有关? 颜色与它有什么关系?
答案1
\fboxrule
必须被视为长度宏!
\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}
答案2
如果你正在使用xcolor
,那么可以\fcolorbox
使用以下命令
尝试:
\fcolorbox{black}{yellow}{\rule[-1.25ex]{0pt}{4ex}\hspace{1em}\textbf{Random Text}\hspace{1em}}
梅威瑟:
\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}
请注意,\fboxrule
和\fboxsep
是长度而不是命令。要更改它们的值,您需要使用\setlength{\fboxrule}{<dim>}
此外,为了避免混乱你\rule...\hspace...<content>\hspace...
可以将自己的命令定义为
\newcommand{\myfbox}[1]{{\setlength{\fboxsep}{1em}%
\fcolorbox{black}{yellow}{#1}%
}}
我已经习惯{{...}}
确保仅为了新命令的目的而\setlength
改变的值。\fboxsep
梅威瑟:
\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}
你甚至可以这样做:
\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
}}}
它将复制一个和你的一模一样的盒子。
\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}