Let’s Learn LaTex: Part 6

In Part 4 we talked about the tabular environment which, as I mentioned, is more or less equivalent to tables in HTML. There is another popular way to typeset tabulated data in LaTex which emulates a feature that simply does not exist in HTML but is commonly seen in word processors. I’m talking about tab stops.

There is a funny feature in Microsoft Word, known as the tab ruler. It is typically located on a little strip at the top of the editing window. It is funny because I have yet to meet a MS Office user who actually knew what it was for and how to use it. Pretty much all Word users assume that hitting the Tab key simply advances the cursor a fix amount of space to the right, except for when it does not. They figure it is one of those eternally unresolved computer mysteries. Word of course tells you exactly where it will place the cursor when you tab over. It places it at the nearest tab stop to the righgt. By default these tab stops are visible on the ruler – I highlighted them for you in the screen shot below:

Tab Stops in MS Word

Default tab stops are evenly spaced half an inch apart which would be kinda limiting if you couldn’t override them. But you can, by simply clicking on the ruler and leaving these L shaped marks which override default tab stops. This feature allows you to properly line up your variable with font text without leaving too much hassle. Until you realize that like most features in word, depending on cursor location and context the setting may be global, per paragraph or per line leading to all kinds of hilarious formatting mishaps.

In LaTex you can achieve a similar effect using the tabbig environment. Except it is much clearer, and it gives you much better control as to where the tab stops are placed. Basically you set them by example like this:

1 2 3 4 5 \begin{tabbing} This is a long line: \= 1254 \\ Another line: \> 5785 \\ Short line: \> 5747 \\ \end{tabbing}

The \= sequence sets a tab stop at the current position and the \> advances the text that follows to the next tab stop position. The result looks like this:

Tabbing Output

As with most LaTex environments, the white space in tabbing is meaningless and the actual length of the line defines the position of the tab stop. So in the example above everything aligns to the length of the first line. This can sometimes be problematic, seeing how the first line in your list does not always have to be the longest one:

1 2 3 4 5 6 \begin{tabbing} This is a long line: \= 1254 \\ Another line: \> 5785 \\ Short line: \> 5747 \\ This one is even longer: \> 1457 \\ \end{tabbing}

This code will produce an ugly, overlapping effect:

Tab Overlap

If you run into this sort of an issue you can often resolve it by applying some padding to the first line using the \hspace command. In case you haven’t guessed it, the command is short for horizontal space and int inserts exactly that into your document. It takes one parameter as a required argument, and it must be a number with a unit of measurement indicator (ie 1in, 2cm, 3em, etc..):

1 2 3 4 5 6 \begin{tabbing} This is a long line: \hspace{3em} \= 1254 \\ Another line: \> 5785 \\ Short line: \> 5747 \\ This one is even longer: \> 1457 \\ \end{tabbing}

The 3em padding before the tab stop will provide adequate spacing for all the lines now:

Inserting a \hspace command into the first line is convenient, but not always ideal. The downside is that it “pollutes” the actual data with irrelevant positioning and formatting code. Frequently you will want your actual data to be de-coupled with the formatting markup that surrounds it.

If you know the approximate width of you longest line, you can make an invisible heading line that will set the tab stops without being displayed by ending it with the \kill command. It works the same as \\ but suppresses printing of the line while maintaining the tab stops it sets:

1 2 3 4 5 6 7 \begin{tabbing} \hspace{12em} \= \kill This is a long line: \> 1254 \\ Another line: \> 5785 \\ Short line: \> 5747 \\ This one is even longer: \> 1457 \\ \end{tabbing}

There are also \+ and \- commands that indent and un-indent all following lines by a tab stop. The former is equivalent of starting each line that follows it with a single \>. As an example of how to use these two operators, take this snippet of code taken directly from my syllabus:

1 2 3 4 5 6 7 8 9 10 11 12 13 \begin{tabbing}   \textbf{Class Schedule:} \= \\ \+\kill % indent everything   \hspace{3em} \= \hspace{8em} \= \hspace{5em} \= \kill Thu. \> 8:15PM-9:30PM \> (RI108) \> Lab Session \\ Thu. \> 9:30PM-10:45PM \> (RI118) \> Lecture Session \\   \< \textbf{Office Hours:} \\ Thu. \> 7:00PM-8:00PM \> RI320\\   \end{tabbing}

The above will produce output that looks something like this:

Tabbing with Indents

Note how in line 4 I use the \+ indent operator, and then everything below automatically start one tab stop to the right. This actually saves me a lot of typing (and potential formatting issues later) since I no longer have to preface every other line with \>. Instead I just stick \< in front of line 10 (the office hours heading) to shift it back one tab stop to the left so that it is in the same position as line 3.

Note that I could have easily put the \+ on line 3, immediately after the tab stop and it would have worked just as well. However since this command affects everything below it, I like to put it on it’s own suppressed line just so that it is painfully obvious what is happening. Otherwise it is easy to miss, and if I come back and start shifting things around later I might be in for a nasty surprise when stuff breaks unexpectedly.

This entry was posted in programming and tagged , . Bookmark the permalink.