2

Here's the minimal example

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amssymb} \newcommand{\bbE}[1][X]{\mathbb{E}\left[#1\right]}

\begin{document}

Working: $\bbE[XY]$

Broken: $\bbE[\bbE[X]] \bbE[\bbE[Y]]$

\end{document}

When I run this I get the following error:

Missing delimiter (. inserted).

Example

reuzed
  • 21
  • Use braces around the argument: \bbE[{\bbE[X]}]. But you don't need (and likely shouldn't) use \left...\right for single symbols like X and Y, so you could simplify your command to \newcommand{\bbE}{\mathbb{E}} – Phelype Oleinik Feb 10 '21 at 14:06
  • @PhelypeOleinik what is the reason not to use \left...\right for single symbols? when I actually use this I have expectations of sums and larger things – reuzed Feb 10 '21 at 17:42
  • @PhelypeOleinik also, braces around any combination of arguments didn't fix the issue – reuzed Feb 10 '21 at 17:48
  • @campa thanks, I think this explains why I'm having the issue – reuzed Feb 10 '21 at 17:50
  • 2
    @reuzed It gets spacing wrong around the parentheses. Compare $x(1+2)x$ with $x\left(1+2\right)x$. Also \left...\right encloses the subformula in a box, so line breaking can't happen if needed. See here. Regardless, that's best practices but not what's causing you the error. The error is due to how LaTeX grabs the optional argument, and adding braces like in \bbE[{\bbE[X]}] does solve the problem (which is what the post campa linked says) – Phelype Oleinik Feb 10 '21 at 17:54
  • 2
    Easy fix: replace your command definition by \NewDocumentCommand\bbE{O{X}}{\mathbb{E}\left[#1\right]}. Commands defined with \NewDocumentCommand auto-match the delimiters, so you can nest them – Phelype Oleinik Feb 10 '21 at 18:05

2 Answers2

1

Another fix:

While latex is looking for the optional argument of the outer command stop (]) it finds the square bracket of the second (inner) command and stops there... This is happening because it works with similar way of my first definitions (see \def commands) that contains something like \@ifnextchar[ and ends at the first ] ... By defining the \@lp (left square bracket) and \@rp commands it will not supposed as closed parenthesis in the first read from latex but will expand to it after that and this will solve the confusion with the optional argument of the command.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amssymb} \makeatletter \def@lp{[} \def@rp{]} \def\bbE{% @ifnextchar[% {@bbeWith}% {@bbeWithout}% } \def@bbeWith[#1]{\mathbb{E}\left@lp#1\right@rp} \def@bbeWithout{\mathbb{E}\left@lp X\right@rp}

%or %\renewcommand{\bbE}[1][X]{\mathbb{E}\left@lp#1\right@rp} \makeatother

\begin{document}

Working: $\bbE[XY]$

Broken: $\bbE[\bbE[X]] \bbE[\bbE[Y]]$

\end{document}

koleygr
  • 20,105
1

You can use the features of xparse that is good in these cases and doesn't require further tricks.

\documentclass{article}
\usepackage{amssymb}

\NewDocumentCommand{\expec}{O{X}}{\mathbb{E}[#1]} %\newcommand{\bbE}[1][X]{\mathbb{E}\left[#1\right]}

\begin{document}

Working: $\expec[XY]$

Working: $\expec[\expec[X]]$ and $\expec[\expec[Y]]$

\end{document}

You may need to add \usepackage{xparse} if you're not running an up-to-date LaTeX distribution.

I used a more semantic name, feel free to use the name of your choice.

enter image description here

egreg
  • 1,121,712