The following code comes from this post. The problem is that I would like a way to automate the size of the box so as to be very small with a few of spacing, but the following solution doesn't work with fractions for example.
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][red]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-0.2em,0.9em)$) rectangle
($(br)+(0.2em,-0.3em)$);}
}
\newcommand{\MyBox}[2][red]{\tikzmark{bl}#2\tikzmark{br}\DrawBox[#1]}
\begin{document}
Lorem ipsum dolor sit amet. \MyBox{Lorem ipsum} dolor sit amet. Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet. \MyBox{$\frac{\frac{2}{5} - 7}{4 + x^3}$} Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum \MyBox[blue]{$3.29 \times 10^{29}$} dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
\MyBox[draw=red,fill=yellow!20,,opacity=0.3]{$3.29 \times 10^{29}$} ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
\end{document}
Best Answer
Another alternative could be
\tcbox
command fromtcolorbox
.declares a box with minimal space around its contents, aligned with surrounding text and white background color. All these default settings can be changed with the optional parameter. Just to show it, equivalences with Gonzalo's parameters are
frame color
->colframe
fill color
->colback
\fboxsep
->boxsep
box depth
->bottom
box height
->top
A complete example (taken from Gonzalo's answer)