Let’s Learn LaTex: Part 4

I sort of neglected my Let’s Learn LaTex series for a while now. The last LaTex related post I made was in April and now it is already November. I figured I might as well get back to it, and hopefully if you were following along you can refresh yourselves using the link above.

Last time I promised to explain tables, but then I realized that tables are quite a big topic so I will split this lesson into two parts. Today I will talk about basic table structure, and next time I’ll outline some more advanced table tricks.

How do you make a table in LaTex? How do you make a list? If you recall from the last post, lists were LaTex environments. So are tables. Instead of using enumerate or itemize environment, you use tabular instead. The \begin{tabular} and \end{tabular} keywords set it up up just like lists. Let me show you a simple one:

\begin{tabular}{ l c r }
  foo1 & bar1 & baz1 \\
  foo2 & bar2 & baz2 \\
  foo3 & bar3 & baz3 \\
\end{tabular}

It will make this fine looking table:

Basic table

It is spartan and simplistic but we can fix that in a few seconds. Let’s talk about the basics. The tabular environment takes an argument which is a textual string that “describes” the columns, and it accepts following literals:

  • l – Left aligned column
  • c – Centered column
  • r – Right aligned column
  • | – A vertical line / column divider

It does not really matter which character you use in the example above, because all the columns are of equal width and LaTex will do auto-kerning and alignment on them as usual.

Once you define the columns, you just start typing your data in, and place the & character when you want to skip to the next column and you end the row with the \\. Naturally if you want to use the & symbol in your table you will need to escape it as \&

Now let’s add some lines. You can use the | (pipe character) to add vertical lines in the column definition string and \hline command to add vertical lines in the body of the table like this:

\begin{tabular}{| l | c || r |}
\hline
  foo1 & bar1 & baz1 \\
\hline
  foo2 & bar2 & baz2 \\
\hline
  foo3 & bar3 & baz3 \\
\hline
\end{tabular}

The result will look like this:

Table with some lines

Much nicer, eh? You should note that LaTex does not have the equivalent of the <th> html tag that would automatically style a row as a column header. You have to do this sort of thing manually either by using multiple \hline commands or by using \textbf or other formatting on individual table cells.

Similarly to HTML tables, the tabular environment won’t wrap long text lines around. This behavior is by design, but it often catches new LaTex users by surprise when their table slides right off the page. Let’s test it and create a nice table with some long sentences. To make it more fun lets use everyone’s favorite panagram featuring that pesky brown fox:

\begin{tabular}{| l | l |}
\hline
  Incorrect version & The quick brown fox jumped over the lazy dog. \\
\hline
  Correct version & The quick brown fox jumps over the lazy dog. \\
\hline
\end{tabular}

Yes, despite the popular belief the brown fox jumps over the dog all the time – like right now for example. If he had jumped over him once at some point in the past and never did it again he would ruin the panagram by ommiting the letter ‘s’. But I digress. For our purpose let’s say we have a rather narrow page. In such case the table above could easily end up looking like this:

Table too wide for the page

Fortunately this is easily fixable by using p{w}, m{w} or b{w} instead of l, c and r where w is some width attribute:

  • p{w} – top aligned wrapped paragraph column
  • m{w} – middle aligned wrapped paragraph column
  • b{w} – bottom aligned wrapped paragraph column

The value of w must be a number followed by measurement unit such as in (inches), cm (cemtimeters) or em (internal LaTex measure which defaults to the width of letter ‘m’ in the current font). Let’s try this with our example:

\begin{tabular}{| l | p{10em} |}
\hline
  Incorrect version & The quick brown fox jumped over the lazy dog. \\
\hline
  Correct version & The quick brown fox jumps over the lazy dog. \\
\hline
\end{tabular}

Here I set the width of the second column to be approximately ten widths of the letter m, which resulted in this output:

Table with restricted width

As you can see, the result is much better.

