[Tex/LaTex] Good practice rules for writing clean LaTeX

best practices

Are there good practice rules for writing clean LaTeX? Something like PEP8 for Python. Examples:

  • spaces or tabs (not a troll) ;
  • how many columns before wrapping (80 for PEP8 compliant Python) ;
  • something about comments ;
  • etc.

Best Answer

The short answer here is simple: 'No, there are no rules akin to PEP8 or similar'.

For a longer answer, it's perhaps worth dividing up LaTeX sources into two types:

  • Documents (the majority of work for most people)
  • Packages and other 'code'

Of course, there is some overlap as one may need to 'quickly do some coding' in a preamble, but the 'nature' of a source file is usually broadly one or the other case.

For documents, the number of authors who will ever see the source is small: the key target is the typeset output. Thus it really is down to the individual(s) involved as to how they lay out their input.

For 'code', one might argue for slightly different outcomes as there is at least the potential for wider reading/reuse of sources. However, practical experience suggests that most packages have only one person actually writing the code, even for widely-used material. Where there are exceptions, most obviously the kernel but also things like beamer or biblatex, the 'team size' tends to be small and informal agreement is normally possible. (Usually one person has started things off so some position can be agreed based on sticking to whatever they've done.) Here of course we might use the .dtx format for the code, which would lead to very different 'comment' outcomes to cases where the source is the package (contrast the kernel with etoolbox, for example).

The one place there is a style guide for code is for expl3: the team have written one based on the way the code has tended towards. Even there, though, there is no sense that code not following these rules is 'bad': it's much more about the content.

It's important to remember that TeX is a macro expansion language, and that makes automatic 'checkers' difficult-to-impossible to implement: see Automatic style guide for LaTeX?.