[Tex/LaTex] automatic size adjustment for nested parentheses

delimitersmath-mode

As I prefer outer parentheses to grow larger in nested expressions, I happen to insert \bigl and \bigr and its larger cousins a lot. I have always wondered whether there is a way to do this automagically.

Inserting \left and \right prophylacticly before all opening and closing parentheses and hoping that it sorts things out doesn't do the trick in all cases as seen below.

The left column is without any size changing commands. The middle column as a \left \right pair at every parentheses/brace/bracket and the rightmost column shows my preferred version.

example

As seen in the middle column of the second and third line the \left and \right pair sometimes makes the outer parentheses larger, on the cost of an extra horizontal space between \Pr and (. The extra space is fine in case of really big opening parentheses but here I'd prefer the regular spacing.

\documentclass{scrartcl}
\usepackage{amsmath}
\usepackage{amsfonts}
\begin{document}
\begin{align*}
&1-(1-F(x))^n&&1-\left(1-F(x)\right)^n&&1-\bigl(1-F(x)\bigr)^n\\
&\Pr(X_{(1)}\le x)&&\Pr\left(X_{(1)}\le x\right)&&\Pr\bigl(X_{(1)}\le x\bigr)\\
&\mathbb{E}[\min\{X_1,X_2\}]&&\mathbb{E}\left[\min\left\{X_1,X_2\right\}\right]&&\mathbb{E}\bigl[\min\left\{X_1,X_2\right\}\bigr]\\
&\left(\pi-\arccos(\frac{y}{r})\right)&&(\pi-\arccos\left(\frac{y}{r}\right))&&\left(\pi-\arccos \left(\frac{y}{r}\right)\right)
\end{align*}
\end{document}

As mathematical expressions can be arbitrarly complex there is probably no general way to do this. But I’m not asking for a solution of the general case. A partial solution that works in the simple cases shown above would be a big help. The simple rule could be, that parentheses never shrink below the size of an inner pair. Of course you can immediately think of extensions like a \mid in a conditional expectation that grows with the size of the expectation’s brackets.

What is your preference regarding the size of nested parentheses? Is there a better way to archieve your preferred style despite inserting \bigl \bigr manually?


EDIT:

A colleague pointed me to section 8.3 of Herbert Voß’ mathmode document where size problems with parentheses are solved by playing with two TeX parameters within a group around the expression in question. This led naturally to appendix G of the TeXbook where the mechanics of \delimitershortfall and \delimiterfactor are explained.

\delimitershortfall specifies the maximum space not covered by a delimiter (default 5pt)
and
\delimiterfactor is the ratio for variable delimiters, times 1000 (default 901).

I used them to implement the never shrink below a subformular size idea from above by setting the shortfall to 0pt and the ratio to 1.0.

example

While it works nicely in line one and three, in line two and four the parentheses now grow too much, and still there is the extra horizontal space introduced by \left.

Best Answer

The nath package with \delimgrowth=1 is very close to your preferred style.

\documentclass{article}
\usepackage{amssymb}
\usepackage{nath}
\delimgrowth=1

\begin{document}
  \begin{equation}
     1 - (1-F(x))^n \\
     \Pr(X_{(1)} \le x) \\
     \mathbb{E}[\min\{X_1, X_2\} ] \\
     ( \pi - \arccos (\frac {y}{r}) )
  \end{equation}
\end{document}

enter image description here

Read the nath guide for details, especially the part about incompatibility with amsmath.

Related Question