LaTeX/Tables
Template:LaTeX/Navigation In academic writing, tables are a common feature, often for summarising results from research. It is therefore a skill that needs mastering in order to produce good quality papers.
However, if there is one area about LaTeX that I feel is the least intuitive, then I am afraid that this is it. Basic tables are not too taxing, but you will quickly notice that anything more advanced can take a fair bit of construction. So, we will start slowly and build up from there.
The tabular environment
The tabular environment can be used to typeset beautiful tables with
optional horizontal and vertical lines. LaTeX determines the width of the
columns automatically.
The first line of the environment has the form:
\begin{tabular}[pos]{table spec}
the table spec argument tells LaTeX the alignment to be used in each column and the vertical lines to insert.
The number of columns does not need to be specified as it is inferred by looking at the number of arguments provided. It is also possible to add vertical lines between the columns here. The following symbols are available to describe the table columns (some of them require that the package array has been loaded):
| l | left-justified column |
| c | centered column |
| r | right-justified column |
| p{width} | paragraph column with text vertically aligned at the top |
| m{width} | paragraph column with text vertically aligned in the middle (requires array package) |
| b{width} | paragraph column with text vertically aligned at the bottom (requires array package) |
| | | vertical line |
| || | double vertical line |
By default, if the text in a column is too wide for the page, LaTeX won’t automatically wrap it. Using p{width} you can define a special type of column which will wrap-around the text as in a normal paragraph. You can pass the width using any unit supported by LaTeX, such as pt and cm, or command lengths, such as \textwidth.You can find a complete list in appendix Useful Measurement Macros.
The optional parameter pos can be used to specify the vertical position of the table relative to the baseline of the surrounding text. You can use the following letters:
| b | bottom |
| c | center |
| t | top |
In the first line you have pointed out how many columns you want, their alignment and the vertical lines to separate them. Once in the environment, you have to introduce the text you want, separating between cells and introducing new lines. The commands you have to use are the following:
| & | column separator |
| \\ | start new row (additional space may be specified after \\ using square brackets, such as \\[6pt])
|
| \hline | horizontal line |
| \cline{i-j} | partial horizontal line beginning in column i and ending in column j |
Note, any white space inserted between these commands is purely down to ones' preferences. I personally add spaces between to make it easier to read.
Basic examples
This example shows how to create a simple table in LaTeX. It is a three-by-three table, but without any lines.
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
|
Expanding upon that by including some vertical lines:
\begin{tabular}{ l | c || r | }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
|
To add horizontal lines to the very top and bottom edges of the table:
\begin{tabular}{ l | c || r | }
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
|
And finally, to add lines between all rows, as well as centering (notice the use of the center environment - of course, the result of this is not obvious from the preview on this web page):
\begin{center}
\begin{tabular}{ l | c || r | }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{center}
|
\begin{tabular}{|r|l|}
\hline
7C0 & hexadecimal \\
3700 & octal \\ \cline{2-2}
11111000000 & binary \\
\hline \hline
1984 & decimal \\
\hline
\end{tabular}
|
Column specification using >{\cmd} and <{\cmd}
Using the array package, one is able to alter the column specification. This is done in the
argument of the tabular environment using >{\command} for commands executed right
before each column element. The code <{\command} for commands to be executed right
after each column element.
As an example: to get a column in math mode enter: \begin{tabular}{|>{$}c<{$}}.
Another example is changing the font: \begin{tabular}{|>{\small}c} to print the column in a small font.
Remark. The commands placed within cannot have any arguments as far as I know. So >{\bfseries} is valid,
but >{\textbf} will not work, and >{\bfseries{} is not valid. If anyone knows how to do this,
please add to this text.
Text wrapping in tables
LaTeX's algorithms for formatting tables have a few shortcomings. One is that it will not automatically wrap text in cells, even if it has overrun the width of the page. For columns that you know will contain a certain amount of text, then it is recommended that you use the p attribute and specify the desired width of the column (although it may take some trial-and-error to get the result you want). Use the m attribute to have the lines aligned toward the middle of the box and the b attribute to align along the bottom of the box.
Here is a practical example. The following code creates two tables with the same code; the only difference is that the last column of the second one has a defined width of 5 centimeters, while in the first one we didn't specify any width. Compiling this code:
\documentclass{article}
\usepackage[english]{babel}
\begin{document}
Without specifying width for last column:
\begin{center}
\begin{tabular}{ | l | l | l | l |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
With width specified:
\begin{center}
\begin{tabular}{ | l | l | l | p{5cm} |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
\end{document}
You get the following output:

Note that the first table is cropped: The output is wider than the page width.
Defining multiple columns
It is possible to define many identical columns at once using the *{num}{str} syntax.
This is particularly useful when your table has many columns.
Here is a table with six centered columns flanked by a single column on each side:
\begin{tabular}{l*{6}{c}r}
Team & P & W & D & L & F & A & Pts \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12 \\
Celtic & 6 & 3 & 0 & 3 & 8 & 9 & 9 \\
Benfica & 6 & 2 & 1 & 3 & 7 & 8 & 7 \\
FC Copenhagen & 6 & 2 & 1 & 2 & 5 & 8 & 7 \\
\end{tabular}
|
@-expressions
The column separator can be specified with the @{...} construct.
It typically takes some text as its argument, and when appended to a column, it will automatically insert that text into each cell in that column before the actual data for that cell. This command kills the inter-column space and replaces it with whatever is between the curly braces. To add space, use @{\hspace{width}}.
Admittedly, this is not that clear, and so will require a few examples to clarify. Sometimes, it is desirable in scientific tables to have the numbers aligned on the decimal point. This can be achieved by doing the following:
\begin{tabular}{r@{.}l}
3 & 14159 \\
16 & 2 \\
123 & 456 \\
\end{tabular}
|
Its space suppressing qualities actually make it quite useful for manipulating the horizontal spacing between columns. Given a basic table, and varying the column descriptions:
\begin{tabular}{|l|l|}
\hline
stuff & stuff \\ \hline
stuff & stuff \\
\hline
\end{tabular}
| {|l|l|} | |
| {|@{}l|l@{}|} | |
| {|@{}l@{}|l@{}|} | |
| {|@{}l@{}|@{}l@{}|} |
Spanning
To complete this tutorial, we take a quick look at how to generate slightly more complex tables. Unsurprisingly, the commands necessary have to be embedded within the table data itself.
Rows spanning multiple columns
The command for this looks like this: \multicolumn{num_cols}{alignment}{contents}. num_cols is the number of subsequent columns to merge; alignment is pretty obvious, either l, c, or r. And contents is simply the actual data you want to be contained within that cell. A simple example:
\begin{tabular}{|l|l|}
\hline
\multicolumn{2}{|c|}{Team sheet} \\
\hline
GK & Paul Robinson \\
LB & Lucus Radebe \\
DC & Michael Duberry \\
DC & Dominic Matteo \\
RB & Didier Domi \\
MC & David Batty \\
MC & Eirik Bakke \\
MC & Jody Morris \\
FW & Jamie McMaster \\
ST & Alan Smith \\
ST & Mark Viduka \\
\hline
\end{tabular}
|
Columns spanning multiple rows
The first thing you need to do is add \usepackage{multirow} to the preamble. This then provides the command needed for spanning rows: \multirow{num_rows}{width}{contents}. The arguments are pretty simple to deduce. With the width parameter, you can specify a fixed width if you wish, or, if you want the natural width (i.e., just wide enough to fit the contents of the column) then simply input an asterisk (*). This approach was used for the following example:
...
\usepackage{multirow}
...
\begin{tabular}{|l|l|l|}
\hline
\multicolumn{3}{|c|}{Team sheet} \\
\hline
Goalkeeper & GK & Paul Robinson \\ \hline
\multirow{4}{*}{Defenders} & LB & Lucus Radebe \\
& DC & Michael Duberry \\
& DC & Dominic Matteo \\
& RB & Didier Domi \\ \hline
\multirow{3}{*}{Midfielders} & MC & David Batty \\
& MC & Eirik Bakke \\
& MC & Jody Morris \\ \hline
Forward & FW & Jamie McMaster \\ \hline
\multirow{2}{*}{Strikers} & ST & Alan Smith \\
& ST & Mark Viduka \\
\hline
\end{tabular}
|
The main thing to note when using \multirow is that a blank entry must be inserted for each appropriate cell in each subsequent row to be spanned.
If there is no data for a cell, just don't type anything, but you still need the "&" separating it from the next column's data. The astute reader will already have deduced that for a table of columns, there must always be ampersands in each row. The exception to this is when \multicolumn and \multirow are used to create cells which span multiple columns or rows.
Resize tables
The command \resizebox{width}{height}{object} can be used with \tabular to specify the height and width of a table. The following example shows how to resize a table to 8cm width while maintaining the original width/height ratio.
\resizebox{8cm}{!} {
\begin{tabular}...
\end{tabular}
}
Sideways tables
Tables can also be put on their side within a document using the rotating package and the sidewaystable environments in place of the table environment. (NOTE: most DVI viewers do not support displaying rotated text. Convert your document to a PDF to see the result. Most, if not all, PDF viewers do support rotated text.)
\usepackage{rotating}
\begin{sidewaystable}
\begin{tabular}...
\end{tabular}
\end{sidewaystable}
The table environment - captioning etc
Although the tabular environment typesets tables brilliantly, it doesn't cover all that you need to do with tables. For example, you might want a caption for your table. For this and other reasons, you should typically place your tabular environment inside a table environment:
\begin{table}[h]
\caption{Performance at peak F-measure}
\begin{tabular}{| r | r || c | c | c |}
...
\end{tabular}
\end{table}
Why do the two different environments exist? Think of it this way: The tabular environment is concerned with arranging elements in a tabular grid, while the table environment represents the table more conceptually. This explains why it isn't tabular but table that provides for captioning (because the caption isn't displayed in the grid-like layout).
A table environment has a lot of similarities with a figure environment, in the way the "floating" is handled etc.
The tabular* environment - controlling table width
This is basically a slight extension on the original tabular version, although it requires an extra argument (before the column descriptions) to specify the preferred width of the table.
\begin{tabular*}{0.75\textwidth}{ | c | c | c | r | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1 & item 2 & item 3 & item 4 \\
\hline
\end{tabular*}
|
However, that doesn't look quite as intended. The columns are still at their natural width (just wide enough to fit their contents whilst the rows are as wide as the table width specified. This looks very ugly. The reason for the mess is that you must also explicitly insert extra column space. Fortunately, LaTeX has rubber lengths, which unlike others, are not fixed, and LaTeX can dynamically decide how long they should be. So, the solution to the current problem is:
\begin{tabular*}{0.75\textwidth}{@{\extracolsep{\fill}} | c | c | c | r | }
\hline
label 1 & label 2 & label 3 & label 4 \\
\hline
item 1 & item 2 & item 3 & item 4 \\
\hline
\end{tabular*}
You will notice the @{...} construct added at the beginning of the column description. Within it is the \extracolsep command, which requires a width. A fixed width could have been used, however, by using a rubber length, such as \fill, the columns are automatically spaced evenly.
Professional tables
Many tables in professionally typeset books and journals feature simple tables, which have appropriate spacing above and below lines, and almost never use vertical rules. Many examples of LaTeX tables (including this Wikibook) showcase the use of vertical rules (using "|"), and double-rules (using \hline\hline" or "||"), which are regarded as unnecessary and distracting in a professionally published form. The booktabs package is useful for easily providing this professionalism in LaTeX tables, and the documentation also provides guidelines on what constitutes a "good" table.
In brief, the package uses \toprule for the uppermost rule (or line), \midrule for the rules appearing in the middle of the table (such as under the header), and \bottomrule for the lowermost rule. This ensures that the rule weight and spacing are acceptable. In addition, \cmidrule can be used for mid-rules that span specified columns. The following example contrasts the use of booktabs and normal LaTeX implementations (the later example requires \usepackage{booktabs} in the preamble).
Need more complicated features?
Have a look at one of the following packages:
- hhline: do whatever you want with horizontal lines
- array: gives you more freedom on how to define columns
- colortbl: make your table more colorful
- supertab: for tables that need to stretch over several pages
- longtable: same as above. Note: footnotes do not work properly in a normal tabular environment. If you replace it with a longtable environment, footnotes work properly
- xtabular: Yet another package for tables that need to span many pages
- tabulary: modified tabular* allowing width of columns set for equal heights
Summary
This concludes discussion of basic tables. Experimentation quickly leads to mastery. While the table syntax in LaTeX can look rather messy, seeing new examples can look confusing. But hopefully, enough has been covered here so that a user can create any table needed for your papers. Unsurprisingly, LaTeX has plenty more up its sleeve, so expect a follow up tutorial covering more advanced features in the near future.




