8

Wikibooks' LaTeX/Advanced Mathematics article has this example:

    \begin{align}
 f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\
      &\qquad {} + 10x + 12
\end{align}

It mentions that

we need to insert the double braces ({}) before the + sign, otherwise latex won't create the correct spacing after the + sign. The reason for this is that without the braces, latex interprets the + sign as a unary operator, instead of the binary operator that it really is.

My question: is this for the "older" versions of LaTeX? Because I tried with and without the braces {}, and can't seem to see any difference. Either that, or the difference is very slight?

For "modern LaTeX", do we need to do this?

Secondary question: Is \qquad{} same as \qquad {}? (note the space)

Null
  • 1,525
yoyostein
  • 573
  • 3
  • 10
  • 6
    I don't think that text is correct. \qquad behaves as an atom ( as is {}) so you get binary op in any case. A lot of these "online" resources are not correct. – daleif Nov 02 '16 at 11:13
  • How ever if you do this example in a different way then the {} is important f(x) = {} & ... \\ &= 10x ..., there the {} is needed to force = into leaving the correct spacing after it as it inly sees & otherwise, in which case it leaves no space after itself. – daleif Nov 02 '16 at 11:15
  • Re your secondary question: Spaces after control sequences are absorbed by TeX, unless the control sequence only has a single character in it (not counting \). So \cs{} and \cs {} or even with a linebreak between are all the same, but \c{} is different from \c {}. – siracusa Nov 02 '16 at 11:28
  • @siracusa that is not always true, xparse can create macros where \foo{} and \foo {} are two different things. – daleif Nov 02 '16 at 11:35
  • @daleif Could you please paste a small sample showing how that could be done? I can't see any way to make the parser not ignore the space, other than changing the catcode of the space perhaps, but that would have to happen before the macro is expanded. – siracusa Nov 02 '16 at 18:16
  • @siracura xparse can make a lot of magic. See the xparse manual it is the g modifier, which makes {} into an optional argument, for it not to eat something it should not, it then differentiates between whether there is a space or not. It all comes from the machinery of latex 3 – daleif Nov 02 '16 at 18:25

2 Answers2

9

Regarding your first question: In general it is necessary to follow this advice and simulate the first operand by {}, to allow TeX to distinguish between unary and binary operators. It is just that the align environment treats operators differently; \qquad does not simulate an operand. So if you don't know otherwise, I'd follow the advice.

The following document demonstrates the effect (or non-effect) of a pair of braces.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\begin{document}

$+10x$

${}+10x$

$\qquad+10x$

$\qquad{}+10x$
\begin{align*}
&+10x\\
&{}+10x\\
&\qquad+10x\\
&\qquad{}+10x
\end{align*}
\end{document}

enter image description here

gernot
  • 49,614
  • 1
    Interesting. So why the effect in align, and not outside? – daleif Nov 02 '16 at 11:36
  • Very interesting. I think I'll only be using qquad in align mode. – yoyostein Nov 02 '16 at 11:50
  • 1
    @yoyostein This is the wrong conclusion. \qquad is just a space command, useful in many places, and doesn't have anything to do with the issue (there are no differences regarding operator spacing in my example above if you compare the cases with/without \qquad). – gernot Nov 02 '16 at 11:52
8

Your source is incorrect, but the additional {} doesn't hurt.

Let me explain what happens. The align environment builds pairs of aligned columns, with cells in math mode that are right aligned in the odd-numbered columns and left aligned in the even-numbered ones.

Each cell in an even-numbered cell begins with an implicit {}, which makes for an empty Ord atom.

The processing of math mode material is quite complex, but not really too complicated.

Each item can be a math atom or “other”; spacing commands are in the “other” category. Subscripts and superscripts are attached to the math atom they belong to, so they are not of a concern here. Math atoms can be of several types:

  • Ord (ordinary symbol)
  • Op (operator)
  • Bin (binary operation symbol)
  • Rel (relation symbol)
  • Punct (punctuation symbol)
  • Open (opening delimiter)
  • Close (closing delimiter)

and some others that, however, are basically transformed into Ord; in order to keep it simple, I'll not analyze them.

The formula TeX finds when processing the & \qquad + 10x line is

{} \qquad + 1 0 x

(because & implies it belongs in an even-numbered column) that translates in the following sequence of atoms and other items (denoted by “X”)

Ord X Bin Ord Ord Ord

The sequence of math atoms determines the spacings that are automatically added. If a Bin atom is preceded and followed by Ord atoms, TeX inserts around it a medium space, independently of “other items” in between; no space is inserted between Ord atoms. You find the complete list at https://tex.stackexchange.com/a/240579/4427

After automatically inserted spaces are dealt with, TeX builds a horizontal box with the items, this time keeping into account the “other items”, so you get

(empty) \qquad (medium space) + (medium space) 1 0 x

(the spaces above are just for separating items)

If the line had been & \qquad {} + 10x, the formula would become {} \qquad {} + 10x and the sequence of atoms and other items would be

Ord X Ord Bin Ord Ord Ord

that would add spaces so to get

(empty) \qquad (empty) (medium space) + 1 0 x

and the result would be the same, because an empty atom produces nothing (apart possibly from triggering an automatic insertion of spaces).

This has been the behavior of amsmath since its release, even when it was called amslatex, so 1992 or so.

The purpose of the implicity empty Ord atom is to get correct spacing in a typical align:

\begin{align}
A &= B+C \\
D &= E+F
\end{align}

where B+C is not really typical, but is used by way of example. The sequence of atoms when processing the second cell in the first row would come from

{} = B + C

so translated into

Ord Rel Ord Bin Ord

and, since TeX inserts a thick space between either Ord-Rel or Rel-Ord, we get

(empty) (thick space) = B (medium space) + (medium space) C

This is to allow the result to be the same as if it had been A=B+C as far as automatically inserted spacing is concerned.

Actually, there is a place where to be careful: if a cell in an even-numbered column starts with an Op atom (such as \sum or \log), a thin space would be inserted, because TeX automatically adds it between Ord and Op.


Is there a difference between \qquad{} and \qquad {}? No. Spaces after a control word are ignored.

egreg
  • 1,121,712
  • you might also cite the amsmath documentation (texdoc amsmath), which on p.3 states "Putting the & after the relation symbol will interfere with the normal spacing; it has to go before." – barbara beeton Nov 02 '16 at 12:39