3

If \newcommand{\ensuremath{something}} expands to

\ifmmode
 something
\else
 $something$
\fi

why is the spacing of

\newcommand{\x}{x_\textsc{y}} 
\newcommand{\y}{\ensuremath{x_\textsc{y}}}

different?

Why is the spacing $\x$ different?\\
Why is the spacing \y different?

spacing

Suuuehgi
  • 887
  • 4
  • 18
  • What happens with your code without \textsc? – Sigur Apr 22 '22 at 18:04
  • 1
    Maybe you want x_{\mathrm{Y}} instead?! – Sigur Apr 22 '22 at 18:05
  • Note you should not rely on _\text.. always use braces. It might work here and now, but noone has promised that it will keep working. The braced _{...} always will. – daleif Apr 22 '22 at 19:20
  • 4
    The space after \y is a command terminator and is removed. – Herb Schulz Apr 22 '22 at 19:45
  • 6
    I don't think this has anything to do with \ensuremath. It's simply because TeX swallows spaces after control sequences. In the line with $\x$ the math mode helps you avoid this effect. In spacing \y different the space is swallowed. See also https://tex.stackexchange.com/q/31091/35864 – moewe Apr 22 '22 at 19:46

2 Answers2

7

If \ensuremath were defined as

\newcommand{\ensuremath}[1]{%
   \ifmmode
      #1
   \else
      $#1$   %<--- space here!
   \fi
}

like you seem to believe, then your expectation would be correct: when defining

\newcommand{\foo}{\ensuremath{foo}}

and using \foo in text mode, the usual TeX rules would gobble the space after \foo but the space would be introduced by the (supposed) definition of \ensuremath.

The fact is, \ensuremath is not defined that way:

\DeclareRobustCommand{\ensuremath}{%
  \ifmmode
    \expandafter\@firstofone
  \else
    \expandafter\@ensuredmath
  \fi}
\long\def\@ensuredmath#1{$\relax#1$}

and the expansion doesn't introduce any space.

(Question to the audience: why \long?)

campa
  • 31,130
  • With \long the error messages are better, in case one has \par in the argument of \ensuremath. – egreg Apr 22 '22 at 21:13
  • 1
    @egreg Uhm, with \long the error is missing $ inserted, without \long it's paragraph ended before \@ensuredmath was complete. I can't say the former is clearer. But it was just curiosity, I would never use \ensuremath to begin with :-) – campa Apr 23 '22 at 08:31
  • @egreg: Why the \relax in the definition \long\def\@ensuredmath#1{$\relax#1$}? – rlsx Aug 07 '23 at 17:09
  • @rlsx Without the \relax, if #1 is empty we have $$, which starts 'raw' TeX display math mode: that's almost certainly not what we want. – Joseph Wright Aug 08 '23 at 07:57
  • @rlsx Adding to Joseph's comment, without \relax a wrong input such as \ensuremath{$x$} (in text mode) would pass without errors. – egreg Aug 08 '23 at 08:16
-1

The previous answers make abundantly clear why, with your definition:
* \newcommand{\y}{\ensuremath{x_\textsc{y}}}
\y removes the following space.
As a workaround, you may consider using the package xspace, and (re)defining \y:
* \newcommand{\y}{\ensuremath{x_\textsc{y}}\xspace}
If you follow this route, look at the customization afforded by \xspaceaddexceptions in that package.

rlsx
  • 19
  • Using \xspace is not recommended. Also x_\textsc{y} works by accident and should be x_{\textsc{y}}, but probably better is x_{\textup{\textsc{y}}}. I find no reason to use \ensuremath for this case, as $\y$ is much clearer. – egreg Aug 08 '23 at 13:44
  • Why is using xspace not recommended? (Otherwise, I wasn't trying the improve the TeXsyntax of the original question.) – rlsx Aug 08 '23 at 13:50
  • There are several corner cases it does not catch, and thus users have to keep this in might every time they use the macro. As egreg mentiones $\y$ is cleaner and give a proper visual separation between math and text at the source level. – daleif Aug 08 '23 at 14:01