9

In the following MWE, what is responsible for the large space before the align environment? How can I remove it (it doesn't appear if I change to \[ \] and remove amsmath) ?

\documentclass{article}

\usepackage[english]{babel}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}

\begin{document}
\setlength{\parskip}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}

\lipsum*[2]

\lipsum*[2]

\begin{align*}1, 2, 3\end{align*}

\lipsum*[2]

\lipsum*[2]
\end{document}

I have read Spacing around align environments (but it doesn't cover the case in which there's a \par before \align and Remove vertical space around align (but it attributes the space to \parskip, which seems wrong: setting \parskip to 0 pt doesn't remove the space). I also read Remove extra vertical space in amsmath's align environment and Remove vertical space around align equations, but they just say that one shouldn't leave a \par before an \align. I'm curious to know what actually causes this blank space, even with all seemingly relevant lengths set to zero.

Edit for Reopen

Why does LaTeX insert space before formulas? is related, but it doesn't address the why: it says that leaving a blank line in the source causes the space but it doesn't say who is adding this space (and it's clearly an amsmath-specific this: the regular displaymath doesn't do that).

Unwanted space before align*

Clément
  • 4,004
  • The space you're looking for is \parskip + \baselineskip. – Werner Nov 02 '16 at 05:54
  • Ah, of course! But why is it adding a \baselineskip in there? – Clément Nov 02 '16 at 06:02
  • It isn't a space it's a spurious line of a paragraph. – David Carlisle Nov 02 '16 at 07:34
  • @DavidCarlisle I don't think this is a dupe. Your answer on that question doesn't say what the reasoning is behind the addition of this blank line. The actual reason is what I'm interested in: it has plenty of non-intuitive consequences, like making it tricky to wrap align* in \begin{small}, for example. – Clément Nov 02 '16 at 16:17
  • @Clément It's built in to tex-the-program so without talking to Donald Knuth it's hard to give reasoning, like many things in TeX, it just is what it is. – David Carlisle Nov 02 '16 at 16:28
  • 1
    @Clément you could ask that the answer there is extended but the question is an almost exact dupe "why insert space before" and "where does the space before" couldn't be much closer. – David Carlisle Nov 02 '16 at 16:31
  • I will remove my "gold hammer" close but it is a duplicate and the paragraph is added by tex-the-program's $$ not anything in amsmath , so it should be closed as a duplicate but I'll let that be decided by voting:-) – David Carlisle Nov 02 '16 at 17:34
  • @DavidCarlisle I think this comment (it's built into TeX) along with your comments under Mico's answer (it's also inserted by \[, but less visible), would make would make a terrific answer, actually :) – Clément Nov 02 '16 at 20:48
  • @Clément honestly I think that's the answer I gave to the duplicate question! – David Carlisle Nov 02 '16 at 21:10
  • @DavidCarlisle: Sorry for the misunderstanding :/ Your response in there says: "it's wrong to leave space", which I knew from other responses on other questions that I linked to. Your comments here say "this is a teX-level thing: TeX adds an hbox before $$". I learnt a lot from your comments here; more than from your answer there. That's all :) – Clément Nov 02 '16 at 21:50
  • 1
    @Clément maybe I'll add some text somewhere:-) – David Carlisle Nov 02 '16 at 22:10

2 Answers2

11

You ask,

Where does the [vertical] space before \align* come from?

It comes from an input mistake -- not a syntax error, mind you, but a mistake nevertheless -- in your code: Display-math environments should never, ever occur at the start of a paragraph.

In the following code, which preserves your \setlength instructions, the instance of align* that is not preceded by a paragraph break features no extra vertical whitespace; in contrast, the instance of align* that is preceded by a paragraph break does feature the dreaded extra whitespace. Observe that this happens even though the five length parameters have been set to 0pt.

To recap: Never begin a paragraph with a display-math environment. Not only is it poor practice from a typographic standpoint, it's also poor practice from a discursive/explanatory standpoint.

enter image description here

\documentclass{article}
\usepackage{amsmath,lipsum}

\begin{document}
\setlength{\parskip}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}
\lipsum*[2] 
\begin{align*}1, 2, 3\end{align*} % no paragraph break before  this 'align*'
\lipsum*[2] 

\begin{align*}1, 2, 3\end{align*} % paragraph break before this 'align*'
\lipsum*[2]
\end{document}
Mico
  • 506,678
  • Hmm. Aren't the various lengths in your example reset to their original values by the \normalsize issued when entering \begin{document}? – Clément Nov 02 '16 at 06:17
  • Also: you mention display math, but this issue is specific to the AMS environments, right? If so, do you know why they decided to add this surprising gotcha? More concretely, deep down, is align just issuing a \vspace{\baselineskip}? – Clément Nov 02 '16 at 06:18
  • @Clément - I've updated the screenshot to show the result of moving the \setlength instructions to after \begin{document}. – Mico Nov 02 '16 at 06:22
  • @Clément - I would not call it a gotcha. Instead, I'd say that the amsmath settings are a prime example of us (the users of LaTeX and amsmath) being treated like competent adults rather than as morons. We are assumed to know and abide by the rules of good typography -- including those that pertain to typesetting display-math environments. If we decide not to play by the rules, well, that's our choice, and we have to live with the consequences. Incidentally, I have no idea why Leslie Lamport did not apply the same rules when he created \[ and \]. – Mico Nov 02 '16 at 06:25
  • Thanks for clarifying! I still don't really see the point in adding this baselineskip, though: does it gain anything over what the usual \[ does? – Clément Nov 02 '16 at 06:37
  • Any downside to adding \BeforeBeginEnvironment{align*}{\ifvmode\PackageError{clement}{Don't use amsmath classes in vertical mode}\fi} to my personal preamble, btw? – Clément Nov 02 '16 at 06:37
  • 1
    @Clément - I'd issue a warning, not an error message. :-) No rules without exceptions, right? I can't think of an exception to the rule that one shouldn't start a paragraph with displayed math. However, you (or others) just might, in which case it wouldn't be good to be stopped by an outright error message... – Mico Nov 02 '16 at 07:04
  • 1
    @Clément amsmath doesn't add a space the spurious paragraph is added by the primitive $$ display math and so \[ and \begin{equation} etc will do the same, so your suggested errpr message would be a bit misleading. – David Carlisle Nov 02 '16 at 07:37
  • 2
    @DavidCarlisle - If you take my MWE, don't load amsmath, and replace the instances of \begin{align*} with \[ and the instances of \end{align*} with \], you will not get the extra vertical space if the displayed equation occurs at the start of the paragraph. This must be because of the \ifvmode\nointerlineskip portion of the code for \[. However, as you've pointed out, the extra vertical space does occur if $$ or \begin{equation} is used to initiate a displayed equation. – Mico Nov 02 '16 at 09:10
  • 1
    @Mico with \[ the nointerlineskip reduced the damage a bit but there is still a spurious hbox there it's just harder to spot:-) Note the main way this "space" differs from a vertical space is that as it is an hbox it is not discarded at a page break. – David Carlisle Nov 02 '16 at 09:14
  • @DavidCarlisle: Since the box you and Mico were talking about is 0pt tall (it comes from \makebox[.6\linewidth]{}), not having the interline glue above it actually makes a difference worth one line. – GuM Nov 02 '16 at 22:57
  • @GustavoMezzetti yes sure, that's presumably why Leslie defined \[ that way. – David Carlisle Nov 02 '16 at 23:24
  • 2
    I honestly think that this is a perfect example of how truly awful latex is. The worst bit is that there's no sensible fix to globally allow a line break before these blocks. The whitespace-sensitive-but-still-a-total-eyesore thing is hilarious too. – N. McA. May 19 '18 at 21:33
  • @N.McA. - I honestly can't tell what the point is that you're trying to make (other than, maybe, to vent a bit). Please elaborate. – Mico May 20 '18 at 08:42
  • @Mico Good typography depends on circumstances. The typesetting engine doesn't know what the writer is trying to do, so there should be no such unnecessary restriction. I think that treating like an adult is to remove such restrictions. One exception to the rule, as you mentioned, is something like \[ a^2 + b^2 = c^2 \] That is the well-known formula for the hypotenuse of a right triangle. – matj1 May 03 '23 at 13:21
  • @Mico An other example of the exception is that there is an equation alone in a floating environment or in a footnote. – matj1 May 03 '23 at 13:31
  • @matj1 - I'm not sure I follow you. What are you saying? Please elaborate. – Mico May 03 '23 at 14:02
  • @Mico You wrote that LaTEX and amsmath users are treated like adults because they are supposed to abide by the rules of good typography. I understand it as that the users are treated like adults in this case by having more restrictions. My point is that LaTEX and other such tools don't the user's intent, so they can't effectively impose rules for good typography because rules for good typography depend on what the user is trying to do. The mentioned exceptions are examples to, as you wrote, “I can't think of an exception to the rule that one shouldn't start a paragraph with displayed math.”. – matj1 May 03 '23 at 14:21
  • @matj1 - You are misrepresenting what I wrote. What I did write is that LaTeX treats its users as adults, in the sense that it doesn't impose obstacles that would prevent users from modifying just about any and all typographically parameters. The default settings can be modified easily, and that's entirely fine. You appear to say that treating users as adults means imposing more restrictions. Well, that is not what LaTeX does. If users want to choose bizarre parameter settings, that's up to them. If a user wants to deliberately flout known rules of good typography, LaTeX won't stop them. – Mico May 03 '23 at 14:41
  • @matj1 - You're probably familiar with the saying "make something fool-proof, and only a fool will want to use it." Well, LaTeX is definitely not fool-proof. I will confidently venture a guess that their utter lack of fool-proofness is an important part of why TeX and LaTeX are so attractive to non-fools who care about good typography. – Mico May 03 '23 at 14:46
  • @matj1 - Sorry, I'm at a site today that's behind a firewall, and this firewall won't let me enter the chat room. – Mico May 03 '23 at 15:02
  • @Mico From the chatroom: I am trying to say that treating users like adults means imposing fewer restrictions (I guess that we agree on that), and that equations can't be at the beginnings of paragraphs is a serious restriction. If I try to have an equation at the beginning of a paragraph, TeX doesn't let me do that. So it seems like it is trying to be fool-proof here. From my previous comment: “other such tools don't the user's intent” – There is supposed to be “other such tools don't know the user's intent” – matj1 May 03 '23 at 15:08
  • @matj1 - You continue to grossly misrepresent what I wrote. Do please try to provide a proof of your claim that "If I try to have an equation at the beginning of a paragraph, TeX doesn't let me do that." Hint: You can't provide such a proof, as your claim is a serious misrepresentation of what TeX/LaTeX actually do. – Mico May 03 '23 at 15:15
  • @Mico David Carlisle wrote that TeX doesn't support starting a paragraph with a math display here. But, if TeX allows having an equation at the beginning of a paragraph, why is there so unexpectedly much space? I think that TeX's equations are built to follow on text in the same paragraph, so, if there is no such text, the spacing breaks. – matj1 May 03 '23 at 15:45
  • @matj1 - My interpretation of David's claim (and, in particular, of his use of the verb "to support") is that if a user decides to start a paragraph with a math display and thereby (deliberately or inadvertently) flouts one of the most basic rules of good math typography, then TeX and LateX will do absolutely nothing to save that user from the consequences of his/her recklessness. David's claim should certainly not be construed as saying that TeX will shut down or, at the very least, issue an error or warning message because of the user's decision to flout a typographic rule. – Mico May 03 '23 at 16:09
0

Not sure of the actual reason but I manage to fix that by placing a \vspace{0.2em}

\begin{document}
\setlength{\parskip}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}

\lipsum*[2]

\lipsum*[2]
\vspace{0.2em}
\begin{align*}1, 2, 3\end{align*}

\lipsum*[2]

\lipsum*[2]

\end{document}