2

I would like an equation environment that doesn't care about empty lines. Normally, empty lines before and after equation start a new paragraph, resulting in additional vertical spacing around the display equation. Empty lines inside the equation environment cause a compilation error:

Text above
                    % <- Empty line causes additional spacing
\begin{equation}
\begin{aligned}
                    % <- Empty line causes error
x &= y \\
                    % <- Empty line causes error
y &= z
                    % <- Empty line causes error
\end{aligned}
\end{equation}
                    % <- Empty line causes additional spacing
Text below
8
  • 9
    Perhaps you use blank lines to visually arrange and organize your document input. If so, just get in the habit of placing a lone % on the otherwise blank lines. It looks virtually the same, and you don't need to modify LaTeX at all. May 12 '20 at 18:57
  • Unfortunately, that conflicts with paragraph motion in Vim. It's also more work. That said, I agree that this is the recommended solution for the majority of users.
    – danijar
    May 12 '20 at 18:58
  • 1
    you have not provided a test document and your fragment has no blank lines that would generate an erroneous \par as they are all commented out. May 12 '20 at 20:16
  • 2
    It is not smart idea adapt LaTeX to specific of your editor. By this you make problems to others who use other LateX editors.
    – Zarko
    Oct 10 '20 at 6:16
  • @Zarko I think there is a misunderstanding here. This question is not specific to an editor. I edit LaTeX source often and don't want to spend additional time maintaining empty comments as a workaround for supporting empty lines, e.g. inside equation environments. This will also not cause problems to other editors.
    – danijar
    Oct 18 '20 at 2:55
1

Solution:

  • Empty lines before. Once LateX processed a \par it can't be removed. Instead, we check if we're at the beginning of a new paragraph with \ifvmode and then set parskip to zero and compensate for the empty line above with negative space.

  • Empty lines after. We can prevent the \par after an environment from being written using \@gobble\par, which ignores both spaces and a line break that might follow the equation environment. For more details, see this answer.

  • Empty lines inside. To hide empty lines from amsmath, we change \endlinechar to a space character. This should not affect the equation otherwise, because spaces are ignored in math mode. I'm unsure why a nested command is needed but it does work.

Example:

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

\xapptocmd\normalsize{%
\abovedisplayskip=.8em plus .2em minus .2em
\belowdisplayskip=.6em plus .1em minus .1em
\abovedisplayshortskip=.8em plus .2em minus .2em
\belowdisplayshortskip=.6em plus .1em minus .1em
}{}{}

\makeatletter
\newcommand{\removeParBefore}{\ifvmode\vspace*{-\baselineskip}\setlength{\parskip}{0ex}\fi}
\newcommand{\removeParAfter}{\@ifnextchar\par\@gobble\relax}
\newcommand{\eq}{\begingroup\removeParBefore\endlinechar=32 \eqinner}
\newcommand{\eqinner}[1]{\endlinechar=32%
\begin{equation}\begin{aligned}#1\end{aligned}\end{equation}\endgroup\removeParAfter}
\makeatother

\begin{document}

\lipsum[2]

\eq{

x &= y \\


y &= z

}


\lipsum[2]

\end{document}

equation environment with removed spaces

7
  • 2
    I'm sorry but this does not work. You had not provided an example so i added one. Note that this is using shortdisplayskip even though the final line of the text is long so the spacing at the top is wrong. Also unlike a blank line before or in an equation, a blank line after an equation is not an error it is the mechanism to mark the following text as starting a paragraph, which isn't possible if you remove the \par unless you use unnatural markup like \par\par so only one is removed. May 12 '20 at 20:11
  • also if you remove the comments so your lines are empty then you get ! Missing $ inserted errors May 12 '20 at 20:14
  • it also sets \parskip to 0pt for the rest of the document, even if it was non-zero initially. May 12 '20 at 21:00
  • Thanks for pointing out these things and improving the example. It should all be working now. By the way, I would say that none of the default behavior here is an error on the side of LaTeX. It was just an inconvenience to me.
    – danijar
    May 13 '20 at 0:57
  • 1
    no: a blank line being end of paragraph is a fundamental syntax feature of the language. May 14 '20 at 6:14

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.