These are a few very common mistakes when using delimiters:
Don't use | or
\vert for absolute values!
Instead use \lvert ... \rvert from the
amsmath
package. Even better, define a command \abs
which uses these, for example using
\DeclarePairedDelimiter. Compare:
\[
b+|-a|
\qquad
b+\vert -a\vert
\qquad
b+\lvert -a\rvert
\] | and \vert look so bad? \[b+x-ay \qquad b+(-a)\]
Note that these two expressions differ only in that x
and y in the first one have been replaced by ( and
)
in the second one. However, the spacing is very different: In the second expression the minus
sign is much closer to the a and to the opening parenthesis than it is to the
a
and the x in the first one. Essentially, TeX recognizes that the minus sign is a binary
operator in the first expression and a unary operator in the second and adjusts the spacing
accordingly.
How does TeX know when to do this? Every symbol in a mathematical expression in TeX has a class, the letters a, b,
x, and y
are classfied as ordinary symbols, whereas ( is classified as an
opening
symbol and ) as a closing symbol. TeX uses the classes of the
surrounding symbols to determine the class of the -. If it is surrounded by
ordinary symbols, it is classified as a
binary operator, otherwise as a unary operator.
The issue with using | as a delimiter is that TeX has no way of knowing
whether this symbol is intended as an opening or closing delimiter so |
is simply classfied as an ordinary symbol by TeX. This is why
$b+|-a|$ results in the symbols having the same spacing as for
$b+x-ay$ instead of the correct spacing as in $b+(-a)$.
Essentially, TeX wrongly assumes that the minus sign in $b+|-a|$ is used as a
binary operator (subtracting a from |). The same issue arises
whenever the same symbol is used for both the opening and closing delimiter, i.e., with
\vert
or \Vert and \| for norms.
To fix this, you could explicitly tell TeX to consider | as either an opening
or closing symbol, using
\mathopen|...\mathclose|, or, much preferably, just use
\lvert and \rvert from the
amsmath
package (which are essentially defined that way).
Don't use ||,
\|, or \Vert for norms!
Instead use \lVert ... \rVert. Even better, define a command \norm
which uses these, for example using
\DeclarePairedDelimiter. Compare:
\[
u+||-v||
\qquad
u+\|-v\|
\qquad
u+\Vert -v\Vert
\qquad
u+\lVert -v\rVert
\] \| and \Vert look so bad? Don't use \big, \Big etc. to get bigger delimiters!
Instead use, e.g., \bigl( ... \bigr). Compare:
\[
b-\bigg(\frac a2\bigg)
\qquad\qquad
b-\biggl(\frac a2\biggr)
\] \big, \Big, etc. look so bad? | and \vert
end up giving wrong spacing in certain situations because they are considered
ordinary symbols, rather than opening or closing
symbols. A similar thing happens with \big, \Big, etc. These
commands tell TeX to make the following symbol bigger but additionally
turn it into an ordinary symbol. This happens even for symbols like
( and ) which are considered opening resp. closing symbols when used
on their own. Don't use \left and \right!
\bigl/\bigr,
\Bigl/\Bigr and so on,\mleft and
\mright
commands from the
mleftright package, or\DeclarePairedDelimiter from the
mathtools package.Compare:
\[
\sin\left(x\right).
\qquad\qquad
\sin(x).
\] \left and \right look so bad? As explained above,
| and \vert
end up giving wrong spacing in certain situations because they are considered
ordinary symbols, rather than opening or closing
symbols. A similar thing happens with \left and \right. These
commands wrap the entire expression as an inner formula, so
$\sin\left(x\right)$ ends up being equivalent to
$\sin\mathinner{(x)}$. Unfortunately, the spacing rules of TeX
introduce additional space, for example, between operators like \sin
and inner formulas or between inner formulas and punctuation.
This issue can be corrected by hand by introducing additional empty
opening and closing symbols as in \mathopen{}\left(...\right)\mathclose{}.
The mleftright package and the starred commands defined using the
\DeclarePairedDelimiter command essentially automate this solution.
These are the best ways to use delimiters:
Use the \DeclarePairedDelimiter command from the
mathtools package!
\big,
\Big, \bigg, or
\Bigg
as an optional argument, which produces delimiters of the corresponding sizes (equivalent to using
\bigl/\bigr and so on). Finally, there is a starred variant, which
produces auto-sized delimiters (equivalent to using \mleft/\mright
from the
mleftright package).
\DeclarePairedDelimiter{\br}{[}{]}
...
\[
\br{x+y}
\qquad
\br[\big]{\tfrac12}
\qquad
\br*{\sum_{i=1}^n a_i}
\]For convenience, you may want to define some shorter commands for the manually sized options:
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\newcommand{\bnorm}[1]{\norm[\big]{#1}}
\newcommand{\bbnorm}[1]{\norm[\Big]{#1}}
\newcommand{\bbbnorm}[1]{\norm[\bigg]{#1}}
\newcommand{\bbbbnorm}[1]{\norm[\Bigg]{#1}}bs. Use the commands \bigl/\bigr etc., if you
have to!
\DeclarePairedDelimiter are sufficient in most cases
and give the same results as using the manual sizing commands
\bigl/\bigr and so on. However, one occasion where they do not work is
when there is a line break in the middle of the enclosed expression. In this case, use the manual
sizing commands directly.Here is code for defining all common delimiters:
\usepackage{mathtools}
\DeclarePairedDelimiter{\pr}{(}{)}
\newcommand{\bpr}[1]{\pr[\big]{#1}}
\newcommand{\bbpr}[1]{\pr[\Big]{#1}}
\newcommand{\bbbpr}[1]{\pr[\bigg]{#1}}
\newcommand{\bbbbpr}[1]{\pr[\Bigg]{#1}}
\DeclarePairedDelimiter{\br}{[}{]}
\newcommand{\bbr}[1]{\br[\big]{#1}}
\newcommand{\bbbr}[1]{\br[\Big]{#1}}
\newcommand{\bbbbr}[1]{\br[\bigg]{#1}}
\newcommand{\bbbbbr}[1]{\br[\Bigg]{#1}}
\DeclarePairedDelimiter{\cu}{\{}{\}}
\newcommand{\bcu}[1]{\cu[\big]{#1}}
\newcommand{\bbcu}[1]{\cu[\Big]{#1}}
\newcommand{\bbbcu}[1]{\cu[\bigg]{#1}}
\newcommand{\bbbbcu}[1]{\cu[\Bigg]{#1}}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\newcommand{\babs}[1]{\abs[\big]{#1}}
\newcommand{\bbabs}[1]{\abs[\Big]{#1}}
\newcommand{\bbbabs}[1]{\abs[\bigg]{#1}}
\newcommand{\bbbbabs}[1]{\abs[\Bigg]{#1}}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\newcommand{\bnorm}[1]{\norm[\big]{#1}}
\newcommand{\bbnorm}[1]{\norm[\Big]{#1}}
\newcommand{\bbbnorm}[1]{\norm[\bigg]{#1}}
\newcommand{\bbbbnorm}[1]{\norm[\Bigg]{#1}}
\DeclarePairedDelimiter{\ang}{\langle}{\rangle}
\newcommand{\bang}[1]{\ang[\big]{#1}}
\newcommand{\bbang}[1]{\ang[\Big]{#1}}
\newcommand{\bbbang}[1]{\ang[\bigg]{#1}}
\newcommand{\bbbbang}[1]{\ang[\Bigg]{#1}}\pr (for parentheses),\br (for brackets),\cu (for curly braces),\abs (for absolute values),\norm (for norms),\ang (for angled brackets)\bpr, \bbpr, \bbbpr, \bbbbpr, and \pr*
for successively bigger and autosized variants respectively:
\[
\pr{x},\,
\bpr{\tfrac12},\,
\bbpr{\bigcup\{A_i \colon i\in\N\}},\,
\bbbpr{\frac12},\,
\bbbbpr{\sum_{i=1}^n a_i},\,
\pr*{\begin{matrix}1&2\\3&4\\5&6\end{matrix}}
\]Note that if you introduce notation such as
\norm, \floor,
or \ceil via a definition in your document, you will probably
want to add a \cfadd to the declaration of the
delimiter, so you can automatically
reference the definition wherever the notation is used. E.g., for the norm
the declaration might look like this:
\DeclarePairedDelimiter{\norm}{\lVert\cfadd{def:norm}}{\rVert}
\newcommand{\bnorm}[1]{\norm[\big]{#1}}
\newcommand{\bbnorm}[1]{\norm[\Big]{#1}}
\newcommand{\bbbnorm}[1]{\norm[\bigg]{#1}}
\newcommand{\bbbbnorm}[1]{\norm[\Bigg]{#1}}\cfadd to the label of the
corresponding definition in your document. Here are some more specialized delimiters:
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\newcommand{\bceil}[1]{\ceil[\big]{#1}}
\newcommand{\bbceil}[1]{\ceil[\Big]{#1}}
\newcommand{\bbbceil}[1]{\ceil[\bigg]{#1}}
\newcommand{\bbbbceil}[1]{\ceil[\Bigg]{#1}}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\newcommand{\bfloor}[1]{\floor[\big]{#1}}
\newcommand{\bbfloor}[1]{\floor[\Big]{#1}}
\newcommand{\bbbfloor}[1]{\floor[\bigg]{#1}}
\newcommand{\bbbbfloor}[1]{\floor[\Bigg]{#1}}
\DeclarePairedDelimiter{\rnd}{\lceil}{\rfloor}
\newcommand{\brnd}[1]{\rnd[\big]{#1}}
\newcommand{\bbrnd}[1]{\rnd[\Big]{#1}}
\newcommand{\bbbrnd}[1]{\rnd[\bigg]{#1}}
\newcommand{\bbbbrnd}[1]{\rnd[\Bigg]{#1}}
\DeclarePairedDelimiterXPP\pnorm[2]{}\lVert\rVert{_{#1}}{#2}
\newcommand{\bpnorm}[2]{\pnorm[\big]{#1}{#2}}
\newcommand{\bbpnorm}[2]{\pnorm[\Big]{#1}{#2}}
\newcommand{\bbbpnorm}[2]{\pnorm[\bigg]{#1}{#2}}
\newcommand{\bbbbpnorm}[2]{\pnorm[\Bigg]{#1}{#2}}
\DeclareFontFamily{U}{matha}{\hyphenchar\font45}
\DeclareFontShape{U}{matha}{m}{n}{
<-6> matha5 <6-7> matha6 <7-8> matha7
<8-9> matha8 <9-10> matha9
<10-12> matha10 <12-> matha12
}{}
\DeclareSymbolFont{matha}{U}{matha}{m}{n}
\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{
<-6> mathx5 <6-7> mathx6 <7-8> mathx7
<8-9> mathx8 <9-10> mathx9
<10-12> mathx10 <12-> mathx12
}{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareMathDelimiter{\vvvert}{0}{matha}{"7E}{mathx}{"17}
\DeclarePairedDelimiter{\tnorm}{\vvvert}{\vvvert}
\newcommand{\btnorm}[1]{\tnorm[\big]{#1}}
\newcommand{\bbtnorm}[1]{\tnorm[\Big]{#1}}
\newcommand{\bbbtnorm}[1]{\tnorm[\bigg]{#1}}
\newcommand{\bbbbtnorm}[1]{\tnorm[\Bigg]{#1}}
\DeclareMathDelimiter{\ldbr}{0}{matha}{"76}{mathx}{"30}
\DeclareMathDelimiter{\rdbr}{0}{matha}{"77}{mathx}{"38}
\DeclarePairedDelimiter{\dbr}{\ldbr}{\rdbr}
\newcommand{\bdbr}[1]{\dbr[\big]{#1}}
\newcommand{\bbdbr}[1]{\dbr[\Big]{#1}}
\newcommand{\bbbdbr}[1]{\dbr[\bigg]{#1}}
\newcommand{\bbbbdbr}[1]{\dbr[\Bigg]{#1}}
\newcommand{\dang}[1]{\langle\mkern-3mu\langle #1\rangle\mkern-3mu\rangle}
\newcommand{\bdang}[1]{\bigl\langle\mkern-4mu\bigl\langle #1\bigr\rangle\mkern-4mu\bigr\rangle}
\newcommand{\bbdang}[1]{\Bigl\langle\mkern-6mu\Bigl\langle #1\Bigr\rangle\mkern-6mu\Bigr\rangle}
\newcommand{\bbbdang}[1]{\biggl\langle\mkern-9mu\biggl\langle #1\biggr\rangle\mkern-9mu\biggr\rangle}
\newcommand{\bbbbdang}[1]{\Biggl\langle\mkern-10mu\Biggl\langle #1\Biggr\rangle\mkern-10mu\Biggr\rangle}
\providecommand\cond{}
\newcommand{\conditionSymbol}[1][]{#1\vert\mathopen{}}
\DeclarePairedDelimiterX\expbr[1]{[}{]}{%
\renewcommand\cond{\conditionSymbol[\delimsize]}
#1
}
\NewDocumentCommand{\E}{s m}{\mathbb E\IfBlankTF{#1}{\IfBooleanTF{#1}{\mkern-2mu\expbr*}\expbr{#2}}}
\newcommand{\bE}[1]{\mathbb E\mkern-1mu\expbr[\big]{#1}}
\newcommand{\bbE}[1]{\mathbb E\mkern-1mu\expbr[\Big]{#1}}
\newcommand{\bbbE}[1]{\mathbb E\mkern-2mu\expbr[\bigg]{#1}}
\newcommand{\bbbbE}[1]{\mathbb E\mkern-2mu\expbr[\Bigg]{#1}}
...
\[
\ceil{x},\;
\bfloor{\tfrac12},\;
\bbrnd{\frac ab},\;
\pnorm2{u+x},\;
\tnorm{A},\;
\bdang{{\textstyle \sum_{i=1}^n u_i},v},\;
\E*{\sum_{i=1}^n X_i \cond Y}
\]