# Conditionally remove the space before a one-letter operator

I'm pursuing the Math Expectation operator (\mathbb{E}).

As \DeclareMathOperator is not very good for one-letter operators (since it adds some spacing after itself), I use a plain \newcommand. But here comes the problem that there's no space before it, so writing something like X \MyExpct X turns into something without spaces, while I would like to see a space between the first X and \MyExpct.

But adding a spacing into \newcommand before \mathbb{E}, like

\newcommand{\MyExpct}{\,\mathbb{E}}


fails when my operator stand in the beginning of an expression! So what I want is a space which would be added before the operator if something (but not a binary operation or a opening bracket!) stands before it, and which would be removed otherwise.

I tried to play with \unskip, \ignorespaces, and even looked into how \DeclareMathOperator deals with it (via \kern) but failed to find the solution.

-
Welcome to TeX.SX! Math operators are expected to have a thin space after them, if not followed by a parenthesis or other delimiter. I don't know why you wouldn't want it, but \newcommand{\MyExpct}{\mathop{}\!\mathbb{E}} should do. –  egreg Oct 25 '13 at 15:22
Thanks @egreg! Yes, since expectation is a sort of an operator which doesn't look neat when it doesn't have parenthesis, I wanted to remove this space. In other words, to "visually attach" the argument to the E-symbol. –  gron Oct 25 '13 at 18:50

## 1 Answer

The rule for a math operator is that in case it's preceded/followed by an ordinary symbol, then a thin space is inserted. This is what's wanted in cases such as

$a\log b$


where we do want a thin space before and after “log”. In a case such as

$a\log(bc)$


the space is inserted only before “log”.

I don't know why you don't want your operator to behave differently; perhaps you don't want to add parentheses and trust on the different shape of the letter for visual distinction. So here's the way:

\newcommand{\MExp}{\mathop{}\!\mathbb{E}}


If \MExp is preceded by an ordinary operator, the thin space will be inserted. However, since the empty \mathop is followed by an ordinary symbol, it would always insert a thin space, that we remove with \!.

\documentclass{article}
\usepackage{amsmath,amssymb}
\newcommand{\MExp}{\mathop{}\!\mathbb{E}}

\begin{document}
X$\MExp$ has no space

$X \MExp X$ shows space and no space
\end{document}


-
I'm not a fan of the syntax, but I've seen it before as in XS for the "closure" or "pointed" version of S. But then again is X then really an operator? In the OPs case a bit more information might be in order –  daleif Oct 25 '13 at 16:22
Thanks! Yes, exactly, I wanted to work with expectation also without parentheses. In this case the space makes it a bit visually detached from its argument. –  gron Oct 25 '13 at 18:51