[Tex/LaTex] What are the standard styles to format LaTeX source code

formattingsourcecode

In any other programming languages, we have several ways to format our source code. For example, some of the standard styles are:

  1. Allman(ANSI)
  2. K&R
  3. GNU
    ….

Within LaTeX, I couldn't find a neat way to clean my code. Most of the time, I usually make them align with each other like so:

An example from TikZ Automaton

\begin{tikzpicture}[shorten >=1pt, node distance=3cm,auto,on grid,initial text=, every state/.style={minimum size=3mm,draw=blue!50,very thick,fill=blue!20}]
            \node[state]            (q_1)                           {$q_1$}; 
            \node[state]            (q_2)   [right=of q_1]          {$q_2$}; 
            \node[state,accepting]  (q_3)   [right=of q_2]          {$q_3$};

            \path[->]
            (q_1) edge                      node             {a}        (q_2)
            (q_1) edge  [loop left]         node             {b}        (q_1)

            (q_2) edge  [bend right]        node[yshift=4mm] {a}        (q_1)
            (q_2) edge                      node             {b}        (q_3)

            (q_3) edge  [bend left]         node             {a}        (q_1)
            (q_3) edge  [loop right]        node             {b}        (q_3)
            ;       
\end{tikzpicture}

Needless to say, manually doing this is very time-consuming and tedious. Additionally, since it does not follow any rule, the format won't be consistent and it could vary from file to file. So my question is, is there a standard style for formatting .tex source code?

Best Answer

I don't think there are many standard styles of coding outside the world of C/C++ and languages with C-like syntax. In fact, the styles you named in the questions are all C styles. I cannot recall any named styles for lua, PHP, Lisp, etc. There certainly are styles widely used for many languages, but except for C/C++, you can hardly find any "style guides".

TeX, whose syntax is quite unique, does not really has a style. It is perhaps even impossible to define a style in my opinion. For example, in C, someone (I think it is Linus) once said if your code has three or more nested levels, then you should redesign it. However, for TeX, it is not uncommon for a math formula to have many levels of brackets and one needs to deal with those long formulas, i.e., how to break them into lines, where to align them, etc. It is hard to define clear rules to be applied for all situations in the TeX world.

But readability of the code is still very important: this is the reason styles were invented in the first place. In my experience, alignment is perhaps the most important tool to improve the readability of TeX code. In fact there are tools, for example the Align plugin for Vim, available to make this task easier. And indentation is just as important to TeX as it is in other languages. I just don't think it is better to define a consistent rule for all TeX code. Instead, I believe TeX is more close to human languages and requires us to improve its readability based on the context.

Related Question