# how to tell Latex to ignore empty lines [duplicate]

This question already has an answer here:

This is a problem I had for long time. I use software to generate Latex (SW). It adds lots of empty lines. Which ends up using extra white space between a text and the displayed equation that follows.

I've always fixed this by manually removing these empty lines once I am done editing the document. But this is time consuming to do each time.

Is there some command one can add to the preamble only to tell Latex to automatically ignore empty lines, or treat them as if they had % as first character?

I will first show a MWE that shows the problem and then show what I tried after that. Compare the output of this file

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[grid]{eso-pic}
\setlength\parindent{0pt}
\begin{document}
line 1

\begin{align*}
x &= y
\end{align*}

line 2
\end{document}

With this file

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[grid]{eso-pic}
\setlength\parindent{0pt}
\begin{document}
line 1
%
\begin{align*}
x &= y
\end{align*}
%
line 2
\end{document}

Here it is side by side

The left image is for the first file above. You can see it has extra space before the \begin{align} and after \end{align} which I do not like.

I'd like to obtain same output as the right side image (less space), but without having to manually edit the latex file each time by either adding % or to delete the blank lines.

This is what I tried. I saw this question how-to-remove-blank-lines-around-equations but this requires one to manually edit the Latex file and add commands after preamble.

I saw this how-to-disable-new-paragraph-at-empty-line and I tried the solution there which says

Redefine \par, for example \let\par\relax

But that broke many things in my main build and I was getting many latex errors. The second solution there which says

Redefine \endlinechar to be 32

This is what I tried.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[grid]{eso-pic}
\usepackage[subtle]{savetrees}
\setlength\parindent{0pt}
\endlinechar=32
\begin{document}
line 1

\begin{align*}
x &= y
\end{align*}

line 2
\end{document}

update: I need to try the above again on my main build. I had written it wrong before. Will update soon to see if it worked.

I also tried the package \usepackage[moderate]{savetrees} and this is the result

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[grid]{eso-pic}
\usepackage[moderate]{savetrees}
\setlength\parindent{0pt}
\begin{document}
line 1

\begin{align*}
x &= y
\end{align*}

line 2
\end{document}

Here is side by side

So \usepackage[moderate]{savetrees} did what I want for this example. But when I tried it on large document, I found it compresses things too much in many other places and made line too close to each others. So I did not like the effect. When I tried \usepackage[subtle]{savetrees}, the space was not removed any more.

So I am not sure what to try next.

I asking if there is some other solution to try. Again, I want all empty lines to be treated as %. But by modifying the preamble only. Not the internals of the document, as the document itself is auto-generated.

Using texlive 2017. I use lualatex to compile latex file.