# Adding extra lines/spaces in source, *not* affecting semantics

I'm editing a file with quite huge interleaves of text and formulas like this

lorem ipsums
\begin{align}
foo & bar\\
qux & rofl\\
& toto
\end{align}
lorem ipsums
…


And I face a problem when I want my editor to indent/wrap the text of the lorem ipsums: it will think that a paragraph goes until the next blank line, which means that it will format the whole thing into a blurb like the following:

lorem ipsums \begin{align} foo & bar\\ qux & rofl\\ & toto \end{align} lorem ipsums …


which, you'll agree, is a lot less nice to read and edit.

I wonder if there is a way round this, so that I can insert lines around my formulas, without changing the semantics?

Maybe something like

lorem ipsums\slurp

\begin{align}
foo & bar\\
qux & rofl\\
& toto
\end{align}
\slurp

lorem ipsums
…


Cheers.

-
I would suggest the best way to deal with this problem would be editor specific, rather than trying to gobble empty lines within tex. What editor are you using? –  Seamus Aug 3 '11 at 10:33
Ah. Just spotted the vim tag. Are you using the LaTeX suite? I'm surprised this doesn't deal with this properly already. –  Seamus Aug 3 '11 at 10:33
No, I'm not using LaTeX suite. I just use plugins not specific to TeX (like snipmate) and the conceal mode of vim. And also a plugin I wrote myself for real-time compilation and display. –  Axioplase Aug 4 '11 at 9:27

If your editor knows about comments, then the simplest method is to put a comment character on the blank lines:

lorem ipsums
%
\begin{align}
foo & bar\\
qux & rofl\\
& toto
\end{align}
%
lorem ipsums


But this will only work if your editor knows not to break up comment lines.

Update: Although I think that the real answer is: use a formatter that's LaTeX-aware, here's three answers to the actual question. This may not be very elegant, and it may break something elsewhere (I haven't tested it much).

\documentclass{article}
% \url{http://tex.stackexchange.com/q/24786/86}
\let\savepar=\par
\def\restorepar{\let\par=\savepar}
\def\nopar{\let\par=\restorepar}

\long\def\igpar#1{}

% Equivalent to the above is:
%\makeatletter
%\let\igpar=\@gobble
%\makeatother

\makeatletter
\def\eatpar{\@ifnextchar\par{\@gobble}{}}
\makeatother

\begin{document}

This is a paragraph.

This is the next paragraph.
This should be part of the previous one.

This is a paragraph.

This is the next paragraph.
\nopar

This should be part of the previous one.

This is a paragraph.

This is the next paragraph.
\igpar

This should be part of the previous one.

This is a paragraph.

This is the next paragraph.
\eatpar

This should be part of the previous one.

\end{document}


I recommend that these be on their own on a line. Not only does that make it easier to remove them later, but also means that the spacing at the end of the previous line is preserved.

The first (\nopar) works by redefining \par so that the next occurrence of \par is effectively ignored. So that it only works once, the next occurrence of \par also resets itself back to \par so that the next one will work as before. The \nopar can actually occur anywhere, not just before the blank line.

The second (\igpar) simply gobbles its next argument. As it is \long, it can gobble a \par with impunity.

The third (\eatpar) tests the next token to see if it is a \par or not. If it is, it gobbles it. If not, it leaves it alone. This is probably the most robust.

All of them work because of how TeX treats blank lines. When the input processor of TeX sees a blank line, it inserts a \par token in to the stream. This means that the blank line gets replaced by a \par before any macro processing is done. So in the internals of TeX, we can test for blank lines by testing against \pars. Otherwise, we'd have to test for the newline character, and test to see if we got two in a row.

-
An editor smart enough not to break comments should be smart enough not to collect environments into a paragraph... –  Seamus Aug 3 '11 at 10:42
@Seamus: True, but if the editor doesn't have a specific LaTeX mode but can cope with code then this would be a halfway house. (But I hadn't taken note of the [vim] tag, otherwise I probably wouldn't have posted.) –  Loop Space Aug 3 '11 at 11:57
Inserting comments quite doesn't work, as it will still them make a mess of everything inside the environment; vim behaves as "do not format what's before and after the comment together, but don't stop formatting, keep going as far as you can." –  Axioplase Aug 4 '11 at 9:31
@Axioplase: Not being a vim-user, I didn't know that. So you really do need a blank line. –  Loop Space Aug 4 '11 at 9:41
@Axioplase: I've updated my answer to provide a possible solution. –  Loop Space Aug 4 '11 at 9:56

Consider \gobblepars:

\makeatletter
\newcommand\gobblepars{%
\@ifnextchar\par%
{\expandafter\gobblepars\@gobble}%
{}}
\makeatother


Notice that the recursive definition allows it to gobble more than just one par. So you should be able to get away with...

One paragraph. \gobblepars

More of that one paragraph.


Just make sure to put a space before the \gobblepars. Alternatively, you could modify \gobblepars to insert a space when it's done, like this:

\makeatletter
\newcommand\gobblepars{%
\@ifnextchar\par%
{\expandafter\gobblepars\@gobble}%
{ }}
\makeatother


[ background: I originally posted about \gobblepars elsewhere... ]

-