6

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.

Bernd
  • 1,785

1 Answers1

11

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).

Bordaigorl
  • 15,135
  • 2
    You should note that, in the preamble (but not inside a macro definition), blank lines are innocuous – Steven B. Segletes Oct 14 '15 at 10:07
  • The first line is wrong really. blank lines are interpreted as the token \par in the preamble and in package files. Whether \par matters at that point is a different thing. in a package, this interpretation happens at that point not when the macro is expanded. – David Carlisle Oct 16 '15 at 10:57
  • @DavidCarlisle ah I did not know the exact mechanism with which the preable ignores empty lines. The bottom line is the same though, that empty lines are innocuous in the preamble apart from inside macro defs (I am trying to just give a rule of thumb for typical documents, of course you can always mix things up with catcodes etc) – Bordaigorl Oct 16 '15 at 11:56
  • @DavidCarlisle but the \par in macro defs in the preamble still get interpreted at the point of expansion, right? – Bordaigorl Oct 16 '15 at 11:56
  • @DavidCarlisle does that mean you suggest to avoid blank lines also in the preamble? – Bernd Oct 16 '15 at 11:59
  • 1
    @Bernd no no need to avoid them mostly but the point is there is nothing special about the preamble and package code here, the mechanism and rule is the same a blank line is \par so you may or may not have a blank line just if you may or may not have \par at that point. – David Carlisle Oct 16 '15 at 13:18
  • @Bordaigorl yes \par token is interpreted there but as worded it implied that for example if you have a blank line in apreamble macro the interpretation of that blank line would depend on for example is \obeylines was in force when the macro is used, but that isn't the case, the line ends are not recorded, there is only a \par the definition of which is not affected by \obeylines – David Carlisle Oct 16 '15 at 13:20
  • @DavidCarlisle ok I updated the answer rewording the problematic paragraph a little. Other suggestions are welcome – Bordaigorl Oct 16 '15 at 14:48