5

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.

migrated from meta.tex.stackexchange.com Oct 14 '15 at 9:46

This question came from our discussion, support, and feature requests site for users of TeX, LaTeX, ConTeXt, and related typesetting systems.

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

  • 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.