[Tex/LaTex] Empty/blank lines in LaTeX code

best practicestex-core

I hope this question is in order here although it rather asks for a "best practice" than for some "solution".

I am wondering to what extent blank/empty lines in the code (I am using pdflatex) should be avoided and e.g. be replaced by %. My source code is full of single blank lines e.g. before the start of a new section, between math/text environments in order to increase the readability of the code.

Are such blank lines going to be ignored anyway (I guess this might depend on the specific instance?)/only cause minor vertical space in the PDF, or are they deemed "bad practice" and should be avoided?

All I could find was this answer according to which – I take it – blank lines should mostly be avoided apart from before tabular and array environments.

Best Answer

In package/class files and in your preamble, paragraph breaks do not have any special meaning so empty lines are innocuous. Use them generously to structure your code. Important caveat: even in the preamble, when you define a macro the newlines inside the definition will be interpreted in the context of their expansion so if you are going to use them in the body, the rules below apply. When catcodes are changed (for example by use of \obeylines) there are no general rules and you need to examine the active character's definitions in that context.

In the body of the document some care should be taken. As mentioned in the comments, an empty line, in the body, is a paragraph break. You should use them to mark the end of paragraphs.

Note however, that consecutive multiple empty lines constitute a single paragraph break and thus they can be used to separate portions of code without inserting extra space apart from that single break.

Some commands, such as sectioning commands, already take care of spacing and insert paragraphs breaks when necessary before the section, so empty lines preceding them will not harm.

Other commands/environments like equations, itemizes or tables, instead are sensitive to the presence of paragraph breaks around them and you should put some care in deciding whether you want that break or not. In case you don't but want to keep some vertical space in code, you can always escape the line with %.

In math mode white space is generally discarded but empty line still generate paragraph breaks which are not allowed, so you should always escape them.

There are similar issues within some environments as tabular, where paragraph breaks are not allowed without extra care (e.g. by using \parbox).

Related Question