\ensuremath
Synopsis:
\ensuremath{formula}
Ensure that formula is typeset in math mode.
For instance, you can redefine commands that ordinarily can be used only in math mode, so that they can be used both in math and in plain text.
\newcommand{\dx}{\ensuremath{dx}} In $\int f(x)\, \dx$, the \dx{} is an infinitesimal.
Caution: the \ensuremath
command is useful but not a panacea.
\newcommand{\alf}{\ensuremath{\alpha}} You get an alpha in text mode: \alf. But compare the correct spacing in $\alf+\alf$ with that in \alf+\alf.
Best is to typeset math things in a math mode.
© 2007–2018 Karl Berry
Public Domain Software
http://latexref.xyz/_005censuremath.html