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.

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?

share|improve this question

1 Answer 1

up vote 13 down vote accepted

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.

share|improve this answer
    
Thank you very much, very detailed! –  Adam Mar 20 at 0:58
1  
I once wrote an answer showing the same problem with fancyvrb: Verbatim environment with correct tab handling and special characters. –  Speravir Mar 20 at 2:07
    
@Speravir Thanks for the link. I hadn't seen that answer of yours. –  Jubobs Mar 20 at 8:52
1  
@Jubobs -- some editors have a function that replaces all tabs by the "correct" number of spaces; e.g., in emacs, it's "untabify". a lot easier (and more reliable) than search and replace. –  barbara beeton Mar 20 at 13:01
1  
@Jubobs -- you're undoubtedly right, but if a user is uncertain, it would be worth checking to see if the feature is available. if not, nothing lost. –  barbara beeton Mar 20 at 13:06

Your Answer

 
discard

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.