I was trying to define a new command for an expectation which scales the vertical dash (in case of conditional expectation) and parenthesis to the argument, respectively. Additionally, I want to have a parameter which specifies which expectation I am talking about, i.e. $\mathbb{E}_{index}$ where index an arbitrary value.

I was following the instruction from this post [vertical bar for absolute value and conditional expectation. Unfortunately, the index is now the argument of the expectation and the other way around. When simply exchanging #1 with #2, I get a missing number error.

The code I am using:

  \ifnum\currentgrouptype=16 \else\begingroup\fi
  \ifnum\currentgrouptype=16 \else\endgroup\fi


I am happy about any help here. Thanks a lot!


What I am trying to achieve is a command which automatically scales the vertical bar in a conditional expectation with the parentheses, i.e. left what I get when simply using \left[ and \right] and right what I actually want:

enter image description here

I'd like to achieve the formula on the r.h.s. by typing $$\expcti*{\frac{1}{2} | Y}{F}$$

  • 1
    Could you post a sketch of what you're trying to achieve? As is, it's not very clear.
    – Bernard
    Commented Jul 16, 2019 at 11:31
  • @Bernahrd Thanks for pointing out, I just added an example of what I am trying to achieve. Is it clear now?
    – Gabriel
    Commented Jul 16, 2019 at 11:49
  • I see. I think one of the variants of \DeclarePairedDelimiter can do it. Unrelated: the formula would look nicer with \mfrac{1}{2} (it is the medium-size fraction command from nccmath).
    – Bernard
    Commented Jul 16, 2019 at 11:59
  • I think the problem lies in the first line of code. the \expcti command should not be affected by the index. I just want the operator to use the second argument as an index. Thanks for the side note, did not know about this.
    – Gabriel
    Commented Jul 16, 2019 at 12:25
  • I think I've provided a simpler code in my answer, with the help of \DeclarePairedDelimiterXPP.
    – Bernard
    Commented Jul 16, 2019 at 13:06

1 Answer 1


Here is a solution, using two arguments: the index and the random variables, separated by a |. If you want no index, use an empty argument. If tere is no conditional part,, i.e. if you type no |, no vertical rule will be printed. If you need to use one in your formula which shouldn't be interpreted as a separator, use {|} :

\usepackage{mathtools, amssymb, nccmath}

{\IfNoValueTF{#2}{#1}{\nonscript\,#1\nonscript\;\delimsize\vert\nonscript\:\allowbreak #2\nonscript\,}}


\[ \Expect*{i}{\mfrac{1}{2}X | Y} \quad\Expect[\bigg]{k}{\mfrac{1}{2}X} \]%


enter image description here

You must log in to answer this question.

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