3

I work in a field of mathematics where long, multi-line computations are bread and butter, and I mostly use align* (and its siblings) from the amsmath package to display them. However, I have not found a simple, foolproof way of typesetting such expressions. What is the easiest way of correctly aligning lines horizontally in multi-line expressions?

Ideally, the solution should not be overly technical, lest my coauthors resort to easier, but incorrect, solutions.

Take the following caricature of a multi-line computation: x = -y [newline] -y. If I have understood Knuth and Swanson1 correctly, then the above should be typeset as

Correctly aligned multi-line expression

that is, something like

x [thickspace] = [thickspace] - [no space] y
                              - [medium space] y

(where the minuses are horizontally aligned). (There should be no space after the first minus, since it's a unary operator, and a medium space after the second, since it's a binary operator.)

I have searched StackExchange for solutions, but in vain. Below are the solutions I have found so far, with MWEs at the bottom.

Some incorrect solutions

  1. The correct, but overly complicated way:

    \documentclass{article}
    \usepackage{amsmath}
    \begin{document}
    \begin{align*}
    x &= -y \\
    &\mathrel{\phantom{=}} -\:y
    \end{align*}
    \end{document}
    
  2. Do nothing. The minuses are incorrectly aligned.

    x &= -y \\
    &-y
    

    (I'm only changing the contents of align* here.)

  3. Indent arbitrarily using e.g. \quad or \qquad. The minuses are incorrectly aligned.

    x &= -y \\
    &\quad -y
    
  4. Use \phantom. The minuses are incorrectly aligned.

    x &= -y \\
    &\phantom{=} -y
    
  5. Use \mathrel{\phantom}. The minuses are incorrectly aligned, and there is no medium space after the second minus.

    x &= -y \\
    &\mathrel{\phantom{=}} -y
    
  6. Use {}&. There seems to be a thin and a thick space after the equals sign, and a thin space after the first minus.

    x ={}& -y \\
    & -y
    
  7. Use aligned. Produces the same as in (6).

    x &= \begin{aligned}[t]
    &-y \\ &-y
    \end{aligned}
    

1: Donald Knuth: The TeXbook, page 170 and Ellen Swanson: Mathematics into Type, Section 3.3.5.

Edit 29 January: I misread Knuth's table; there should be a medspace after the second minus. Also, turned the fragments into a full document.

Ulrik
  • 31
  • What about \documentclass{article} \usepackage{amsmath} \begin{document} \begin{align*} x = &-y \\ &-y \end{align*} \end{document}? – leandriis Jan 28 '21 at 20:37
  • @leandriis that gives mathbin spacing for - – David Carlisle Jan 28 '21 at 20:41
  • @DavidCarlisle: Interesting, didn't know that. – leandriis Jan 28 '21 at 20:42
  • 1 puts a thin space between - and y which is not the space you would normally get for an infix binary - (which is medspace) 3 is what I would do (or better qquad) (It is always better to post a complete document not fragments) – David Carlisle Jan 28 '21 at 23:00
  • @DavidCarlisle: Thanks, I changed "thin space" to "medspace" and turned to fragment into a full document. Regarding qquad, see my comment to your answer. – Ulrik Jan 29 '21 at 06:09

2 Answers2

2

enter image description here


\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align} x ={}& {-}y \ & {-}:y \end{align}

\end{document}

This produces the spacing that you ask for as clarified in comments:

x={} gives standard thickmuspace around =

{-}y then forces the prefix - with no space added,

and {-}\: produces a medmuspace between - and y as you would get automatically in x-y


The above however is not the layout I would use as there is no real semantic reason to align the - and (as can better be seen with a more typical alignment involving a second = or inequality) it makes it harder to distinguish a wrapped continuation line from a second aligned equation.

Compare the first form using the spacing given above with the second which has much simpler markup and I would say much clearer layout.

enter image description here


\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align} x ={}& {-}y \ & {-}:y \ ={}&{-}2y \end{align}

\begin{align} x &= -y \ & \qquad - y \ & =-2y \end{align}

\end{document}

David Carlisle
  • 757,742
  • This is equivalent to "solution" 5. It's incorrect because the second minus is typeset as a unary instead of binary operator. – Ulrik Jan 28 '21 at 21:04
  • @Ulrik OK done as you ask (although I would never use this layout) I'd be more likely to add \qquad at the start of the second line to indent it as it is a continuation not a second aligned equation. – David Carlisle Jan 28 '21 at 22:44
  • @Ulrik see updated answer with enlarged example. – David Carlisle Jan 28 '21 at 23:10
  • Thanks for your suggestions, but I feel all three solutions suffer from the faults mentioned in my question: The second solution is overly complicated (I would have a hard time convincing my coathors to write this themselves), and the third solution arbitrarily and incorrectly indents the continued line.

    Note: I consider Ellen Swanson the authority in these matters, and Section 3.5.5b clearly states "… align to the right of the first verb" (= in this example). See also 3.5.5d: "always keep expression visually within fences".

    – Ulrik Jan 29 '21 at 06:17
  • You are asking for a layout not directly supported and I show it with three extra { groups and a \: which seems pretty minimal, or you could as wipet just showed use a negative space and a custom command to have less markup in each instance. Either way it is not doing what ES states of aligning on the = the reason you are needing extra markup is that you are aligning on an unusual asymmetrically spaced - with no space to the left and a medium space to the right, and such a symbol spacing is not directly supported in tex. @Ulrik – David Carlisle Jan 29 '21 at 08:42
  • Please see the figure in Swanson's Section 3.3.5b. She's not saying you should align at the =, but to the right of the =. I honestly don't understand what is "unusual" about this example – the first minus is a negation operator, and the second minus is a binary operator. Swanson clearly says they should be aligned. How can this not be supported in TeX? – Ulrik Jan 29 '21 at 08:49
  • @Ulrik Swanson 3.3.5b is a vague sketch you are choosing to interpret that as saying that the - on the wrapped line should be asymmetrically spaced, I don't see any reason to interpret it that way. But as I say I would definitely indent this, especially if there are following forms that do need to align on the = as I show above. – David Carlisle Jan 29 '21 at 08:56
2

Something like this:

\def\n{\mathrel{\phantom{=}}\mskip-\thinmuskip{}}

$$\eqalign{ x &= -y \cr &\n -y } $$ \bye

wipet
  • 74,238
  • +1 yes I wondered about suggesting something like that as well, you are probably right that as this puts all the extra markup in one place it's easier to define a custom command to do it. (I still wouldn't use this layout though as I think it's confusing for the reader) – David Carlisle Jan 29 '21 at 08:45
  • Thanks, this seems like a robust solution. I was hoping to avoid having to create new macros, though – since this must be a very commonly encountered issue, I felt it should be natively supported by TeX (or LaTeX, or amsmath). – Ulrik Jan 29 '21 at 08:52