A better expectation operator which plays nicely with undersetting?

I would like to define an expectation operator that acts like \iint in the following sense: I want it to behave nicely with limits in that I would like it to default to place text to the right, but with \limits, place it under. Here's an example of ways that don't work and do work.

The last line is my desired result, but is done manually. How do I do this automatically?

The code producing this is below.

\documentclass[preview]{standalone}
\usepackage{amsmath}
\DeclareMathOperator*{\E}{\bf E}
\DeclareMathOperator{\EE}{\bf E}
\newcommand{\EEE}{\mathop{{\bf E}}}
\begin{document}
$\iint_{R} f(x, y)\, dx dy \qquad \mbox{versus} \qquad \iint\limits_{R} f(x, y) \, dx dy$
$\E_{R} f(X, Y) \qquad \mbox{versus} \qquad \E\limits_{R} f(X, Y)$
$\EE_{R} f(X, Y) \qquad \mbox{versus} \qquad \EE\limits_{R} f(X, Y)$
$\EEE_{R} f(X, Y) \qquad \mbox{versus} \qquad \EEE\limits_{R} f(X, Y)$
desired result (but less ad-hoc)
$\mathbf{E}_{R} f(X, Y) \qquad \mbox{versus} \qquad \underset{R}{\mathbf{E}}~f(X, Y)$
\end{document}

• Note that \bf has been deprecated for 25 years. Oct 14 '19 at 20:07

You can use the model of \int for this:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\E}{\operatorname*{\mathbf{E}}\ilimits@}
\makeatother

\begin{document}

$\iint_{R} f(x, y)\, dx \, dy \qquad \text{versus} \qquad \iint\limits_{R} f(x, y) \, dx \, dy$
$\E_{R} f(X, Y) \qquad \text{versus} \qquad \E\limits_{R} f(X, Y)$

\end{document}


Using \ilimits@ will make the command obey the intlimits option just like \int.

Use \nolimits instead of \ilimits@ if you don't want that.

• Thanks. I'll take your comment about \bf (and your more subtle hint about \text versus \mbox) under advisement. Have you thought about compiling some of these common TeX deprecations/crimes against LaTeX? Oct 26 '19 at 4:42

For some reason, \DeclareMathOperator will not react to \limits. However, \DeclareMathOperator* will react to \nolimits. So this should give you the result you are lookiing for:

\DeclareMathOperator*{\E}{{}\bf E}

$\E\nolimits_{R} f(X, Y) \qquad \mbox{versus} \qquad \E_{R} f(X, Y)$


For technical reasons, I'm not able to show output. (Since retiring from AMS, I have lost access to all my previous resources, and haven't yet rebuilt a working system.)

The presence of the {} in the definition is intended to counteract the default positioning of a single character as an operator to be vertically centered on the math axis. This is the default (specified in Appendix G of the TeXbook) so that operators such as \sum, \int, etc., are properly positioned.

The inability of the unstarred \DeclareMathOperator to react to \limits seems unreasonable, so I will make a request for it to be implemented in a future release of amsopn.

Update. I have been urged to check the documentation in amsopn.dtx, and there I find that \limits has been purposely inhibited, as pointed out by @egreg. I therefore withdraw the suggestion that it be made available.

• \DeclareMathOperator has been defined purposely not to obey to \limits and I fully agree with the decision. It is for operators which never take limits; those which do are defined with \DeclareMathOperator*. Oct 14 '19 at 19:46
• @egreg -- I stand corrected. The restriction on \limits is documented in amsopn.dtx, which I didn't check before. I retract the suggestion that it should be made available. Oct 15 '19 at 2:02

Your first operator works the other way:

\documentclass[preview]{standalone}

\usepackage{amsmath}
\DeclareMathOperator*{\E}{\mathbf E}
\DeclareMathOperator{\EE}{\mathbf E}
\newcommand{\EEE}{\mathop{{\mathbf E}}}

\begin{document}
$\iint_{R} f(x, y)\, dx dy \qquad \mbox{versus} \qquad \iint\limits_{R} f(x, y) \, dx dy$
$\E_{R} f(X, Y) \qquad \mbox{versus} \qquad \E\nolimits_{R} f(X, Y)$

\end{document}


• Makes sense. I selected the other one, because the default behavior I'd like to be what yours requires \nolimits for. Useful information, nonetheless, however. Oct 10 '19 at 23:57
• The ‘normal way’ doesn't seem to work for a reason I can't explain. Maybe some guru will have an explanation. Oct 11 '19 at 0:00
• \DeclareMathOperator{\a}{b} basically defines \a as \mathop{\operator@font{b}}\nolimits@, where \nolimits@ is an ams macro written so that \nolimits@\limits expands to \nolimits, so it stops you changing the limits behaviour. I don't know why amsmath is designed like this. If you write \EE\relax\limits_R with the example above it will look OK, so an alternative would be \DeclareMathOperater{\E}{\mathbf{E}} \newcommand\EE{\E\relax}, and this might be better in another instance if the operator font effects were desired. Oct 12 '19 at 17:41
• @AlexWatson: The reason why the amsmath (actually, the amsopn) package is designed like that is, according to the comments in amsopn.dtx, to prevent an explicit “\limits typed by the user from having any effect. This is used for operatornames whose standard usage is never to have limits”. In other words, people at the AMS were concerned that somebody could smuggle something like \sin\limits^2 x + \cos\limits^2 x = 1 into a submitted manuscript and have such an eyesore make it in print on some of the journals they publish. But \EE\limits\limits isn’t catched…
– GuM
Oct 12 '19 at 23:51
• Perhaps @barbarabeeton can cast more light on the rationale that lay behind the choice of having \DeclareMathOperator behave the way it does.
– GuM
Oct 13 '19 at 11:46

You can specify \nolimits after the definition of the operator:

\documentclass[preview]{standalone}

\usepackage{amsmath}

\newcommand\EE{\mathop{{}\mathbf{E}}\nolimits}

\begin{document}

$\EE_R F \text{ versus } \EE\limits_R F$

\end{document}


You can find a hint to this in a double-dangerous-curve part of chapter 17 in the TeXBook: 'If you say \nolimits\limits (presumably because some macro like \int specifies \nolimits, but you do want them), the last word takes precedence.'

The default behaviour of operators is \displaylimits, as you found.

(Following egreg's comment: the {}\mathbf{E} ensures the correct the vertical alignment.)

• Perfect. Thanks! Oct 10 '19 at 23:55
• This vertically moves the E; check against \newcommand{\fixedEE}{\mathop{{}\mathbf{E}}\nolimits} Oct 14 '19 at 20:08
• Thanks @egreg, I have updated the answer. Oct 15 '19 at 11:09