# Tabs versus spaces for indenting TeX documents

The title tells it all. What is a "correct" way to indent TeX documents?

Different programming languages have different conventions. Python recommends 4 spaces. Your Makefile will not even work if you do not use tab for indenting. Generally normalized (Allman style) BSD C code is indented with tabs of width 8.

Being an OpenBSD user I tend to use tab (set to 8 characters) to indent all my code including TeX except Python. However due to the fact that I use NVI with displayed line numbers the effective width of my terminal is only 72 characters which, coupled with 8 character indentation, makes some of my TeX documents difficult to read.

On another hand quick look at my

``````/usr/local/share/texmf-dist/tex/latex
``````

directory reveals that LaTeX style files are indented with two spaces. Is that true in general? Should I use the same convention for LaTeX documents too?

-
Possibly related: tex.stackexchange.com/questions/3866/… –  Matthew Leingang Jan 23 '12 at 4:33

What is a "correct" way to indent TeX documents?

I believe there is no "correct" way -- certainly no single correct way -- to indent input lines of an ordinary TeX or LaTeX document. (The situation may be different for input that adheres to LaTeX3 coding methods, but I'm not sure of that.) As long as TeX can parse the input, it's completely agnostic as to the form of the input it receives. Any indentation practices -- whether indenting with "soft tabs" or "hard" spaces, and whether the indentation amounts to 2, 4, or 8 spaces -- are therefore entirely cosmetic and voluntary in nature. To be sure, I don't believe anyone in their right mind would advocate not using some rule for indenting parts of one's TeX code.

A separate thought: In order to raise the readability of TeX input code, a technique that is probably as important as (or possibly even more important than) adopting some rule for indentation is using vertical whitespace liberally and consistently -- between paragraphs, above a sectioning command, before and after a `\newenvironment` statement, before and after a LaTeX `float`, etc.

-

From a personal point of view, this may be very subjective. Some use `<TAB>`s, others use `<space>`s, and any combination of them. I think you might find very different styles of indentation if you start looking a different authors's `.sty` files on CTAN. My feeling is that the indentation should reflect nesting horizontally, and perhaps grouping vertically. In both instances this defines some structure within the code.

From TeX's point of view, this is irrelevant in most cases. That is, unless the category codes have changed. In particular, the `verbatim` environment, where spaces are treated as spaces; even consecutive ones.

From the TeX Book (Chapter 8: The Characters You Type, p 46 onward):

The input to TeX is a sequence of "lines." Whenever TeX is reading a line of text from a file, or a line of text that you entered directly on your terminal, the computer's reading apparatus is in one of three so-called states:

• State `N`: Beginning a new line;
• State `M`: Middle of a line;
• State `S`: Skipping blanks.

... If TeX sees an end-of-line character (category 5), it throws away any other information that might remain on the current line. Then if TeX is in state `N` (new line), the end-of-line character is converted to the control sequence token `\par` (end of paragraph); if TeX is in state `M` (mid-line), the end-of-line character is converted to a token for character 32 of category 10 (`<space>`); and if TeX is in state `S` (skipping blanks), the end-of-line character is simply dropped.

... If TeX sees a character of category 10 (`<space>`), the action depends on the current state. If TeX is in state `N` or `S`, the character is simply passed by, and TeX remains in the same state. Otherwise TeX is in state `M`; the character is converted to a token of category 10 whose character code is 32, and TeX enters state `S`. The character code in a space token is always 32.

The last two paragraphs are most telling of how TeX handles vertical and horizontal white space (respectively) within the code, allowing one to tailor your code to your own liking pretty much. Literally, TeX is just "skipping blanks".

-