1

I want to write a tiny bit of code which for TeX which would make a function that takes inputs A and B and outputs

$
\mathbb{E}\left[A \middle| B\right].
$

Basically I want a "dynamic" \newcommand function. Is this possible? If so how can I do this?

2
  • How do I use it then in Tex? What would the format be (sorry if this is basic). I tried $\expt{1}{2}$ to test but this does not work?
    – ABIM
    Jan 30, 2017 at 19:44
  • 1
    @CSA: Just copy the definition into your preamble, as David Carlisle proposed and correct the typo(?) \mathBB to \mathbb. Since \ensuremath is used, you can omit the $...$
    – user31729
    Jan 30, 2017 at 20:07

3 Answers 3

2

You can very simply define such a command with \DeclarePairedDelimiterXPP from mathtools. This command has a starred version, which is equivalent to a pair of implicit \left…\right and a \middle, and a non-starred version which accepts an optional argument for fine tuning the size: \big, \Big,\bigg and \Bigg.

\documentclass{article}
\usepackage[utf8]{inputenc}%
 \usepackage{mathtools, nccmath}

\providecommand\given{}
\DeclarePairedDelimiterXPP\EV[1]{E}[]{}{
\renewcommand\given{\nonscript\;\delimsize\vert\nonscript\;:}
#1}

\begin{document}

\[ \EV*{Z\given\frac{X}{Y}}\quad \EV[\Bigg]{Z\given\frac{X}{Y}} \quad \EV[\Big]{Z\given\mfrac{X}{Y}}\]%

\end{document} 

enter image description here

3

enter image description here

\documentclass{article}

\usepackage{amsfonts}
\newcommand\expt[2]{\ensuremath{\mathbb{E}\left[#1\middle|#2‌​\right]}}

\begin{document}

 $\expt{P}{x} = \expt{Q}{y}$

\end{document}
1

Here's a solution that use's Knuth's own recommendations -- cf. p. 174 of the TeXbook -- for the horizontal spacing that should be used in expressions that containing vertical bars to denote conditioning events.

enter image description here

\documentclass{article}
\usepackage{amsfonts}   % for '\mathbb' macro
\usepackage{amsmath}    % for '\DeclareMathOperator' macro
\usepackage{mleftright} % for '\mleft' and '\mright' macros
\DeclareMathOperator{\E}{\mathbb{E}} % uncond'l expectation operator
\newcommand\coex[2]{\E\mleft[\,#1\;\middle|\;#2\,\mright]}
\begin{document}
$\coex{Y}{X}$, $\coex{W}{\dfrac{U}{V}}$
\end{document}

You must log in to answer this question.

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