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}