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.

6
  • 3
    A blank line is a paragraph break. If you don't want to have a paragraph break, escape the blank line with the percent sign. Commented Oct 14, 2015 at 9:56
  • 2
    Never, ever, put a blank line before a displayed equation. Commented Oct 16, 2015 at 10:58
  • @DavidCarlisle <noob_mode>could you please briefly explain why?</noob_mode> Commented Aug 16, 2016 at 22:32
  • 1
    @ScumCoder Have an answer about that somewhere I'll see if I can find it Commented Aug 16, 2016 at 23:02
  • 1
    tex.stackexchange.com/questions/309108/… or tex.stackexchange.com/questions/132965/… @ScumCoder Commented Aug 16, 2016 at 23:24

1 Answer 1

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

8
  • 2
    You should note that, in the preamble (but not inside a macro definition), blank lines are innocuous Commented Oct 14, 2015 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. Commented Oct 16, 2015 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) Commented Oct 16, 2015 at 11:56
  • @DavidCarlisle but the \par in macro defs in the preamble still get interpreted at the point of expansion, right? Commented Oct 16, 2015 at 11:56
  • 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. Commented Oct 16, 2015 at 13:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.