# LaTeX/Tables

LaTeX

Getting Started

Common Elements

Mechanics

Technical Texts

Special Pages

Special Documents

Creating Graphics

Programming

Miscellaneous

Help and Recommendations

Appendices

edit this boxedit the TOC

Tables are a common feature in academic writing, often used to summarise research results. Mastering the art of table construction in LaTeX is therefore necessary to produce quality papers and with sufficient practise one can print beautiful tables of any kind.

Keeping in mind that LaTeX is not a spreadsheet, it makes sense to use a dedicated tool to build tables and then to export these tables into the document. Basic tables are not too taxing, but anything more advanced can take a fair bit of construction; in these cases, more advanced packages can be very useful. However, first it is important to know the basics. Once you are comfortable with basic LaTeX tables, you might have a look at more advanced packages or the export options of your favorite spreadsheet. Thanks to the modular nature of LaTeX, the whole process can be automated in a fairly comfortable way.

For a long time, LaTeX tables were quite a chaotic topic, with dozens of packages doing similar things, while not always being compatible with one another. Sometimes you had to make trade-offs. The situation changed recently (2010) with the release of the tabu package which combines the power of longtable, tabularx and much more. The tabu environment is far less fragile and restricted than the older alternatives. Nonetheless, before attempting to use this package for the first time it will be beneficial to understand how the classic environment works, since tabu works the same way.

## The tabular environmentEdit

The tabular environment can be used to typeset 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 list in chapter Lengths.

The optional parameter pos can be used to specify the vertical position of the table relative to the baseline of the surrounding text. In most cases, you will not need this option. It becomes relevant only if your table is not in a paragraph of its own. You can use the following letters:

 b bottom c center (default) t top

To specify a font format (such as bold, italic, etc.) for an entire column, you can add >{\format} before you declare the alignment. For example \begin{tabular}{ >{\bfseries}l c >{\itshape}r } will indicate a three column table with the first one aligned to the left and in bold font, the second one aligned in the center and with normal font, and the third aligned to the right and in italic.

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 \newline start a new line within a cell (in a paragraph column) \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 examplesEdit

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} $\begin{array}{lcr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}$

Expanding upon that by including some vertical lines:

 \begin{tabular}{ l | c || r }   1 & 2 & 3 \\   4 & 5 & 6 \\   7 & 8 & 9 \\\end{tabular} $\begin{array}{l|c||r} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}$

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} $\begin{array}{l|c||r}\hline 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \hline \end{array}$

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{array}{l|c||r}\hline 1 & 2 & 3 \\ \hline 4 & 5 & 6 \\ \hline 7 & 8 & 9 \\ \hline \end{array}$
 \begin{tabular}{|r|l|}   \hline   7C0 & hexadecimal \\   3700 & octal \\ \cline{2-2}   11111000000 & binary \\   \hline \hline   1984 & decimal \\   \hline\end{tabular}

### Text wrapping in tablesEdit

LaTeX's algorithms for formatting tables have a few shortcomings. One is that it will not automatically wrap text in cells, even if it overruns the width of the page. For columns that will contain text whose length exceeds the column's width, 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). For a more convenient method, have a look at The tabularx package, or The tabulary package.

Instead of p, use the m attribute to have the lines aligned toward the middle of the box or the b attribute to align along the bottom of the box.

Here is a simple 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 has been cropped, since the output is wider than the page width.

### Manually Broken Paragraphs in Table CellsEdit

Sometimes it is necessary to not rely on the breaking algorithm when using the p specifier, but rather specify the line breaks by hand. In this case it is easiest to use a \parbox:

 \begin{tabular}{cc}   boring cell content & \parbox[t]{5cm}{rather long par\\new par}\end{tabular}

### Space between columnsEdit

To tweak the space between columns (LaTeX will by default choose very tight columns), one can alter the column separation: \setlength{\tabcolsep}{5pt}. The default value is 6pt.

### Space between rowsEdit

Re-define the \arraystretch command to set the space between rows:

 \renewcommand{\arraystretch}{1.5}

Default value is 1.0.

An alternative way to adjust the rule spacing is to add \noalign{\smallskip} before or after the \hline and \cline{i-j} commands:

\hline
Gnat      & per gram    & 13.65      \\
& each        & 0.01       \\
Gnu       & stuffed     & 92.50      \\
Emu       & stuffed     & 33.33      \\
Armadillo & frozen      & 8.99       \\
\hline
\end{tabular
}

### Using arrayEdit

\usepackage{array}
%or \usepackage{dcolumn}
...
\begin{tabular}{llr}
\firsthline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal    & Description & Price (\$) \\ \hline Gnat & per gram & 13.65 \\ & each & 0.01 \\ Gnu & stuffed & 92.50 \\ Emu & stuffed & 33.33 \\ Armadillo & frozen & 8.99 \\ \lasthline \end{tabular } ### Using booktabsEdit \usepackage{booktabs} ... \begin{tabular}{llr} \toprule \multicolumn{2}{c}{Item} \\ \cmidrule(r){1-2} Animal & Description & Price (\$) \\
\midrule
Gnat      & per gram    & 13.65      \\
& each        & 0.01       \\
Gnu       & stuffed     & 92.50      \\
Emu       & stuffed     & 33.33      \\
Armadillo & frozen      & 8.99       \\
\bottomrule
\end{tabular
}

Usually the need arises for footnotes under a table (and not at the bottom of the page), with a caption properly spaced above the table. These are addressed by the ctable package. It provides the option of a short caption given to be inserted in the list of tables, instead of the actual caption (which may be quite long and inappropriate for the list of tables). The ctable uses the booktabs package.

