2

Consider the following code

\documentclass{amsbook}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\[
E(X) = \delta \left[ \frac{1}{-\delta} x e^{-\delta x} \left| \right|_0^\infty \ldots  \right]
\]
\end{document}

It produces the following, which looks odd:

enter image description here

Reversing the order of the middle \left| and \right| solves the problem, but only partially (note the differently-sized final delimiter):

\[
E(X) = \delta \left[ \frac{1}{-\delta} x e^{-\delta x}  \right|_0^\infty \left| \ldots  \right]
\]

enter image description here

Why is that? Is there an appropriate way to do this?

1

3 Answers 3

2

For a one-off formula you need nothing special, just use \Bigl and friends.

For several appearances,

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}

\ExplSyntaxOn

\NewDocumentCommand{\brackeval}{O{}mmmm}
 {% #1 = size instruction
  % #2 = before the evaluation bar
  % #3 = lower bound
  % #4 = upper bound
  % #5 = after the evaluation bar
  \tonyk_brackeval:nnnnn { #1 } { #2 } { #3 } { #4 } { #5 }
 }

\cs_new_protected:Nn \tonyk_brackeval:nnnnn
 {
  \tl_use:c { c__tonyk_brackeval_open_#1_tl } [
  #2 \vphantom{#5}
  \tl_use:c { c__tonyk_brackeval_middle_#1_tl } |\sb{#3}\sp{#4}
  \;
  #5 \vphantom{#2}
  \tl_use:c { c__tonyk_brackeval_close_#1_tl } ]
 }

% case of no optional argument
\tl_const:cn { c__tonyk_brackeval_open__tl } { }
\tl_const:cn { c__tonyk_brackeval_middle__tl } { }
\tl_const:cn { c__tonyk_brackeval_close__tl } { }
% case of *
\tl_const:cn { c__tonyk_brackeval_open_*_tl } { \mleft.\kern-\nulldelimiterspace\mleft }
\tl_const:cn { c__tonyk_brackeval_middle_*_tl } { \mright }
\tl_const:cn { c__tonyk_brackeval_close_*_tl } { \mright }
% other cases
\clist_map_inline:nn { big, Big, bigg, Bigg }
 {
  \tl_const:cx { c__tonyk_brackeval_open_#1_tl } { \exp_not:c { #1l } }
  \tl_const:cx { c__tonyk_brackeval_middle_#1_tl } { \exp_not:c { #1 } }
  \tl_const:cx { c__tonyk_brackeval_close_#1_tl } { \exp_not:c { #1r } }
 }

\ExplSyntaxOff

\begin{document}

\begin{gather}
E(X)=\delta\brackeval{x}{a}{b}{\dots} \\
E(X)=\delta\brackeval[big]{x}{a}{b}{\dots} \\
E(X)=\delta\brackeval[Big]{-\frac{1}{\delta}xe^{-\delta x}}{0}{\infty}{\dots} \\
E(X)=\delta\brackeval[*]{-\frac{1}{\delta}xe^{-\delta x}}{0}{\infty}{\dots}
\end{gather}

\end{document}

The effect is similar to Davislor's answer, but it allows also automatic sizing (the syntax is slightly different, though). The trick for automatic sizing is to add a \left. at the beginning, so for the bar we can use \right that accepts limits. The sizes will be the same thanks to the phantoms.

enter image description here

5

Although you could normally use \left[ ... \middle| ... \right] or \left\lbrack ... \middle\vert ... \right\rbrack, that breaks on the limits here.

So, a good alternative is \DeclarePairedDelimiterX from \mathtools. (See section 3.6 of the manual.) This allows you to divide the inside of the expression into multiple segments and use \delimsize for the middle delimiter. You call it with a size argument, such as [\big] or [\bigg].

\documentclass{amsbook}
\usepackage{amsmath, amssymb}
\usepackage{mathtools}

\DeclarePairedDelimiterX{\expect}[4]{\lbrace}{\rbrace}{#1 \delimsize\vert_{#2}^{#3} #4}

\begin{document}
\[
E(X) = \delta \expect[\bigg]{\frac{1}{-\delta} x e^{-\delta x}}{0}{\infty}{\ldots}
\]
\end{document}

Computer Modern sample

You’d normally be able to use a starred version of the macro to scale automatically, such as \expect*, but if you do that here, the limits will break.

Eta: And I just noticed I wrote \lbrace and \rbrace instead of \lbrack and \rbrack. Whoops!

1
  • Excellent.. +1.
    – Sebastiano
    Feb 8, 2023 at 8:59
1

I have used \Bigl and \Bigr delimiters than \left and \right to have a correct size with the whole equation.

\documentclass{amsbook}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\[
E(X) = \delta \Bigl[ \frac{1}{-\delta} x e^{-\delta x}  \Bigl|_0^\infty \cdots  \Bigr]
\]
\end{document}

enter image description here

With a large size \Biggl and \Biggr:

\documentclass{amsbook}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\[
E(X) = \delta \Biggl[ \frac{1}{-\delta} x e^{-\delta x}  \Biggl|_0^\infty \cdots  \Biggr]
\]
\end{document}

enter image description here

3
  • 1
    I thought TeX sized things correctly automatically, that was why I was surprised by the result of my first try. Good to know about \Big and \Bigr. Handy too that there is no ``matching'' required -- saves on key strokes.
    – TonyK
    Feb 7, 2023 at 20:34
  • 1
    To all those who responded thank you. So many amazing answers. I confess that I"m confused that I'm still getting answers because I understood this post had been deleted by Mensch and Werner. I assume I can still see it because it's my post, but I assume no one else can. Certain aspects of this Exchange leave me scratching my head.
    – TonyK
    Feb 9, 2023 at 17:34
  • @TonyK Don't worry. This is the LaTeX :-)
    – Sebastiano
    Feb 9, 2023 at 17:36

You must log in to answer this question.

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