# LaTeX Table Tips

This page documents a few tricks for making LaTeX tables that I found useful when writing my thesis and preparing various publications. Just a quick note: For all examples on this page that use a tabular environment, you should put the tabular within a table environment if you want to make it an “official” table with caption, label and float position.

If coding tables by hand seems tedious, you can also cheat by converting Excel spreadsheets to LaTeX tables.

## Set both column width and column alignment

Column widths in tables are specified by column type p, e.g.: p{3cm}, which will make a column 3 cm wide. However, by default p columns are left aligned. To specify alignment for fixed width columns, you need to specify the alignment of the p columns. By doing this, you can create tables that look like:

First, you need to use the array package in your document:

\usepackage{array}

Specify the width and alignment of each table column with code from the following example:

\begin{tabular}{|p{1.5cm}|>{\raggedright}p{2.5cm}|>{\centering}p{1.5cm}|>{\raggedleft}p{3cm}|}
\hline
Default & Left-Aligned & Centered & Right-Aligned \tabularnewline
\hline
1.5 cm & 2.5 cm & 1.5 cm & 3 cm \tabularnewline
\hline
\end{tabular}

One thing to note is that to use this, the line break \\ must now be replaced with:

\tabularnewline

The column types used above are:

• p{1.5cm} specifies a left-aligned column (default) with 1.5 cm width.
• >{\raggedright}p{2.5cm} specifies a left-aligned column (the right side is ragged) with 2.5 cm width.
• >{\centering}p{1.5cm} specifies a centered column with 1.5 cm width.
• >{\raggedleft}p{3cm} specifies a right column (the left side is ragged) with 3 cm width.

You can define new column types for each of the above cases, as described here (which is where I learned this technique). This would make typing the table easier.

## Continuous vertical lines across double horizontal lines

In LaTeX tabulars, using \hline\hline will produce a double horizontal line but any vertical lines will be broken across the gap. To create an uninterrupted, intact vertical line across double hlines requires workaround: create an empty row and set the line break height using something like \\[-0.8em]. Adjusting the line break height adjusts the spacing between the double horizontal lines. The result will produce a table with double horizontal lines and a continuous vertical line across the gap:

The code below generates the above table:

\begin{tabular}{|l|l|}
\hline
Class & Accuracy (\%) \\
% The following code produces double hline with continuous vertical line.
\hline % First hline
& \\[-0.8em] % Empty table row with custom line break spacing.
\hline % Second hline.
Ice & 85\\
\hline
Water & 95\\
% The following code produces double hline with continuous vertical line.
\hline % First hline
& \\[-0.8em] % Empty table row with custom line break spacing.
\hline % Second hline.
Overall & 90\\
\hline
\end{tabular}

I found this tip from this blog post on keeping vertical lines intact across double horizontal lines.

If you look closely at the picture of the table above, you can see some artifacts around the vertical lines after each double \hline where the vertical lines are darker. I think this is because there actually are two vertical lines overlapping in those areas (one for the empty row and one for the next row). When PDFs are rasterized at a low resolution, these overlapping lines produce a darker single line due to roundoff errors and the like in the rasterization algorithm. Zooming in enough in the PDF removes the artifact (because the two overlapping lines should be on top of each other perfectly so only one line is visible).

## Align the top of a figure with the top of the text in a table row

When I was writing my thesis, including figures or graphics in a table was not straightfoward. It seems that whenever an image is included, the bottom of an image in a table cell was aligned with the first line of text in the other cells of the same table row:

The baseline of an image is at the bottom of the image. In table rows, the text content of each cell is vertically aligned so that the baselines of the first line of text are in the same vertical position. When the content of the cell is an \includegraphics, then the base line of that cell is the bottom of the image, causing the resulting table to look like the above.

In order to vertically align the top of the image with the top of the text in each table row, the image baseline must be adjusted. Once adjusted, you can produce tables like this:

First, this technique requires the calc package:

\usepackage{calc}

The following code produces the desired table (replace placeholder with the name of your graphic file):

\begin{tabular}{|p{2in}|c|}
\hline
Some text ... & \raisebox{2ex - \height}{\includegraphics[width=1in]{placeholder}} \\
% Use the raisebox command to set the baseline of the image, which will be aligned with baselines of the first lines of text in the same table row.
% The raisebox command changes the baseline of the image relative to the original baseline, which is at the bottom of the image.
% The (2ex - \height) sets the image baseline to about 1 text line below the top of the image.
% This lines the top of the image with the text properly.
\hline
\end{tabular}

