[Tex/LaTex] Tabs vs spaces in listings: what’s the difference and should I care

listingsspacing

This is a follow-up question to Problem with Listings package concerning line breaking and wrong alignment. Why do I experience problems when I copy and paste code inside the lstlisting environment? Should I be careful about tabs or spaces in listings? And if yes, then why is that? How exactly do tabs and spaces affect horizontal spacing in source code typeset with the listings package?

Best Answer

There are other invisible characters than the space character (ASCII code 32); one of them is the tabulator, a.k.a. tab character (ASCII code 9), which are used in some types of source files; most notably, perhaps, in makefiles. For more information about invisible characters in general, I refer you to the Wikipedia page on ASCII control characters.

The listings package treats space characters and tabulators very differently, as explained in subsection 2.5 of the listings documentation.

You might get unexpected output if your sources contain tabulators. The package assumes tabulator stops at columns 9, 17, 25, 33, and so on. This is predefined via tabsize=8. If you change the eight to the number n, you will get tabulator stops at columns n+1,2n+1,3n+1, and so on.

If you don't want any surprises, and if your code could do with just spaces and no tabulators, sticking to spaces and eschewing tabulators entirely is probably a good idea.

I'm guessing you're using a LaTeX IDE, such as texmaker, as opposed to a text editor, such as emacs or vim. Your IDE most likely inserts a tab character when the Tab key is pressed by default, but there should be a way to configure it to insert a fixed number of spaces (typically, 2,4, or 8) instead. Such an option may not be retroactive, though; in other words, tab characters that were already present in your file may not be replaced by spaces simply by activating that option. You may have to search and replace tabs by spaces to get rid of all of them. This arguably is error-prone but you may not have any other option, if you use an IDE. However...

Edit (following Barbara Beeton's comment): ... editors such as emacs and vim have a built-in function to efficiently replace all tabs by spaces in the current buffer. In emacs, you have untabify; in vim, you have retab (see this).

enter image description here

Good text editors also allow you to make invisible characters (tabulators, newlines, etc.) visible, so you know what type of invisible character you're actually dealing with. In the picture above (screenshot of a vim buffer), the symbol denotes a tabulator and the ¬ symbol denotes a newline.

That's two more reasons to leave your IDE behind and start using a text editor :)

Update: texmaker 4.2 (2014/05/01) now allows advanced users to extend the IDE's functionalities by running custom Javascript code from within. This opens the door to a "retab" script, but assumes you know a modicum of Javascript.