90

This expression is just an example, of course.

My question is: should one always specify the quantity being exponentiated by surrounding it with braces, or is it acceptable to drop those braces? (Cases in which \left(...\right) are used lie outside the scope of my question.)

It seems to me that one should write {(x+y)}^2 but I do see (x+y)^2 a lot...

enter image description here

\documentclass{article}
\begin{document}
${(x+y)}^2$ or $(x+y)^2$?
\end{document}
Hendrik Vogt
  • 37,935
jub0bs
  • 58,916
  • I think there's no need for the first expression, as the second is readable, and if you are between lines (inline math), the second expression won't change interline spacing. – Manuel Mar 23 '13 at 23:53
  • 23
    Definitely (x+y)^2. – egreg Mar 23 '13 at 23:53
  • 10
    There is a clear visual difference between the two renderings, in the position of the exponent. – Kaz Mar 24 '13 at 05:40

5 Answers5

104

There is an argument that logically the squaring operation applies to the whole subterm so the {} are correct. However it makes the {} into a mathord which can affect spacing to adjacent terms and if the subterm is large (but \left \right are not used so the brackets are not stretched) then the superscript could float up and become visually detached. So on balance, I think it's better to use the )^2 form.

enter image description here

\documentclass{article}

\begin{document}

${(x+y)}^2 + \sin{(x+y)}^2+{(x+\frac{\frac{A}{B}}{C}+y)}^2$

$(x+y)^2 + \sin(x+y)^2+(x+\frac{\frac{A}{B}}{C}+y)^2$

\end{document}
David Carlisle
  • 757,742
  • 13
    The parentheses themselves apply to the whole subterm. So you square the subterm, by setting the exponent to the closed parenthesis. – egreg Mar 24 '13 at 00:00
  • 7
    @egreg, mathematically yes, but that's not the way TeX views it (as you know:-) – David Carlisle Mar 24 '13 at 00:02
  • As both a typophile (type that word and ask Google for spelling suggestions :) ) and an amateur mathematician (if meta) I prefer the second line to the first as I have a preference for closely attaching functions and parenthesis. Just sayin... – hsmyers Mar 24 '13 at 01:55
  • @David: See my answer for another reason why {} shouldn't be used. – Hendrik Vogt Mar 24 '13 at 20:51
  • @HendrikVogt Yes I should have mentioned the spacing (and lack of line breaking) within the subterm. egreg also mentioned that in a comment to one of the other answers. – David Carlisle Mar 24 '13 at 20:55
31

There's one major reason against using {} that isn't mentioned in the other answers: like \left(...\right), it makes the spacing inside lose its flexibility. Look at this example:

output

In the first case, the space around the - is shrunk to make everything fit into the line, but the {} around the parentheses has the effect that the space around the + isn't shrunk. In the second case you get even spacing: the shrinking around the - is the same as around the +.

\documentclass{article}
\setlength{\textwidth}{4.9cm}
\begin{document}
$a+{(x+y)}^2$ versus $a+(x+y)^2$
\end{document}

Also if you want to prevent a line break, don't use {} around the expression, use \nobreak instead at appropriate places, like in (x+\nobreak y)^2.

jub0bs
  • 58,916
Hendrik Vogt
  • 37,935
  • ${...}$ is the correct notation for preventing a line break of an inline formula –  Mar 24 '13 at 20:55
  • @Herbert: It would be correct if it wouldn't affect the spacing! – Hendrik Vogt Mar 24 '13 at 20:57
  • 7
    @Herbert if you use {} to prevent linebreaking then you have to accept it also freezes horizontal glue. An alternative (but perhaps less convenient way) is to set binoppenalty and relpenalty to 10000 – David Carlisle Mar 24 '13 at 20:59
  • @DavidCarlisle: sure ... –  Mar 24 '13 at 21:00
  • IMHO, this argument is problematic: You're suggesting that we should use construct which are semantically inappropriate (i.e. have the ^2 only apply to the right parenthesis) in order to achieve a more pleasing effect in the final output - because of questionable design choices of the typesetting engine, which cannot be otherwise overridden. – einpoklum Nov 22 '13 at 07:54
  • @einpoklum: Well, I'm a mathematician, so I don't see inappropriate semantics here - why should I surround parentheses with braces? I agree that this feature of the typesetting engine is cumbersome, but it's less a design choice than a limitation of computers from the late seventies, when the choice was made. It would of course be great if LuaTeX or some other engine would offer better typesetting in such a case. – Hendrik Vogt Nov 22 '13 at 08:27
  • You're squaring the parenthesized formula, not the right parenthesis, so the squaring should apply to the { elements of the formula } bunched together... at least that's the way I think of it. – einpoklum Nov 22 '13 at 08:43
  • 1
    @einpoklum: What I meant: For me, the parentheses () do already bunch the formula together, so there's no need for braces. – Hendrik Vogt Nov 22 '13 at 11:47
  • Umm, as we see in the example in David Carlisle's answer - no, they don't. Typesetting-wise, at least. – einpoklum Nov 22 '13 at 23:09
  • @einpoklum: That's what I was saying all the time: math-wise they do. (And typesetting-wise, the output is fine without braces; see Jobobs' answer. Knuth designed the TeX-engine in such a way that you don't have to type braces in order to get nice output.) – Hendrik Vogt Nov 23 '13 at 08:51
