TransWikia.com

How can I ignore empty lines before, after, and inside an equation environment?

TeX - LaTeX Asked by danijar on March 7, 2021

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

One Answer

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 @gobblepar, 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}

xapptocmdnormalsize{%
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}{ifvmodevspace*{-baselineskip}setlength{parskip}{0ex}fi}
newcommand{removeParAfter}{@ifnextcharpar@gobblerelax}
newcommand{eq}{begingroupremoveParBeforeendlinechar=32 eqinner}
newcommand{eqinner}[1]{endlinechar=32%
begin{equation}begin{aligned}#1end{aligned}end{equation}endgroupremoveParAfter}
makeatother

begin{document}

lipsum[2]

eq{

x &= y 


y &= z

}


lipsum[2]

end{document}

equation environment with removed spaces

Correct answer by danijar on March 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP