1

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

Mathematica graphics

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

Mathematica graphics

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.

marked as duplicate by David Carlisle, Nasser, Community Mar 7 '18 at 23:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If you ignore empty lines, how will you indicate breaks between paragraphs? Explicitly with \par? – ShreevatsaR Mar 7 '18 at 23:45
  • not \let\endlinechar\32 !!! that makes \endlinechar be 3 and typesets 2 (and breaks latex in lots of places) it is a register so \endlinechar=32 – David Carlisle Mar 7 '18 at 23:49
  • @ShreevatsaR Yes. The software I use, has tag to insert for each section, subsection, etc... so I add new say section and start typing. When I want new subsection, or new paragraph, I click on button to add that. – Nasser Mar 7 '18 at 23:49
  • @Nasser but better would be to use an editor that does not write broken latex – David Carlisle Mar 7 '18 at 23:49
  • 1
    I was just typing an alternative answer :-) Will type it here: you can \usepackage{luacode} and after all other packages have been loaded (e.g. just before/after \begin{document}) do: \begin{luacode} luatexbase.add_to_callback('process_input_buffer', function(line) if line == '' then return '%' .. line end return line end, 'Remove or ignore blank lines.') \end{luacode} – ShreevatsaR Mar 8 '18 at 0:02

Browse other questions tagged or ask your own question.