LaTeX Tables

Posted: 18th July 2009 by Tim in LaTeX
Tags: , , ,

Creating tables in LaTeX is one of the more complicated tasks in simple document writing. This tutorial will cover the basics of creating LaTeX tables, including adding captions, adding the table to the List of Tables as well as referencing the table in other parts of the document.

LaTeX is good in the way it does a lot of the layout and positioning for you. As far as tables are concerned, LaTeX will populate the List of Tables, correctly add table reference numbers and decide the best place to display the table in the document. In or for LaTeX to do that, though, we must define it correctly.

LaTeX table objects are surrounded by \begin{table} and \end{table} tags. Within these tags, we define the table data (\begin{tabular} and \end{tabular}), caption (\caption{add_caption_here}) and label used to reference the table elsewhere in the document (\label{add_label_here}).

For the table data, we first need to define the basic table layout. We do this with the \begin{tabular} tag. Each column can be defined as left justified (l), centred (c) or right justified (r). If we want to put a black border between columns, we simply add a bar (|) between the columns. For example, to define a table with one right-justified column and three left-justified columns, with lines between each column, we would use

\begin{tabular}{ r | l | l | l }

Everything between this tag and the \end{tabular} tag is considered table data. An ampersand (&) signals the end of one cell and the start of the next. The end of a row is marked with a double backslash (\\). To put a black border under that row, add \hline after the double backslash. Two consecutive \hline calls will print a double-line.

To reference the table in the document, we use the \ref{label} tag.

So, to add a table to a document with a caption, which is listed in the List of Tables and referenced in the document we would do the following:

\begin{tabular}{ r | c | c }
ID & First Name & Surname \\ \hline \hline
1 & Tim & Murphy \\ \hline
2 & Joe & Bloggs \\ \hline
3 & John & Smith
\caption{Team Members}

As can be seen in Table \ref{tbl:team_members}, there are three members in the team.

An example of the document produced by this code may be seen here.