4

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.
  • Sorry, I saw similar questions after posting mine. But anyway. Maybe another (good) answers will be given... – profbejian Jul 5 '17 at 23:04
  • 5
    Welcome! I'm afraid this question can only elicit people's opinions and these differ widely. – cfr Jul 5 '17 at 23:04
  • 7
    The things you are asking about are really a matter of personal preference. There are good an bad practices for writing LaTeX documents, but they do not mostly have to do with the appearance of the code. (Comments, maybe, in the sense of: do comment your code. Not sure what else there might be to say.) Some people wrap to some number. Others don't. I don't like it. I just start a new line for a new sentence. This works nicely for version control. For me, that is. Not necessarily for you. What matters is how you markup and not what your markup looks like. – cfr Jul 5 '17 at 23:08
  • 4
    In addition to what @cfr says, to some extent these kinds of questions arise for other languages when writing code, and are useful for e.g. maintenance, readablilty by others etc. But most LaTeX documents (as opposed to document classes or packages) are not code (although they contain some) and unless you have multiple co-authors are read mainly by you. So the need for readability becomes even more personal, and therefore even less relevant. – Alan Munn Jul 5 '17 at 23:12
  • 1
    The answer is that there are no official guidelines! Personally I think that it would be a good thing if such guidelines were developed, especially for writing latex code/packages but, in practice, most people would ignore them. – Andrew Jul 6 '17 at 7:15
6

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

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.