The \raisebox command adjusts the baseline of the image in the table cell. Raising the box to 2ex - \height moves the baseline of the image by 2ex - \height relative to the default position (bottom of the image), where \height is the height of the image box. The final base line is 2ex below the top of the image, as 2ex is about the height of one text line (it is the height of two 'x' characters). When this baseline is aligned with the baseline of the first line of text in the other cells of the same table row, the top of the image lines up with the top of the text.

## Background colours in table cells

The colortbl package lets you set the background colour of individual table cells, columns and rows. This example shows how to set individual cell colours using the \cellcolor command. You can create tables that look like this:

First, include the colortbl package:

\usepackage{colortbl}

Use the following code to create the above table:

\begin{tabular}{|c|c|}
\hline
Colour 1 & Colour 2 \\
\hline
\cellcolor[rgb]{0.000,0.700,1.000} Blue & \cellcolor[rgb]{1.000,0.700,0.500} Orange \\
\hline
\end{tabular}

The \cellcolor command can be included anywhere in the cell whose colour you want to set. The \cellcolor command also works for longtable cells. This technique was handy for one of my publications where I had to shade individual table cells.

If you are using PdfTeX to produce PDFs, you will notice that cell colour background sometimes partially overlaps with the table lines when you view the PDF file. This is supposedly a PDF rendering issue in the viewer caused by rounding errors in the rasterization algorithm (the algorithm that actually draws the PDF to your screen). If you zoom in close enough or print the PDF, the artifact disappears.

## Multiple page tables

If your table has a lot of rows, you may need to split it up across several pages. The longtable environment allows you to do this. Here is a basic example of using longtable to create a multi-page table, with a specific caption and header row(s) for the first page and a different caption and header row(s) for the subsequent pages.

First page:

Subsequent pages:

This is useful if you want one caption for the first page of the table, while the caption should be “continued” for subsequent pages. For example, “Table 1: This is table 1” on the first page and then on subsequent pages, “Table 1: (continued)”.

Note that in the above pictures, I purposely set the \textheight of the page to 2 inches so I can have multiple page tables with only 12 rows of data (this is easier to present on this website). The textheight can be set by including this command before the document begins: \setlength{\textheight}{2in}

To use longtable, include the longtable package:

\usepackage{longtable}

Next, use the following code to produce the table shown above:

\begin{longtable}{|l|l|}

% specifies header for first page of table.
\caption{This caption appears on the first page of the table. \label{tab.longtable_example}} \\
\hline

% specifies header for rest of the pages.
\caption{Caption for subsequent pages.} \\
\hline

% This is the actual table data.
\hline
Table row & Table row \\
\hline
Table row & Table row \\
\hline
Table row & Table row \\
\hline
% [ ... and so on. The rest of the rows are not included for conciseness ... ]

\end{longtable}

The lines before \endfirsthead specify the caption and heading row(s) that will be used for the first page of the table. The next few lines before \endhead specify the same thing but for subsequent pages. There is quite a bit of flexibility here. For example, if you wanted the heading area of the subsequent pages to have two heading rows, you can just specify it before the \endhead:

	% specifies header for rest of the pages.
\caption{Caption for subsequent pages.} \\
\hline
Heading 2 for subsequent pages & Heading 2 for subsequent pages \\
% finished specifying subsequent headers

This would put multiple heading rows for subsequent pages, which will create the following second page:

The same can be specified for the first page before the \endfirsthead command.

I've seen some examples where the caption for the subsequent pages is specified using:

\caption[]{Caption for subsequent pages.}

This is supposed to give you a numbered caption that does not appear in the List of Tables (due to the contents of [] being empty). I haven't tried it. An alternative could be \caption* but that would give you no numbers.

## Discussion

, 2011/08/31 09:15
Hi, the post helped me out a lot in the formatting of my thesis tables. Cheers.
, 2012/11/23 14:35
Thanks for your article. I am using Longtable and want to color the background of the header. Any advise?

Thanks, Ralph
, 2012/12/07 14:03
I haven't tried it but you should be able to just use the colortbl package with longtable. There's a \rowcolor command.
, 2014/02/02 00:05
I am trying to put a period after the table number on the continued pages. Any ideas?
, 2014/12/15 14:34
Thank you very much. It was helpful.
, 2015/05/13 03:02
Thanks a lot.