4

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
danijar
  • 1,005
  • 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. – Steven B. Segletes 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. – David Carlisle 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 06: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 02:55
  • I don't understand why this question was closed as opinion-based. The question poses a clear technical problem with a code example. What is opinion-based is not the technical question but the recommendation by some users against wanting what I want to achieve. – danijar Oct 18 '20 at 02:56
  • Hi. Did you find a solution to this? I couldn't understand the answer below. Many thanks – Confounded Jan 28 '21 at 18:11
  • @Confounded Yes, the example code in my answer works. – danijar Jan 31 '21 at 09:23

1 Answers1

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

danijar
  • 1,005
  • 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. – David Carlisle May 12 '20 at 20:11
  • also if you remove the comments so your lines are empty then you get ! Missing $ inserted errors – David Carlisle 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. – David Carlisle 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 00:57
  • This is not an answer to the question as posted, it is advertised as allowing a more flexible input form ignoring blank lines but it adversely affects the layout of the whole document even if the \eq macro is not used. Try a document \documentclass{article}\begin{document}aaaaa\begin{equation} 1=1\end{equation} aaaa \end{document} You are changing the spacing around all display math. the \eq macro will still give missing $ errors in \parbox and similar constructs, and the \eq macro suppresses page breaking before the equation. – David Carlisle May 13 '20 at 07:25
  • Thanks again for pointing out these limitations. Do you have a suggestion for fixing them? – danijar May 14 '20 at 02:00
  • 1
    no: a blank line being end of paragraph is a fundamental syntax feature of the language. – David Carlisle May 14 '20 at 06:14