What is the difference between (
and \left(
in LaTeX? Sometimes, when the content is small, it does not seem to matter which pair I use.
What is the best practice when it comes to which parentheses use?
best practicesbracketsmath-mode
What is the difference between (
and \left(
in LaTeX? Sometimes, when the content is small, it does not seem to matter which pair I use.
What is the best practice when it comes to which parentheses use?
The \DeclarePairedDelimiter
command from mathtools is useful. For example defining an absolute value macro;
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
gives also a starred version with \left
and \right
\abs*{\frac{a}{b}}
and the unstarred version takes an optional argument that can be \big
, \Big
, etc
\abs[\Bigg]{\frac{a}{b}}
Under normal conditions, using \quad
inside or outside \text
gives the same result, as far as the amount of space is concerned. I'm disregarding what happens when \text
is used in a sub/superscript, for the moment.
TeX maintains two current fonts when it's typesetting a math formula: it remembers the font that was current before the formula started and also takes note of the current math family so that math symbols which are given type 7 are typeset using the font corresponding to the current family. Note that the current math family is only saved as a number; this number will be turned into a real font only when the formula is finished and is being converted into a horizontal list.
When an instruction such as \quad
is found in a formula, it expands to \hskip1em
as usual and the em is converted into a length by examining the fontdimen 6 parameter of the current text font, so the first that was mentioned above. If \mskip18mu
were used, the em of the font attached to math family 2 would be used; but it's not.
When \text{text}
is processed in math mode, TeX executes (in a group)
\mathchoice
{\textdef@\displaystyle\f@size{text}}%
{\textdef@\textstyle\f@size{\firstchoice@false #1}}%
{\textdef@\textstyle\sf@size{\firstchoice@false #1}}%
{\textdef@\textstyle \ssf@size{\firstchoice@false #1}}%
and we're interested in the first and second arguments to \mathchoice
. The \firstchoice@false
command is just to avoid executing multiple times commands such as \label
or \stepcounter
that can possibly sneak in the argument to \text
, but it's irrelevant for the problem at hand.
For the sake of simplicity, let's assume we're in \displaystyle
(but for \textstyle
it would be the same). TeX typesets four boxes, but uses only the one resulting from
\textdef@\displaystyle\f@size{text}
This is turned into
\hbox{{\everymath{\displaystyle}\let\f@size\f@size\selectfont text}}
We have to remember that \f@size
, \sf@size
and \ssf@size
expand to the font sizes LaTeX has determined to be the current overall font size (they are 10pt, 7pt, 5pt when the size is \normalsize
in a 10pt document; they are 12pt, 8pt, 6pt when the size is \normalsize
in a 12pt document or \large
in a 10pt document; and so on). In this case the \let
does nothing, but \selectfont
is really relevant!
What does it do? It resets the current text font based on the current values of \f@encoding
, \f@family
, \f@series
, \f@shape
, \f@size
and \f@baselineskip
. In this case, since \f@size
has not changed, the same font that was current outside \text
is carried over inside it.
As a conclusion, \quad
will have the same width inside \text
as it has outside it.
If \text
is used in a first level subscript, only \f@size
changes, becoming equivalent to what \sf@size
was before, so the \quad
will use the width corresponding to the current text font, but at \sf@size
.
Of course, using \quad
outside \text
has another difference: the argument to \text
is typeset in a box, so this glue cannot be used as a break point if the formula is inline. (This was remarked by David Carlisle.)
Why the example by Przemysław Scherwentke gives different quads? When we do
\font\meta=logo10 at 20pt \meta
we're overriding the NFSS infrastructure for font selection: \meta
will become the current text font, but the values of the \f@...
macros is not changed. Therefore for the quads in
\meta $a \quad\text{META}\quad b$
the value corresponding to \meta
are used (it is the current text font). Conversely, in
\meta $a \text{\quad META\quad} b$
the determination of 1em happens after \selectfont
, so the font is reset to what it was before the \meta
instruction, because the \f@...
macros are not updated when issuing \meta
. Indeed “META” is typeset in the current text font, not using the logo font.
Here's a test.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\font\meta=logo10 at 14.4pt
{\meta $a \quad\text{META}\quad b$}
{\meta $a \quad\mbox{META}\quad b$}
{\meta $a \text{\quad META\quad} b$}
{\meta $a \quad\text{META}\quad b$}
\newcommand{\goodmeta}{%
\fontencoding{U}%
\fontfamily{logo}%
\fontsize{14.4}{14.4}%
\selectfont}
{\goodmeta $a \quad\text{META}\quad b$}
{\goodmeta $a \quad\mbox{META}\quad b$}
{\goodmeta $a \text{\quad META\quad} b$}
{\goodmeta $a \quad\text{META}\quad b$}
\end{document}
One can see that in lines 1, 3 and 4 the word “META” is typeset in Computer Modern, while in line 2 it's in the logo
font (because \mbox
uses the current font without issuing any \selectfont
command).
In the other cases, where \goodmeta
is used, the results are identical.
Best Answer
There are in fact four important differences:
\left( ... \right)
etc. scales according to the height and depth of its contents. This scaling is "dumb" in the sense that it will always take the full height and depth into account (how much of the expression is covered is controlled by\delimitershortfall
and\delimiterfactor
): for example, in\left( \rule{1cm}{1cm} \right)
, the parentheses reach far below the square. A more relevant example is\left( \sum_a^b \right)
where the parentheses also cover the sum limits. The simple delimiters(
and)
and also the manually-sized delimiters\big(
etc. don't scale.\left ... \right
forms a group: if you say\newlength\mylength \[ \left( \mylength=1cm \right) \the\mylength \]
you get0.0pt
because the value was reset. More importantly, you cannot have line breaks inside\left ... \right
groups, neither manual nor automatic ones, without special trickery. Any\left
needs a matching\right
.\left
etc. For example,<
produces a less-than sign, while\left<
produces an angle bracket.\big
etc. use the same interpretation as\left
(because they use\left
internally). Technically,\left
uses the delimiter code, while unadorned characters use the mathematical code.\left ... \right
inserts an inner node, while(
inserts an opening node. This becomes visible in$\sin()$
vs.$\sin\left(\right)
. Therefore you can never simply replace(
by\left(
and vice versa, you always have to check whether the spacing comes out right. An automatic solution to this issue is offered in Spacing around\left
and\right
, but the spacing within\left...\right
can still be different as explained in this answer.