# Using \ensuremath inside \text

Using \ensuremath inside \text works fine and gives good results. However, I realized that the spacing in superscripts changes. I tried working with \mathchoice, but that doesn't know that it is in a superscript.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\newcommand*\somepackagecommand[1]{\text{#1}}
\newcommand*\mytextmathstatement{calculate \ensuremath{1-1}}
\sffamily
\noindent
\mytextmathstatement\\
$\somepackagecommand{\mytextmathstatement}$\\% ok - identical
\somepackagecommand{\mytextmathstatement}% ok - identical

\noindent
$2^{\text{calculate }1-1}$\\
$2^{\somepackagecommand{\mytextmathstatement}}$\\% spacing differs
\end{document}


It's a weird thing. I get the superscript font size, but the (scaled-down) normal spacings. Setting \scriptstyle by hand would shrink the font too much.

Any idea how to 'perfectly escape' from within \text to \math?

• Welcome to TeX.SX! I don't recommend this! Use \text for real non-math typesetting and \ensuremath for real math content – user31729 Mar 19 '15 at 15:17

It's not a mystery and \ensuremath has nothing to do with this. You of course get the same result from

$a^{\text{calculate \ensuremath{1-1}}}$


and

$a^{\text{calculate$1-1$}}$


Why this is different from $a^{\text{calculate }1-1$?

By general TeX rules, spaces around binary operation or relation symbols are not added if they appear in a superscript or in a subscript.

However, \text works in a different fashion: when you say \text{<material>} you're really typesetting four boxes:

\mbox{\everymath{\displaystyle}\fontsize\f@size{0}\selectfont <material>}
\mbox{\everymath{\textstyle}\fontsize\f@size{0}\selectfont <material>}
\mbox{\everymath{\textstyle}\fontsize\sf@size{0}\selectfont <material>}
\mbox{\everymath{\textstyle}\fontsize\ssf@size{0}\selectfont <material>}


and then TeX chooses the one that's relevant, in your case the third one. So math is typeset as if it were at the higher level, which means spaces are inserted around binary operation or relation symbols.

Implementing the removal of those spaces could only be done by setting \medmuskip and \thickmuskip to zero, but this would mean you can't use them to add space where you want it.

You get the tighter superscript spacing back with

$2^{\somepackagecommand{calculate \ensuremath{1{-}1}}}$


It's probably possible to make a variant of \text that does this automatically but better really to avoid \text and \ensuremath

I would normally use

$2^{\text{calculate }1-1}$


If you really need inter-word spacing and

$2^{\mathrm{something}-2}  if you don't. In the updated MWE you could use $2^{\medmuskip=0mu \somepackagecommand{\mytextmathstatement}}\$


There's a chance someone has used \medmuskip explicitly and this would also affect that as well as automatic spacing around binary operators, but probably, they haven't.

• Yes, this would work around this issue. I was interested in a real escaping solution. I therefore changed the MWE in my question. – mh246 Mar 19 '15 at 15:32
• @mh246 extended my answer for the new MWE – David Carlisle Mar 19 '15 at 15:36