You can do this using \tcboxmath from the theorems library of tcolorbox:
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \newcommand*{\mywbox}{% \tcboxmath[colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt]% } \begin{document} This \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document}

You may want to abstract things a little bit using a style too, so that you can apply it to other boxes should the need arise:
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \tcbset{my math box/.style={ colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt} } \newcommand*{\mywbox}{\tcboxmath[my math box]} \begin{document} This \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document}
Making the background of the box transparent
This can be done using /tcb/opacityback and a skin using a jigsaw frame engine, such as standard jigsaw or enhanced jigsaw.
\documentclass[fleqn]{article} \usepackage{tcolorbox} \tcbuselibrary{theorems} \tcbset{ my math box/.style={ standard jigsaw, % useful for 'opacityback' and 'opacityframe' colback=green!20, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt, opacityback=0.6, } } \newcommand*{\mywbox}{\tcboxmath[my math box]} \begin{document} This \makebox[0pt]{% \raisebox{-0.5\height}[0pt][0pt]{\hspace{1cm}\includegraphics{example-image-duck}}% }% \mywbox{3x} is in line math. This \[ y = \mywbox{-5x} - 5 + 6 \] is not in line math. \end{document}
