182

Why does the addition of a \left and \right introduces an extra space around the formula? Specifically, why do $\cos(\theta)$ and $\cos\left(\theta\right)$ render differently?

Is there a way to use resizable delimiters without introducing the spurious spaces?

Why did Knuth design it like this?

Edit: This occurs too, if \operatorname{MYOP} or some other operator, like \lim, \min etc. is used. (This edit is basically to make it easier to find this answer.)

lumbric
  • 4,269
  • 12
    Yes, this is infuriating... Prevents me making a single command for Big-Oh notation that can handle both simple and tall formulae. How on earth was this overlooked??? – j_random_hacker Oct 26 '10 at 23:16
  • 11
    Not sure if it helps in all cases, but $f{\left(x\right)}$loses the extra space. With an operator like \cos instead of something simple like f, additional braces around the operator are needed to kill the space: ${\cos}{\left(\theta\right)}$. Not as good as answers below, but nice to know if you aren't able to change preamble. – alex.jordan Mar 04 '15 at 06:49

7 Answers7

153

I've found an answer in a usenet post by Heiko Oberdiek and another by Donald Arseneau. Improved code thanks to shiznick.

The issue is that \left and \right introduces an inner atom (see The TeXbook, chapter 18, section 4), which has different spacing rules than ordinary atoms, produced by ( and ) (and the other delimiters).

To remove this spurious spacing, one has to manually insert opening and closing atoms in the formula. Specifically, $\cos(\theta)$ and $\cos\mathopen{}\left(\theta\right)\mathclose{}$ render exactly the same.

However, inserting it does make your formulas ugly. So a good solution would be to just redefine \left and \right:

\let\originalleft\left
\let\originalright\right
\renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft}
\renewcommand{\right}{\aftergroup\egroup\originalright}

Incidentally, this code also fixes the spacing of |, that is very fragile. Check for example $|+x|$ and $\left|+x\right|$. Also $\cos|\theta|$ and $\cos\left|\theta\right|$. This is due to the fact that both delimiters are equal, and TeX doesn't know if they're opening or closing the expression. The same logic applies to \|.

EDIT: The old code had a problem with subscripts and superscripts: they didn't accompany the growth of the delimiters. Fixed now thanks to Philipp Stephani and Heiko Oberdiek.

