Does the tab key do anything?

Let's look at latex.ltx, where we find

277 \catcode`\{=1 % left brace is begin-group character
278 \catcode`\}=2 % right brace is end-group character
279 \catcode`\$=3 % dollar sign is math shift
280 \catcode`\&=4 % ampersand is alignment tab
281 \catcode`\#=6 % hash mark is macro parameter character
282 \catcode`\^=7 % circumflex and uparrow are for superscripts
283 \catcode`\_=8 % underline and downarrow are for subscripts
284 \catcode`\^^I=10 % ascii tab is a blank space

This is essentially taken unchanged from the start of plain.tex and the last line is what is of interest for the question at hand.

It says that the ASCII character number 9 is assigned category code 10. Wait, where is number 9? According to TeX conventions, ^^I is the same as typing the character that differ by 64 from I (plus or minus, depending on which gives a code point between 0 and 127). Since I is position 73 in ASCII and 73+64=137>127, we do 73-64=9. According to TeX's rules, the concoction

\catcode`\^^I=10

is the same as typing \catcode 9=10.

Category code 10 means “blank space” and, basically, all characters having this category code behaves the same. So the <tab> character is ignored at the beginning of lines and after control words, but produces a blank space in the same contexts where a normal space (character code 32) would.

Of course, one can change the category code so the character behaves differently. For instance,

\documentclass{article}
\catcode`\^^I=12

\begin{document}
a   b
\end{document}

(between a and b there's a <tab> character, which might be lost in copy-paste in this site) would produce

enter image description here

because the Computer Modern font in OT1 encoding has Ψ in slot 9.

However, if category codes are not changed, <tab> is the same as a space (or a run of spaces, because of standard TeX rules).


What the <tab> character does in the editor window is another matter. For instance in my settings for Aquamacs (an Emacs variant for Mac OS X) the <tab> character does exactly nothing. Some editors might produce a run of spaces, others a real <tab> characters, others may use it for moving to marked points.


The effect of the tab key is entirely dependent on how the OS interprets that key, how the editor treats the result of the OS's interpretation and, finally, how TeX interprets the result of the editor's treatment in the particular context.

On my machine, Kile is currently configured so that pressing the tab key inserts spaces if I am editing a file with the mode set to LaTeX. By default, that's currently true for all .tex files, for example, and some other source files.

Until recently, I had Kile configured to insert a tab character in this kind of case. However, this turns out to cause various issues, so I altered the configuration.

There is nothing sacrosanct about the connection between the key labelled tab and the insertion of a tab character. And the editor may well transform the input further whether it is a tab character or not.

This point might seem pedantic. But it is not. Because if pressing the tab key influences the typeset document in a way you don't like, you have options.

One is to quit using the key in problematic places. I could have resolved the issues caused by tab characters in this way: I could have used the space bar instead. But that would be a pain. I want a particular number of spaces in particular places. I don't want to have to insert the correct number in the correct place.

But that's not the only option. The other option is to alter the effect of the key on the source code.

This can be done at the level of the OS/desktop environment/GUI or terminal configuration. This is a good choice if, like me, you find CapsLock a pain because you don't realise you've switched it on. You can turn CapsLock into, say, another Ctrl key or use it for character composition.

Or it can be done by the editor, which is a better choice for this kind of case. Not only does it avoid altering the effect of the key in applications not related to TeX. It also permits much more fine-grained control. You might want the key to do something different in .bib files, say. Or in .sh files.


You can use tab in your document without altering the output.

You can then indent your code for a better readability.

For example, emacs have a functionnality to indent the code in automatic way and I suppose that most of editors have it.

Tags:

Sourcecode