[Tex/LaTex] whitespace/brace/indentation conventions for LaTeX programming

bracesindentationprogramming

This question is not about formatting the text/markup of a document. I'm really focused on the programming aspect of LaTeX. I'm working on a fairly large set of document classes and packages, and I'm wondering what's conventional for bracing and indentation.

Looking at the latex.ltx file, I see some groups which have their closing braces as the end of the last line, e.g.,:

\def\vgl@{\par \dimen@\prevdepth \hrule \@height\z@
  \nobreak\vskip\skip@ \prevdepth\dimen@}

Other times the closing brace is in the next line, as in

\def\ltx@sh@ft #1{%
  \dimen@ #1%
  \kern \strip@pt
    \fontdimen1\font \dimen@
  } % kern by #1 times the current slant

I suppose the latter style could be called K&R. I don't know if the former has enough adherents in the C world to have a name.

As of this morning I'm considering K&R style braces with indents of four spaces. latex.ltx and other sources I've read use two, but four seems a bit more readable. Extra newlines means one has to be careful about extra spaces and \pars being added, but for purposes of readability I like the space.

I'm interested to hear what other conventions LaTeX programmers have used and come to like.

Best Answer

I'll reply to this question in more detail when I've got a few more minutes, but my currently favored approach for LaTeX2e programming is to always have the closing brace on it's own line (and two spaces indentation, since sometimes it stacks up a lot). E.g.,

\def\foo{%
  \bar{abc}%
}

The more inline style, as in your first example, I used to use religiously, but I've since found it makes re-reading code that little bit harder (especially when the closing braces stack up several deep).

I strongly dislike and discourage the form of your first example where multiple commands are placed on the same line. When people say that TeX programming is hard to read, these are the examples they use.

In LaTeX3 programming, I have a few more rules of thumb, but they'll have to wait for another day.

Related Question