I'm struggling with a command that doesn't do what I expect. This is my code
\newcommand{\expectation}[2][]{\mathrm{E}_{#1}\left[#2\right]}
\begin{document}
I get $\expectation{A}{B}$, but I expect $\mathrm{E}_{A}\left[B\right]$\\
Optional arguments, I get $\expectation{B}$, and I expect $\mathrm{E}\left[B\right]$
\end{document}
This does not make a lot of sense to me. If I skip the newcommand and directly plug in what I want, everything looks fine. But the newcommand version with both inputs does something different. What am I missing?
(Even worse: I'm sure everything worked as intended yesterday. Today it doesn't anymore)
Edit: turns out this was due to a fixed bug in Texstudio. Optional arguments have to be called with brackets
\expectation[A]{B}
with square, not curly brackets around theA
. It is an optional argument. Curly braces are for mandatory arguments.\ifblank
frometoolbox
to test if the_{#1}
is even needed.