2

If I define a new command like:

\newcommand{\expect}[2]{\mathbb{E}_{#2}\left[#1\right]}

which produces for

\expect{x}{y^2}

enter image description here

How can I get rid of the small space between the expectation and [].

7
  • 1
    Why there is \left[...\right] and not only [...]? This \left/\right pair creates an inner atom in math list and this gives the spacing.
    – wipet
    Commented Oct 17, 2022 at 12:15
  • Okay, I didn't know that. I use it because sometimes some expressions within [] are so to large like $$\mathbb{E}[(X_0)^2]$$ Commented Oct 17, 2022 at 12:17
  • In this case, I guess that also the \scriptspace is added. The mleftright package might be interesting for you.
    – mickep
    Commented Oct 17, 2022 at 12:18
  • With \left[(X_0)^2\right] the brackets are oversized and the result is much worse than [(X_0)^2].
    – egreg
    Commented Oct 17, 2022 at 12:19
  • Thanks! I will have a look. @egreg what would be good solution to '\int_{[0,1]}|\mathbb{E}[x_{1}^{K_y} -x_1]- \mathbb{E}[x_2^{K_y}-x_2]|' ? Commented Oct 17, 2022 at 12:24

1 Answer 1

2

The excess space is due to two factors:

  1. the main one is from \left[;
  2. the other space comes from \scriptspace because of the subscript to \mathbb{E}.

Before analyzing a solution, please consider the output of

\documentclass{article}
\begin{document}
\[
\left[(\hat{X}_0)^2\right] \quad [(\hat{X}_0)^2]
\]
\end{document}

and judge for yourself whether you really want to apply \left and \right indiscriminately.

enter image description here

Now a possible solution with \DeclarePairedDelimiter. First we typeset the operator “E” with its subscript, then delegate the rest to \expectvalue.

Due to the possible absence of the subscript, I make the relevant argument optional.

\documentclass{article}
\usepackage{mathtools}
\usepackage{amssymb}

\NewDocumentCommand{\expec}{sO{}mo}{%
  \operatorname{\mathbb{E}}%
  \IfValueT{#4}{_{#4}\kern-\scriptspace}%
  \IfBooleanTF{#1}{\expectvalue*{#3}}{\expectvalue[#2]{#3}}%
}
\DeclarePairedDelimiter{\expectvalue}{[}{]}

\begin{document}
\[
\expec{x}[y^2] \quad
\expec{(X_0)^2}[y^2] \quad
\expec[\big]{(X_0)^2}[y^2] \quad
\expec[\big]{(\hat{X}_0)^2}[y^2] \quad
\expec*{(\hat{X}_0)^2}[y^2] % wrong!
\]

\[
\int_{[0,1]}\bigl|\expec{x_{1}^{K_y} -x_1} - \expec{x_2^{K_y}-x_2}\bigr|
\]

\end{document}

enter image description here

Look in the manual of mathtools for the features of \DeclarePairedDelimiter.

1
  • @campa There is no space between a \mathop and a \mathopen.
    – egreg
    Commented Oct 17, 2022 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .