0

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:

\newcommand{\expcti}[2]{\operatorname{\mathbb{E}}_{#2}\expectiarg#1}
\DeclarePairedDelimiterX{\expectiarg}[1]{[}{]}{%
  \ifnum\currentgrouptype=16 \else\begingroup\fi
  \activatebar#1
  \ifnum\currentgrouptype=16 \else\endgroup\fi
}


\newcommand{\innermid}{\nonscript\;\delimsize\vert\nonscript\;}
\newcommand{\activatebar}{%
  \begingroup\lccode`\~=`\|
  \lowercase{\endgroup\let~}\innermid 
  \mathcode`|=\string"8000
}

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

Edit:

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}$$

6
  • 1
    Could you post a sketch of what you're trying to achieve? As is, it's not very clear.
    – Bernard
    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
    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
    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
    Jul 16, 2019 at 12:25
  • I think I've provided a simpler code in my answer, with the help of \DeclarePairedDelimiterXPP.
    – Bernard
    Jul 16, 2019 at 13:06

1 Answer 1

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 {|} :

\documentclass{article}
\usepackage{mathtools, amssymb, nccmath}
\usepackage{xparse}

\DeclarePairedDelimiterXPP\Expect[2]{\mathbb{E}_{#1}}[]{}{\setargs{#2}}%
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{|}}m}
{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1}{\nonscript\,#1\nonscript\;\delimsize\vert\nonscript\:\allowbreak #2\nonscript\,}}
\DeclarePairedDelimiterXPP\expectaux[3]{\mathbb{E}_{#1}}[]{}{#2\nonscript\:\delimsize\vert\nonscript\:#3}%

\begin{document}

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

\end{document} 

enter image description here

You must log in to answer this question.

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