Ignore blank lines in math mode

I have the generated LaTeX code with mathematical expressions. My problem is these generated expressions can contain some blank lines which pdflatex can't compile. Code is generated by custom application and I can't change its output. I have to handle it by some post-processing application which removed these lines or directly in LaTeX.

Code example:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
$x^{2} + \sqrt{7} - \frac{3}{7}$

$$x^{2}$$
\end{document}


So my question is: Is there a way to ignore or replace this blank lines in LaTeX? Thanks.

• Put a comment (%) symbol in these blank lines. – Augustin Nov 1 '15 at 20:22
• I have edited my post. Application is custom, I can't change its output. – user3376620 Nov 1 '15 at 20:32
• the restriction regarding blank lines in math is built into tex. it would be difficult to get around it without a change in tex, which isn;t about to happen. – barbara beeton Nov 1 '15 at 20:42
• It is possible but the generator is broken If it is maintained at all you should try to get it fixed. If you can not fix it, rather than corrupt the tex with fragile work-arounds I would post process teh generated code with perl or python or sed etc, and remove blank lines before adding to the tex document. – David Carlisle Nov 1 '15 at 21:22

You may try

\documentclass{article}
\usepackage{amsmath}
\everymath=\expandafter{\the\everymath\let\par\relax}
\everydisplay=\expandafter{\the\everydisplay\let\par\relax}

\begin{document}
$x^{2} + \sqrt{7} - \frac{3}{7}$

$$x^{2}$$
\end{document}


but this won't work for the amsmath alignment environments.

A version that seems to work also with align and gather; however, there might be unexpected effects.

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\everymath=\expandafter{\the\everymath\let\par\relax}
\everydisplay=\expandafter{\the\everydisplay\let\par\relax}

\makeatletter
\patchcmd[\long]{\collect@@body}{}{}{}{}
\patchcmd[\long]{\push@begins}{}{}{}{}
\patchcmd[\long]{\align@}{}{}{}{}
\patchcmd[\long]{\gather@}{}{}{}{}
\patchcmd[\long]{\measure@}{}{}{}{}
\patchcmd[\long]{\gmeasure@}{}{}{}{}
\makeatother

\begin{document}
\begin{align}

x^{2} + \sqrt{7} - \frac{3}{7}

\end{align}
\begin{gather}

x^{2} + \sqrt{7} - \frac{3}{7}

\end{gather}

$$x^{2}$$
\end{document}