## Sideways tablesEdit

Tables can also be put on their side within a document using the rotating or the rotfloat package. See the Rotations chapter.

## Table with legendEdit

To add a legend to a table the caption package can be used. With the caption package a \caption*{...} statement can be added besides the normal \caption{...}. Example:

 \begin{table}   \begin{tabular}{| r | r || c | c | c |}       ...   \end{tabular}   \caption{A normal caption}   \caption*{     A legend, even a table can be used     \begin{tabular}{l l}       item 1 & explanation 1 \\     \end{tabular}   }\end{table}

The normal caption is needed for labels and references.

## The eqparbox packageEdit

On rare occasions, it might be necessary to stretch every row in a table to the natural width of its longest line, for instance when one has the same text in two languages and wishes to present these next to each other with lines synching up. A tabular environment helps control where lines should break, but cannot justify the text, which leads to ragged right edges. The eqparbox package provides the command \eqmakebox which is like \makebox but instead of a width argument, it takes a tag. During compilation it bookkeeps which \eqmakebox with a certain tag contains the widest text and can stretch all \eqmakeboxes with the same tag to that width. Combined with the array package, one can define a column specifier that justifies the text in all lines:

 \newsavebox{\tstretchbox}\newcolumntype{S}[1]{%   >{\begin{lrbox}{\tstretchbox} }%   l%   <{\end{lrbox}%   \eqmakebox[#1][s]{\unhcopy\tstretchbox} }%}

See the documentation of the eqparbox package for more details.

## Floating with tableEdit

In WYSIWYG document processors, it is common to put tables in the middle of the text. This is what we have been doing until now. Professional documents, however, often make it a point to print tables on a dedicated page so that they do not disrupt the flow. From the point of view of the source code, one has no idea on which page the current text is going to lie, so it is hardly possible to guess which page may be appropriate for our table. LaTeX can automate this task by abstracting objects such as tables, pictures, etc., and deciding for us where they might fit best. This abstraction is called a float. Generally, an object that is floated will appear in the vicinity of its introduction in the source file, but one can choose to control its position also.

To tell LaTeX we want to use our table as a float, we need to place a tabular environment in a table environment, which is able to float and add a label and caption.

The table environment initiates a type of float just as the environment figure. In fact, the two bear a lot of similarities (positioning, captions, etc.). More information about floating environments, captions etc. can be found in Floats, Figures and Captions.

The environment names may now seem quite confusing. Let's sum it up:

• tabular is for the content itself (columns, lines, etc.).
• table is for the location of the table on the document, plus caption and label support.
 \begin{table}[position specifier]   \centering   \begin{tabular}{|l|}     ... your table ...   \end{tabular}   \caption{This table shows some data}   \label{tab:myfirsttable}\end{table}

In the table, we used a label, so now we can refer to it just like any other reference:

 \ref{tab:myfirsttable}

The table environment is also useful when you want to have a list of tables at the beginning or end of your document with the command

 \listoftables

The captions show now up in the list of tables, if displayed.

You can set the optional parameter position specifier to define the position of the table, where it should be placed. The following characters are all possible placements. Using sequences of it define your "wishlist" to LaTeX.

 h where the table is declared (here) t at the top of the page b at the bottom of the page p on a dedicated page of floats ! override the default float restrictions. E.g., the maximum size allowed of a b float is normally quite small; if you want a large one, you need this ! parameter as well.

Default is tbp, which means that it is by default placed on the top of the page. If that's not possible, it's placed at the bottom if possible, or finally with other floating environments on an extra page.

You can force LaTeX to use one given position. E.g. [!h] forces LaTeX to place it exactly where you place it (Except when it's really impossible, e.g you place a table here and this place would be the last line on a page). Again, understand it correctly: it urges LaTeX to put the table at a specific place, but it will not be placed there if LaTeX thinks it will not look great. If you really want to place your table manually, do not use the table environment.

Centering the table horizontally works like everything else, using the \centering command just after opening the table environment, or by enclosing it with a center environment.

For complex or dynamic tables, you may want to use a spreadsheet. You might save lots of time by building tables using specialized software and exporting them in LaTeX format. The following plugins and libraries are available for some popular software:

However, copying the generated source code to your document is not convenient at all. For maximum flexibility, generate the source code to a separate file which you can input from your main document file with the \input command. If your speadsheet supports command-line, you can generate your complete document (table included) in one command, using a Makefile for example.

See Modular Documents for more details.

## Need more complicated features?Edit

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
• threeparttable makes it possible to put footnotes both within the table and its caption
• arydshln: creates dashed horizontal and vertical lines
• ctable: allows for footnotes under table and properly spaced caption above (incorporates booktabs package)
• slashbox: create 2D tables with the first cell containing a description for both axes. Not available in Tex Live 2011 or later.
• diagbox: compatible to slashbox, come with Tex Live 2011 or later
• dcolumn: decimal point alignment of numeric cells
• rccol: advanced decimal point alignment of numeric cells with rounding
• numprint: print numbers, in the current mode (text or math) in order to use the correct font, with separators, exponent and/or rounded to a given number of digits. tabular(*), array, tabularx, and longtable environments is supported using all features of numprint
• siunitx: alignment of tabular entries
• pgfplotstable: Loads, rounds, formats and postprocesses numerical tables.

## ReferencesEdit

1. Package multirow on CTAN

 Previous: Rotations Index Next: Title Creation