94

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.

David Carlisle
  • 757,742
uli
  • 4,315
  • 1
    I tend to use square brackets when this is causing a problem. In general I use left and right most of the time and let the equations be as wide as they need to be. Is there a particular reason that it bothers you? – qubyte Nov 24 '11 at 12:18
  • 1
    @Mark In words like Schifffahrt a sequence of the same letter looks strange. I think it is the same with a sequence of same size parentheses (((. Of course I could use [{( but I find the variation in the size nicer to look at. – uli Nov 24 '11 at 12:27
  • I see now. I think most cases for me naturally go away when I break equations over lines. LaTeX probably already has the best default behaviour, but maybe it is worth exploring. – qubyte Nov 24 '11 at 12:34
  • 1
    Journals tend to recommend mixing the parentheses to save some space. Also one should be a bit careful about the sizes. Sometimes there is no need to use huge parentheses around a long equation, if the large part is in the middle and it is not large (above/below) at the ends. I tend to recommend users to scale such that it is clear what is being fenced in, but also such that the parentheses do not dominate the expression. – daleif Nov 24 '11 at 12:48
  • Definitely, this would require some kind of two-pass system, since when we encounter the first opening bracket, we don't know yet whether there'll be more. But this does not necessarily mean two LaTeX passes and messing with the aux file, it would be enough to analyze the token list constituting the equation beforehand. Maybe l3regexp would help? Notice that in general this seems to be one of these problems where computers cannot solve all possible cases (because of the complicated aesthetical issues involved). – mbork Nov 24 '11 at 22:12
  • 1
    @mbork: Maybe I don't quite understand what you mean, but isn't the \left...\right mechanism from the TeX core already a two-pass system? At the opening \left, TeX doesn't know yet what size the delimiter should have. Moreover, nesting \left...\right also works. – Hendrik Vogt Jun 25 '12 at 07:42
  • Regarding the extra space around \left...\right see Spacing around \left and \right. – Hendrik Vogt Jun 25 '12 at 07:53
  • @uli: what is the font used in the image in the question? It looks much nicer than the standard TeX font. – Mohan Dec 31 '12 at 11:38
  • @Mohan: It is the wonderful work of Johannes Küster, who created Minion Math as a companion to Robert Slimbach’s Minion. – uli Dec 31 '12 at 15:56
  • I'm sorry for a foolish question, but, before your first table, you say "The middle column [h]as a \left \right pair at every parentheses/brace/bracket and the rightmost column shows my preferred version"; and yet the middle column of the $(\pi - \arccos(y/r))$ row has only one \left \right pair for two parentheses, whereas the rightmost column has two \left \right pairs. Is this what was intended? – LSpice Dec 28 '14 at 15:52
  • 1
    @l-spice Your question is not foolish, indeed there is a '\left \right'-pair missing. – uli Jan 05 '15 at 07:25

5 Answers5

61

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.

David Carlisle
  • 757,742
Aditya
  • 62,301
  • @Herbert, Aditya if I could only accept both answers. But as I have to choose... I love the nath solution for its easier to read latex source, nothing is cluttered with \left and \right. – uli Dec 03 '11 at 20:59
  • 1
    Does it work in the beamer class? (asking because it crashed my beamer, could be an incompatibility with another package though) Thanks. – PatrickT Feb 20 '22 at 22:20
38

Use it this way with a small negative space between two large parentheses:

\documentclass{scrartcl}
\usepackage{amsmath}
\begin{document}
\delimitershortfall=-1pt
\begin{align*}
\left(\pi-\arccos \left(\frac{y}{r}\right)\right)\\
\left(\pi-\arccos \left(\frac{y}{r}\right)\!\right)
\end{align*}
\end{document}

enter image description here

here is an idea how it can be done with a double (( and/or )). It is also possible to use \left((...( and a \right))...). only the number of \left and \right parentheses must correspond to each other

\documentclass{scrartcl}
\usepackage{amsmath}

\delimitershortfall=-1pt
\let\Right\right
\let\Left\left
\makeatletter
\def\right#1{\Right#1\@ifnextchar){\!\right}{}}
\def\left#1{\Left#1\@ifnextchar({\!\left}{}}
\makeatother
\begin{document}
\begin{align*}
\left(\pi-\arccos \left(\frac{y}{r}\right)\right)\\
\left(\pi-\arccos \left(\frac{y}{r}\right))\\
\left((\pi-\arccos \left(\frac{y}{r}\right)))\\
\left(\left(x + \frac12\right)\arccos(\pi)\right)\\
\left((x + \frac12\right)\arccos(\pi)\right)
\end{align*}
\end{document}

enter image description here

Moriambar
  • 11,466
7

You can do this without using external packages, which can simplify things a bit if the solution needs to work e.g. in KaTeX. You can define your own "growing parenthesis" command by using invisibe subscripts around the parenthesis' contents:

\newcommand{\paren}[1]{\mathopen{}\left( {#1}_{{}_{}}\,\negthickspace\right)\mathclose{}}
\newcommand{\bracket}[1]{\mathopen{}\left[ {#1}_{{}_{}}\,\negthickspace\right]\mathclose{}}

Which gets you exactly the look you want when used at the right places (middle column was modified):

\begin{aligned}
&1-(1-F(x))^n&&1-\paren{1-F(x)}^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}\bracket{\min\left\{X_1,X_2\right\}}&&\mathbb{E}\bigl[\min\left\{X_1,X_2\right\}\bigr]\\
&\left(\pi-\arccos(\frac{y}{r})\right)&&\left(\pi-\arccos\left(\frac{y}{r}\right)\right)&&\left(\pi-\arccos \left(\frac{y}{r}\right)\right)
\end{aligned}
\[ \paren{\paren{\paren{\paren{\paren{}}}}} \]
\[ \bracket{\bracket{\bracket{\bracket{\bracket{}}}}} \]

aligned

Szabolcs
  • 171
4

My current solution is to use the mathtools macro \DeclarePairedDelimiter

e.g. \DeclarePairedDelimiter\brac{(}{)} allows me to use \brac*{stuff} for autoexpanding brackets. This probably is preferable to nath with extremely large documents (the readme for nath suggests that it takes up to 3 times the compile time!)

Of course it's somewhat annoying to have to star the command every time, so you can swap the behavior, detailed here: Easily change behavior of \DeclarePairedDelimiter

Also, nath is notorious for being incompatable with almost all math related packages, so it's not actually useful at all.

2

What I do is declaring a parenthis-function that uses \vphantom with \raisebox to add extra height to the outer delimiter.

The solution is not pretty:

  • I use \mathop because else \vphantom does not work and then add negative space to counter the extra space that \mathop adds.

  • Also inside \raisebox you need to use \( \) to get into mathmode and \displaystyle to get the right virtual height

But it works for me and you can still tweak \delimtershortfall and \delimiterfactor to your liking (if you then also adjust the raise in \raisebox). Also you can change the value in \raisebox to adjust the growth of the delimiters.

\documentclass[12pt,a4paper]{article}
\usepackage{mleftright}

\global\delimitershortfall=5.5pt \global\delimiterfactor=880 \renewcommand{\left}{\mleft} \renewcommand{\right}{\mright} \newcommand{\braced}[1] { \left( #1 \right) \mskip-1.5mu \vphantom{ \mathop{ \raisebox{3pt}{ (\left(\displaystyle #1 \right)) } } } }

\begin{document} [ \braced{ x + \braced{\braced{ y }} } ] \end{document}

Example

S. Kohl
  • 111
  • 1
  • 5
  • is it bad behavior that i postet this exact same answer under another question that i regard a pulicate of this one? – S. Kohl Jun 28 '20 at 07:31