One of the lovely things about LaTex is that it was written by programmers for programmers. Programmers are lazy beasts, so anything that requires lots of typing or copying and pasting will usually get lumped into some sort of a function. This is true for tables. For example, if you need to define several identical columns, you can use the * (star/asterisk) notation in your column definition. It usually takes the form of:

*{howmany}{columndef}

The value of howmany is a number and the value of columndef is actual column definition (for example c or a p which can be accompanied by some pipe characters to create column border). Let me give you a quick example:

\begin{tabular}{ l | *{4}{c|} }
  Student Name	& HW 1 & HW 2 & HW 3 & HW 4 \\
\hline
  John Smith	& A & B & A & F \\
  Jane Smith	& C & C & F & F \\
  Bob Johnson	& A & F & A & B \\
\end{tabular}

It will look like this:

Table created using the star notation

The last thing I want to show you today is spanning. Sometimes you want to have rows that span more than one column. In HTML tables you can achieve that by sticking an attribute on a <td> tag. Since LaTex uses a much simpler table structure, and the column definition attribute is already overloaded as it is, things get hairier here – but only a little bit.

Essentially instead of typing the columns or rows that need to span, you use a special command such as:

\multicolumn{howmany}{columndef}{content}

Let me give you a quick example:

\begin{tabular}{| l | *{4}{c|} }
\hline
  \multicolumn{5}{|c|}{Homework Grades} \\
\hline
  Student Name	& HW 1 & HW 2 & HW 3 & HW 4 \\
\hline
  John Smith	& A & B & A & F \\
  Jane Smith	& C & C & F & F \\
  Bob Johnson	& A & F & A & B \\
\hline
\end{tabular}

I’m sure you can see the \multicolumn statement replaces an entire row here, but if it didn’t you could still use & characters to the left and right of it. Here is the output:

Table with multicolumn

If you want spanning rows, you will need to use a package called multirow which is something I did not explain yet. Which is why I think the next lesson will be about importing packages and then I will swing back to tables.

Let’s Learn LaTex
<< Prev Next >>
This entry was posted in Uncategorized. Bookmark the permalink.



8 Responses to Let’s Learn LaTex: Part 4

  1. Blanko2 NEW ZEALAND Mozilla Firefox Windows says:

    oh wow, this is great, i didnt know you had a series like this! im definitely reading the first three, then this one.

    Reply  |  Quote
  2. bingfeng Google Chrome Windows says:

    Hi, I downloaded several Latex template made by you several days ago. Such as exam, thesis and so on. But I deleted them by mistake, and it is so sad that I cannot even find the website where I downloaded these files. So would you like to do me a favor, and give me a website link? Thanks very much.

    Reply  |  Quote
  3. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ bingfeng:

    I think you are looking for this page.

    Reply  |  Quote
  4. Bonnie AUSTRALIA Internet Explorer Windows says:

    Hey,
    Just wondering, do you have any idea how I could get LaTeX to add a footnote for something in a table (using the \tabular command)? It puts the footnote mark where I ask it to, but the footnote text disappears into oblivion. That was using the \footnote command. I’ve also tried using the \footnotemark and \footnotetext commands, but then the numbering is wrong (it works ok for the last reference in the table, but is wrong for all the others). Thanks!

    Reply  |  Quote
  5. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Bonnie:

    Well, the official way of doing this is using \footnotemark command like in this example but it does get messy.

    I guess the short answer is not to use tabular but instead go for one of the packages that extend it. I know that tabularx and longtable support footnotes.

    There are some more suggestions here, maybe you can check them out.

    Reply  |  Quote
  6. aleks UNITED STATES Google Chrome Mac OS says:

    moar tex tutorials please! these are great.

    Reply  |  Quote
  7. Facundo Javier ARGENTINA Google Chrome Windows says:

    You must continue this series of tutorials, I learned a lot! :)

    Reply  |  Quote
  8. Pingback: Let’s Learn LaTex: Part 6 | Terminally Incoherent UNITED STATES WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *