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.

5
  • What about \documentclass{article} \usepackage{amsmath} \begin{document} \begin{align*} x = &-y \\ &-y \end{align*} \end{document}? Commented Jan 28, 2021 at 20:37
  • @leandriis that gives mathbin spacing for - Commented Jan 28, 2021 at 20:41
  • @DavidCarlisle: Interesting, didn't know that. Commented Jan 28, 2021 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) Commented Jan 28, 2021 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. Commented Jan 29, 2021 at 6:09

2 Answers 2

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} 
8
  • This is equivalent to "solution" 5. It's incorrect because the second minus is typeset as a unary instead of binary operator. Commented Jan 28, 2021 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. Commented Jan 28, 2021 at 22:44
  • @Ulrik see updated answer with enlarged example. Commented Jan 28, 2021 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". Commented Jan 29, 2021 at 6: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 Commented Jan 29, 2021 at 8:42
2

Something like this:

\def\n{\mathrel{\phantom{=}}\mskip-\thinmuskip{}} $$\eqalign{ x &= -y \cr &\n -y } $$ \bye 
2
  • +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) Commented Jan 29, 2021 at 8: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). Commented Jan 29, 2021 at 8:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.