doncherry
  • 54,637
  • Can you give an example of where \right)\mathclose{} gives you better spacing? I don't see the application here. – Will Robertson Sep 02 '10 at 01:13
  • 2
    Of course! The line where I first noticed the bug: \[\ket{\psi} = \alpha \ket{0} + \beta \ket{1},\]

    the \ket macro is

    \newcommand{\ket}[1]{\left| #1 \right\rangle}

    which was giving too much space between the \rangle and the comma.

    Also, this question: http://tex.stackexchange.com/q/2401/1035

    – Mateus Araújo Sep 02 '10 at 02:37
  • 37
    As result of the newsgroup thread I had written package mleftright. It also adds some error checks (group nesting levels). – Heiko Oberdiek Aug 05 '12 at 17:05
  • What about $y=\mleft[x^2\mright],$ vs. $y=[x^2],$? Why is there a difference? – letmaik Feb 22 '13 at 21:17
  • @neo - it's (generally) better to post a query as a new question rather than as a comment to an answer, especially if it's been a while since that answer was posted. By posting a new question (preferably together with a full MWE), you assure yourself that a large group of people will see your posting. Incidentally, it's preferable to keep the comma outside the math material -- unless it really is part of a math expression. – Mico Mar 01 '13 at 11:11
  • 5
    What are the limitations of this approach? Are there cases when it is not recommended to apply this workaround? Or is it something that can be safely added to all preambles? – Dror Jan 12 '14 at 14:08
58

The package mathtools has implemented a general solution for that problem. It defines the command \DeclarePairedDelimiter that you can use as follows:

\DeclarePairedDelimiter\pars{\lparen}{\rparen}.

With this single declaration you have a new powerful command for several cases:

  • \pars{x} just replaces $(x)$
  • \pars*{x} will do the right thing with \left and \right (same result as the answer of Mateus Araújo)
  • \pars[\big]{x} which replaces $\bigl(x\bigr)$ (all the size commands work)
Lorents
  • 170
  • 5
    I've run a few cases with 'mleftright' and 'mathtools', and the results seem identical. The code in 'mathtools' seems to have greater flexibility, although 'mleftright' includes some additional checks for error conditions. Is there a reason to prefer one of these rather than the other? – John Sep 10 '18 at 01:52
  • 10
    Shouldn't it be \bigl(x\bigr) instead of \bigr(x\bigl)? – L. F. May 09 '19 at 13:51
28

"As result of the newsgroup thread", which was mentioned in Mateus Araújo' answer, Heiko Oberdiek wrote the mleftright package. "It also adds some error checks (group nesting levels)." (both cited from Heiko's comment to Mateus Araújo' answer)

\mleft and \mright from that package can be used instead of \left and \right and do not insert additional space.

When additional space shall not be used, also \left. and \right. should be looked at (see What does \right. do?). To get rid of the additional space there
\left.\kern-\nulldelimiterspace and \right.\kern-\nulldelimiterspace can be used (probably best by defining those as new commands and using those).

Stephen
  • 14,890
24

The following code fixes both \left and \right -- put it in the preamble:

\let\originalleft\left
\let\originalright\right
\def\left#1{\mathopen{}\originalleft#1}
\def\right#1{\originalright#1\mathclose{}}

I haven't tested it in all scenarios, of course, but I added it to the preamble of my entire Ph.D. dissertation, and it worked without errors and without any additional modifications to the code.

shiznick
  • 326
  • 2
    It makes me somewhat nervous to use macros to mess around with delimiters; they have their own unfathomable rules of spacing. The simpler, the better.

    But I liked your idea of actually fixing \left and \right instead of just defining new commands.

    – Mateus Araújo Sep 01 '10 at 03:46
  • 1
    It makes me nervous, too. I think this is about as simple as it's going to get (although I'm happy to be proven wrong). The fact that such a long and complicated 200-page document TeX'ed without incident with these changes (and no others) gives me hope that they are minimal and robust. – shiznick Sep 01 '10 at 04:29
  • Well, I do consider mine simpler (to the TeX engine).

    But I did some non-trivial experiments with yours and couldn't find an error.

    – Mateus Araújo Sep 01 '10 at 05:52
  • 1
    Okay, I have an error (again due to Arseneau):

    $\left \delimiter "4266308 8,9\right \delimiter "5267309 $

    Its not an spacing error: your macro fails to realise that \delimiter "5267309 is actually just one character.

    – Mateus Araújo Sep 01 '10 at 12:42
  • 1
    Yup. Agreed. The code you posted works where this code fails, and semantically it probably is better. (I now know about \aftergroup! :) On the other hand, you made improvements to that code based on the code I posted here and didn't mention this fact in your revisions. I'm happy to work toward the same goal of improving the code, but a short line like "(Thanks to shiznick for the suggestion of using \let.)" really goes a long way. It might even earn you another uptick... – shiznick Sep 01 '10 at 15:36
  • 1
    Of course. I just thought it was obvious that I took the code from you. – Mateus Araújo Sep 02 '10 at 02:25
9

With LuaTeX you can control the spacing between an op atom (such as \cos) and an inner atom (which is what is generated by a \left...\right pair).

\documentclass{article}

\Umathopinnerspacing\displaystyle=0mu
\Umathopinnerspacing\textstyle=0mu

\begin{document}

$\cos(\theta)$

$\cos\left(\theta\right)$

\end{document}

enter image description here

Henri Menke
  • 109,596
8

This is in answer to the particular question of why Knuth designed it like this. I believe it is because opening and closing using \left and \right is often done in conjunction with sums or other big mathematical symbols. Compare the following two formulas: normalwith_fix

The bottom one is with the suggested fix from the earlier answer. In my opinion, the top one looks more balanced, which is probably the reason for the functionality being as it is.

  • Considering that Donald Knuth has such a reputation, Latex is an collection of warnings how not to design a language. The reasoning may apply in that particular application but not in others, and there is no built-in alternative. – shuhalo Jul 30 '23 at 22:02
2

I have been using the accepted answer for a while now, but actually I came up with something in the last few days that also gets around the issue that \left \right prevents line breaking.

\def\delim#1#2#3{
  \mathopen{\left#1 \vphantom{#2} \right.}
  \kern-\nulldelimiterspace #2 \kern-\nulldelimiterspace
  \mathclose{ \left. \vphantom{#2} \right#3 }
}

The \kern-\nulldelimiterspace removes the spacing produced by \right. and \left..

It's not too hard to extend this to allow for \middle either.

One note of caution: I think nesting this too much will cause LaTeX to run out of memory.

Max Xiong
  • 281
  • 1
  • 4
  • How do you use \delim? From what I can tell, it doesn't work in an align environment because an error occurs when you include \\ inside \delim. It also doesn't seem to work for automatic line breaking when using inline text. – Paul Wintz May 16 '22 at 19:09
  • @PaulWintz \delim{(}{\frac{1}{2}}{)}. You should define other macros for parens, brackets, etc. based on this. I just tested this again on overleaf and it does make the delimiters behave as if they are just \bigl \bigr etc. so that the spacing around operators can still break lines and expand/shrink to justify text. And no, it won't work if you put a line break in the middle of it in align mode. – Max Xiong May 20 '22 at 16:50