11

The following code does not compile:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{align}

    \eta_{m} = & \omega_{m1}D_{1} + \omega_{m2}D_{2} + \dots + \omega_{mT}D_{T} \\
    \eta_{a} = & \omega_{a1}D_{1} + \omega_{a2}D_{2} + \dots + \omega_{aT}D_{T}

\end{align}

\end{document}

However, if I remove empty lines, it does. (Same with equation environment). Why? Why would it be problematic for a code to allow spaces? Convention?

For reference, the error in TexStudio is quite dramatic, for an apparent trivial issue:

line 7: Paragraph ended before \align was complete.
line 7: Missing $ inserted.
line 7: Missing \endgroup inserted.
line 7: Display math should end with $$.
line 8: Misplaced alignment tab character &. &
line 8: Missing $ inserted. & \omega
line 9: Misplaced alignment tab character &. &
line 10: Missing $ inserted.
line 11: Misplaced \cr. \end{align}
line 11: Misplaced \noalign. \end{align}
line 11: Misplaced \noalign. \end{align}
line 11: Extra }, or forgotten \endgroup. \end{align}
line 11: Missing $ inserted. \end{align}
line 11: Display math should end with $$. \end{align}

NOTE: I am aware of this, this, and this question. But none of them explains why empty lines are problematic (besides a trivial and rather tautological answer like "the code does not allow it").

  • 4
    It's 'by design': one can argue with the design decision but I'm not sure if that is really an objective question. – Joseph Wright Sep 9 '16 at 12:17
  • 4
    You might think this is more readable in some language say Python but it is not readable in TeX. Because empty lines designate something very important in typesetting which is a paragraph. And you can't just throw them around. A similar question in Python would be why would indentation is so strict in Python? etc, – percusse Sep 9 '16 at 12:38
  • 1
    Thanks for the question, which led to answers explaining something I'd always wondered about. I too would like to allow blank lines in display math environments, for readability in the editor. I've learned to do without them, and to recognize that error for what it is when I forget and leave a blank line. I may be less likely to do that now. – Ethan Bolker Sep 9 '16 at 12:53
  • 1
    @EthanBolker if you are tempted to leave blank lines in math "for readability" but just do not because of the error there is a worry that you add blank lines in other places for similar readability reasons but are not saved by the error message, for example blank lines before display math don't generate an error but always produce poor output. blank lines before or after a list should not be used for cosmetic reasons, they denote a paragraph break and affect the vertical space around the list and the indentation of the following text, etc... – David Carlisle Sep 9 '16 at 13:16
  • 1
    @EthanBolker I think lots of people do that, but myself I think it is clearer not to add something, than to add something that you know is wrong, and then comment it out:-) – David Carlisle Sep 9 '16 at 13:22
12

The error message tells exactly why it is problematic:

Paragraph ended before \align was complete.

In standard (La)TeX, an empty line signifies a paragraph break.

Since it does not make sense to start a new paragraph inside a single math display, the display math environments were not implemented with paragraph breaks in mind (a simplification, but we might think of this in this way: they are not "long" environments).

Because these environments were not created allowing paragraph breaks, when TeX encounters an empty line (a paragraph break with a "standard" catcode setup), it assumes that the user forgot to close the environment, closes it (thereby leaving display math), starts a new paragraph, and moves happily along.

Then it finds all these other math-mode tokens, but we are no longer in math mode because of the above paragraph. So that is the cause of all the errors following the first one.


As requested, elaborating a bit on the following statement:

[I]t does not make sense to start a new paragraph inside a single math display[.]

Depending on one's chosen/preferred style, a displayed equation or group of equations is either

  • part of a single sentence unit, or
  • a standalone unit roughly equivalent to a sentence.

The former case is most common in mathematical and technical writing. Here, the equation(s) fit in with the surrounding wording and include punctuation so they can be read continuously with the surrounding text. In this case, we would not start a new paragraph within an equation any more than we would start a new paragraph within a single sentence.

In the latter case, which is less common, a single equation could be its own paragraph, but it is more likely to have some text sentences around it introducing it or explaining it, so the displayed equation is usually "attached" to some body copy, so it belongs to the paragraph of that body copy. (Just as a single sentence could be its own paragraph, but it is more common for multiple sentences to make up a paragraph.) Either way, again, we would not start a new paragraph within an equation any more than we would start a new paragraph within a single sentence.

For groups of equations in a single display environment, the very fact that they are logically grouped means they are related and part of the same "idea" in some way. Just as we group sentences together to form paragraphs, equations grouped in this way should belong to the same "idea" or paragraph. If you really intend to start a new "idea" or paragraph within a group of equations, I would argue that the group of equations should be split at this point rather than trying to introduce a paragraph break inside the group of equations.

Hopefully this helps to explain why I said that it doesn't make sense to start a new paragraph inside a math display environment.

14

By design latex tries to give commands a consistent syntax, the environment syntax in particular here makes it clear what is intended to be in math, and what is not.

However the error message comes from the tex engine itself if \par is seen in math mode. To prevent this latex would have to prevent blank lines generating a \par token (which is tricky to do in all contexts) or locally redefine \par to do nothing (which is what it does to ignore blank lines between table rows) However it doesn't.

The reason for the error is related to the primitive syntax for math $ and $$ which doesn't have a clear begin/end distinction, and to the error recovery of adding $ when a math construct is used in text.

If (as posted here every other day) you go

blah blah foo_bar  blah ...

then on seeing the _ Tex inserts a $ to get into math mode. After that it would be quite likely the entire document was gobbled up in one line, with no spaces. The error trap on end of paragraph "limits the damage" allowing TeX to force an end to math mode at the end of the paragraph and re-start the next paragraph in text mode and so reduce the number of spurious errors reported.

Whether or not this is still useful with modern editor workflows is open to debate but it is certainly an intentional design feature.

  • So then whenever I have an empty line in a text editor, Tex sees it as a \par command? In other words, a tex file is basically a single line document with a lot of \par in between? – luchonacho Sep 9 '16 at 13:48
  • 2
    @luchonacho you can change the interpretation (eg verbatim environment) but the default parsing is that a single newline is reported as a space token, and two or more consecutive newlines are reported as a space token followed by \par try \renewcommand\par{hello\endgraf} and then add a few blank lines here and there:-) – David Carlisle Sep 9 '16 at 14:13

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.