18

Months later...

A relevant passage of the TeXbook (Chapter 16, p129) is the following, which is in agreement with David's answer; I thought I should add it here, for completeness.

A superscript or subscript following a character applies to that character only; but when following a subformula it applies to that whole subformula, and it will be raised or lowered accordingly. For example,

$((x^2)^3)^4$ enter image description here

${({(x^2)}^3)}^4$ enter image description here

In the first formula the ^3 and ^4 are superscripts on the right parentheses, i.e., on the ) characters that immediately precede them, but in the second formula they are superscripts on the subformulas that are enclosed in braces. The first alternative is preferable, because it is much easier to type and it is just as easy to read.

(my emphasis)

jub0bs
  • 58,916
  • 2
    Isn’t this ultimately the same as David’s {(x+\frac{\frac{A}{B}}{C}+y)}^2 example (the last one)? However, the source is a good one. :) – Qrrbrbirlbel Nov 21 '13 at 19:50
  • The example is similar, yes. Perhaps I should just delete this answer and edit David's to add the reference... – jub0bs Nov 21 '13 at 19:55
3

I (edit just corrected my belief to) always use $\left( x + y \right)^2$, or rather I have a command for the parenthesis and doing $\paren{ x + y }^2$. That's semantically a valid thing to do, and I let the TeX engine authors sort out how they want to render it.

einpoklum
  • 12,311
  • 7
    I wouldn't recommend this. To be honest, I consider this wrong input. – egreg Mar 24 '13 at 11:17
  • 3
    The unnecessary usage of \left and \right and the braces; the \left-\right pair "freezes" the spacing around +, not allowing it to stretch or shrink for helping justification in in-line math; the braces usually put the exponent too high. – egreg Mar 24 '13 at 11:24
  • @einpoklum "suitable". that and not adding {} around the outside which as discussed in several answers here affects spacing in several ways. Actualy if you don't add {} you don't need to check for the following superscript as it will work automatically. – David Carlisle Mar 24 '13 at 14:05
  • @DavidCarlisle: Ok, I'm convinced, correcting accordingly and removing my earlier comments. – einpoklum Mar 24 '13 at 20:29
  • @egreg: {} also freezes the spacing, see my answer. – Hendrik Vogt Mar 24 '13 at 20:58
  • @HendrikVogt Yes, I know! But in the original formulation of the answer the damage was already done by \left and \right. – egreg Mar 24 '13 at 21:01
  • @egreg: Ah, I see! – Hendrik Vogt Mar 24 '13 at 21:04
  • 3
    einpoklum: In this particular case, I find some of the comments here hard to follow without being able to see the whole conversation--including the comments you deleted. That having been said, I don't want to suggest that deleting your own earlier comments that you now consider erroneous is in any way wrong. [Or even that I have any authority whatsoever to say whether it is wrong.] – Charles Staats Mar 24 '13 at 22:46
  • 1
    Just a little tip: The \DeclarePairedDelimiter macro from the mathtools package allows one to define \paren in a flexible (“suitable”) way. – Qrrbrbirlbel Nov 21 '13 at 19:56
  • @Qrrbrbirlbel: Ping... can you explain why this allows you to define a 'suitable' \paren? – einpoklum Oct 08 '14 at 18:45
  • 1
    \paren from \DeclarePairedDelimiter{\paren}{(}{)} gives will only freeze the spacing when used with the * option (equivalent to \left..\right), otherwise it avoids the {...} grouping problem. – Andrew Swann Mar 27 '17 at 11:10
2

Unlike MathML, TeX's math mode makes no attempt to encode the semantics of the typeset math. Math mode is susceptible to linebreaks, but you can use braces to prevent that: the formula${(x+y)^2}$ will not break across lines, while $(x+y)^2$ may.

Mike Stay
  • 477