# How can I remove empty lines from the content of an environment?

I'm using this equation command to reduce typing when creating aligned equation blocks. I would like to use empty lines within this command to help structure my LaTeX source. Currently, those cause an error because display equation environments don't support them. How can I remove all empty lines from #1, but keep the \\, before forwarding the content to the amsmath environment?

Minimal example:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\eq}[1]{\begin{aligned}#1\end{aligned}}
\makeatother

\begin{document}
\eq{
z &= f(y) \\
y &= f(x) \\

x &= 5
}
\end{document}


Error message:

Runaway argument?

\begin {aligned} z &= f(y) \\ y &= f(x) \\
! Paragraph ended before \gather was complete.
\par
l.14 }

I suspect you've forgotten a }', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

• All math display ens does not support blank lines. And btw don't do this it is waste of your time using it as a macro. If you what to combine gather and aligned do it as an environment, use it as a macro makes the code much much harder to read and to work with. Trust me, ice been there and learned my lesson. Instead look into how your editor can save you time when typing. Apr 4 '20 at 16:55
• @daleif Thanks for your comment. I'm aware that the math display environments don't support blank lines. That's why I want to remove them from the input argument before passing the content into the amsmath environment. I appreciate your advice but I've been using the above macro for a while now and it has saved me a lot of typing in documents with many display equations. Readability can be important, but it's also a trade-off. Apr 4 '20 at 17:09
• \noindent\begin{gather} is wrong and will generate poor output in most cases. You have not shown how you are using this but there should not be a blank line before display math. Apr 4 '20 at 17:09
• The \eq environment above simply lets me put empty lines before and after the environment in the TeX source without them causing paragraphs in the output PDF. This lets me structure my document with empty lines and quickly move around using Vim's [ and ]. I appreciate the advice, although I think this is not directly relevant for my question. Apr 4 '20 at 17:12
• This isn't automatic, but instead of a blank line, you can put a line containing only a %. That gives a similar open visual effect without the \par problem. Apr 4 '20 at 18:14

I recommend not doing this but you can put a normal space at the ends of lines instead of ^M so it never generates a \par.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\eq}{\begingroup\endlinechar=32 \eqx}
\newcommand{\eqx}[1]{\endlinechar=32 \begin{gather}\begin{aligned}#1\end{aligned}\end{gather}\endgroup}
\makeatother

\begin{document}
\eq{
z &= f(y) \\
y &= f(x) \\

x &= 5
}
\end{document}
`