8

The middle equation in the following example seems to have wrong spacing around the = sign. The only difference is that I used =& for aligning around the = sign rather than &=.

This worries me, because sometimes I want to emulate "multline equations", and I want the equations to be aligned like that but without putting an = sign at all.

Is it known, explainable behaviour? I.e., is it a bug or a feature?

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
\[
 a=b
\]
 \begin{align*}
  a =& b\\
  c =& d
 \end{align*}
 \begin{align*}
  a &= b\\
  c &= d
 \end{align*}
\end{document}

output

3
  • 2
    There's nothing that prevents you to put anything you want after &. Please, add an example of what you mean by "emulate multiline equations". For getting the space right, use &=. But also ={}& can work.
    – egreg
    Jan 14 '12 at 11:46
  • 1
    Does &\phantom{=} work as well?
    – celtschk
    Jan 14 '12 at 11:56
  • @egreg: by "emulate multiline equations" I meant a run-away right-hand side, like the example you present in your answer. I would like to achieve that via \begin{align} a=&axy+b\\&+y\end{align}, but it gives the wrong spacing, as I argued. Both ={} and &\phantom{=} seem to work, though. Jan 14 '12 at 22:36
6

The technical reason is that the align environment adds an empty math object {} to the beginning of the second cell in each aligned column, but doesn't add it to the end of the first one. This is precisely so that the equals sign behaves as though it comes between two variables when you write &=. I honestly can't figure out why it doesn't add one in the first cell also; although it does occasionally change the spacing (such as when writing =&) it never seems to do so in a way that is obviously wrong.

As others have suggested, you can write ={}& when you want the equals sign to have the correct spacing.

7
  • 1
    The specification of the alignment is (roughly) \hfil$#$&${}#$\hfil, so with a&=b TeX sees $a$ in the first cell and ${}=b$ in the second. With a=&b this becomes $a=$ and ${}b$ that doesn't insert a thick space after the =.
    – egreg
    Jan 14 '12 at 15:55
  • Right (that's how the TeXbook defines its alignment macro, anyway); but why not $#{}$&${}#$? It would put a double {}{} between every column, but when would that actually cause a visible error?
    – Ryan Reich
    Jan 14 '12 at 15:58
  • 1
    Programmers' decision to define a uniform syntax? :)
    – egreg
    Jan 14 '12 at 16:04
  • 1
    @egreg -- programmer's decision, but the programmer was knuth. this is inherited from plain tex. to test it, use the code on p.191 of the texbook, just before exercise 5.19. input it twice, once with &= and once with =&, then tex it. you'll get the same bad spacing that comes from amsmath. i've found this commentary regarding the "decision" on pp.190-191: "Multi-line displays usually consist of several equations that should be lined up by their = signs", and "For example, every right-hand side often begins with an = sign." may be something more in tex.web, but didn't check. Jan 14 '12 at 20:22
  • 3
    I've figured out why do not add one in the first cell. Consider A&=-B. If add in both cell, then the minus sign would be recognized as an binary operator, thus cause extra space between minus sign and B
    – Steven Sun
    May 14 '20 at 7:58
7

It is a feature, and an important one: it is simple and efficient for the vast majority of cases, because quite frequently align is used for aligning successive rows at a relation symbol. With &= you are guaranteed to get the correct spacing around the = sign.

On the other hand, sometimes one can need also to "align after the equals". There are various ways to accomplish this task.

For example, if the right hand side of an equation to be aligned is too long, you can use aligned:

\begin{align}
a &= b\\
c &= \!\begin{aligned}[t]
     &axy+b\\
     &+y
     \end{aligned}
\end{align}

This is by no means a "workaround": it marks correctly what one is going to achieve (apart from a small adjustment that may indeed qualify as a small bug, but would also be there with a =& syntax).

Why did the developers of amsmath chose this instead of =&? Well, one reason is that they borrowed some ideas from the TeXbook. But, more importantly, many cases of align follow the pattern

\begin{align}
x &= <some expression> \\
  &= <a development of that expression> \\
  &= <a further development>
\end{align}

where a =& syntax would be cumbersome.

In some cases it can be handy to align differently; I've found some of them and

a = {} &

solves the spacing problem.

2
  • The first solution that you outline still gives a slightly incorrect spacing. Change the first equation to $a&=a$ to make it more evident: the two $a$ in the right-hand sides are not perfectly aligned. Jan 14 '12 at 22:29
  • Could you please give more arguments in favor of "it's a feature"? In the other answer, Ryan Reich seems to think the opposite, and I also do not see any good reason after reading the rest of your message. Unless I am misunderstanding you, the two "solutions" you suggest are workarounds to get the behaviour I was expecting. Jan 14 '12 at 22:34
2

In the align environment, it treats A & B as $A$ ${}B$. which means if you input

\begin{align*}
 A  &  B  \\
 C  &  D
\edn{align*}

What it actually outputs is a tabular with four items like

That's how align works.

As for your question, the spacing is added only when there's something after it. If nothing is after the "=", no space would be added.

I've also figured out why they do not add {} in the first cell $A{}$ ${}B$. Consider A&=-B. If add {} in both cell, the spacing between the "=" and "-" would become bigger(space is added twice). Also, "=" should be on the right-hand side, or the minus sign would be recognized as an binary operator, thus cause extra space between minus sign and B.

You can see none but the first one made the correct spacing.

enter image description here

In real life, the Relation sign is often followed by Ordinary, But to proceed with Ordinary/Binary/Operator. Thus, the author made suggestions "=" must on the right so that character after "=" could be classified (Ord/Bin/Op) correctly.

In our little example, only when the Rel is on the right can the TeX figured out the minus sign as Ordinary rather than a Binary.

As for your question, if you don't mind adjusting the spacing manually, you can use A={} & B. or you can use

A & = B \\
  &\phantom{\;=\;}C

The \; is normally added by TeX automatcally.

4
  • Thanks! This is useful information, even though technically it is a slightly different issue than the one I had in my question: in one case the problem is the $-$ that needs something around it to behave as a binary operator, in the other it's $=$ that needs something around it to behave as a relation symbol (and these two classes of symbols are treated differently in Tex). May 14 '20 at 9:16
  • I have added some explanation of how align works and how to solve your problem.
    – Steven Sun
    May 15 '20 at 10:31
  • Thanks; that makes a lot of sense; very clever. May 15 '20 at 10:32
  • I've also written a copy of this in Chinese. See zhuanlan.zhihu.com/p/140775227
    – Steven Sun
    May 15 '20 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.