Tables

Tables are common elements in most scientific documents, LaTeX provides a large set of tools to customize tables, change the size, combine cells, change the colour of cells and so on. This article explains how.

The tabular environment is the default LaTeX method to create tables. You must specify a parameter to this environment, {c c c} tells LaTeX that there will be three columns and that the text inside each one of them must be centred.

It was already said that the tabular environment is used to type tables. To be more clear about how it works below is a description of each command.

{ |c|c|c| }

This declares that three columns, separated by a vertical line, are going to be used in the table. Each c means that the contents of the column will be centred, you can also use r to align the text to the right and l for left alignment.

\hline

This will insert a horizontal line on top of the table and at the bottom too. There is no restriction on the number of times you can use \hline.

cell1 & cell2 & cell3 \\

Each & is a cell separator and the double-backslash \\ sets the end of this row.

First, to use the parameters shown in the example, you must import the package array in the preamble of your LaTeX file with the next command

\usepackage{array}

In the tabular environment, the parameter m{5em} sets a length of 5em for first column (1cm for the other two) and centres the text in the middle of the cell. The aligning options are m for middle, p for top and b for bottom. In standard tables new lines must be inserted manually so the table won't stretch out of the text area, when using this parameters the text is automatically formatted to fit inside each cell.

If you don't need to control the width of each cell, but of the entire table and then distribute the space within evenly, use the package tabu. See the example below:

The environment tabu is similar to tabular but more flexible, it's available after adding the line \usepackage{tabu} to the preamble. Notice that the environment opening statement is different, in the example the table width is set to 0.8 the width of the text. You can use any of the LaTeX units for such length.

The parameter inside braces | X[l] | X[c] | X[r] | sets the alignment of each column: the first one to left, the second one to center and the third one to right.

The command multirow takes three parameters. The first one is the number of rows to be combined, 3 in the example. The second parameter is the width of the column, 4em in the example. Finally, the third parameter is the content of the cell.

The caption of each table will be used to generate this list. For languages supported by the babel package, the title "List of tables" will be translated accordingly. See the article about International language support for more info.

and after the tabular environment. The command \rowcolors takes three parameters each passed inside braces:

the row to start,

the colour for odd rows and

the colour for even rows.

See the xcolor package documentation (at the further reading section) for a list of available colours and how to create your own. In the example the colours green and yellow are mixed in different proportions.

Below is a description about how to change the colour of each element in the table:

Colour of the lines. The command \arrayrulecolor is used for this. In the example an HTML format is used, but other formats are available too, see the xcolor documentation for a complete list (link provided at the further reading section).

Background colour of a cell. Use the command \cellcolor. You can either enter the name directly inside the braces (red, gray, green and so on) or pass a format parameter inside brackets (HTML in the example) and then set the desired colour inside the braces using the established format.

Background colour of a row. In this case \rowcolor will accomplish that. The same observations about colour selection mentioned in the two previous commands are valid for this one.

Background colour of a column. This one is a bit tricky, the easiest way is to define a new column type. The command

\newcolumntype{s}{>{\columncolor[HTML]{AAACED}} p{3cm}}

define a column type called s whose alignment is p, the column width is 3cm and the colour is set with HTML format to AAACED. This new column type is used in the tabular environment.