Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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$};

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

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?

share|improve this question
The formatting you give in the example is inconsistent itself: sometimes a space but in other cases not before square brackets, random spaces inside the options themselves, random amount of spaces (or did you use tabs?) between columns. Besides, formatting TikZ is a whole different story, opposed to formatting TeX. –  Pieter Jul 30 '11 at 7:43
Thanks for the quick response. Yes, I often use tabs between columns, but sometimes I use spaces when the line gets too long. –  Chan Jul 30 '11 at 7:46
Please let questions long enough open so that other people can post answers as well. There is no need to accept the first answer quickly. –  Martin Scharrer Jul 30 '11 at 10:23
@Martin Scharrer: I really apologize for being rush. I will wait a bit more next time. Feel free to post your answer since I think other people including me definitely appreciate it. Thank you. –  Chan Jul 31 '11 at 1:17
@Chan: Thanks. I wasn't talking about a potential answer of mine in this case but meant it as a general rule. See the Should we wait a bit before accepting answers? post on TeX - LaTeX Meta for details. –  Martin Scharrer Jul 31 '11 at 9:01
show 1 more comment

1 Answer

up vote 16 down vote accepted

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.

share|improve this answer
+1 I must agree with your answer. TeX is more close to human languages and require us to improve its readability based on the context. Thank you. –  Chan Jul 30 '11 at 9:50
I agree with readability; I am biased by programming perhaps, for me readability translates in indentation levels for sections, environments and so on. I find navigation in text much easier then. –  eudoxos Jul 30 '11 at 13:48
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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