User Tools

Site Tools


Tabular Material


First, a distinction: “tabular” refers to information arranged in columns and rows—what most word processors call a table. A table in LaTeX, strictly speaking, is a larger unit; it may very well contain tabular material, but can also include text not in tabular form (e.g., to explain the source or organization of the tabular material), have a caption, and be automatically numbered and included in a list of tables. This section will teach you how to set up your tabular material, which is one of the more complex topics for those new to LaTeX. If you want your tabular material to be in a table, put it inside a \begin{table} . . . \end{table} environment. Note that tables (but not standalone tabulars) float, that is, may be not appear in the final document in exactly the same place that they do in the source. See OTHER SECTION for more about floats.

LaTeX provides three environments for creating tabular material: tabbing, tabular, and array. We will begin with tabbing, the simplest and most limited of the three, and then introduce tabular. The array environment is used for mathematics and works in the same way as tabular, with some additional features specific to math; the latter may be discussed in the MATH SECTION of this guide.

The tabbing environment

The LaTeX tabbing environment works just like the tab stops you set on the ruler in a word processing program or (for those old enough to remember such things) the mechanical tab stops on a typewriter. It is subject to the same limitations as those mechanisms, being suitable only for short items whose lengths are determined in advance, but tabbed lists will break across pages—something that tabular cannot do, as will be explained below.

Look at this simple list of American presidents:

\hspace{1.5in} \= \hspace{1in}\= \\
Washington, George \>1789--1797 \> commander in chief during the Revolution \\
Lincoln, Abraham \>1861--1865 \> assassinated just after the Civil War \\
Roosevelt, Franklin \> 1933--1945 \> longest-serving president

The second line sets the positions for the tab stops with the command \=. You can specify the positions using \hspace, as in this example, or you can include a line of text such as the following instead:

Washington, Georgexx \= 1933--1945xx \= longest \kill

The latter is convenient if you see that one particular entry is clearly the longest one in a given column, so you don’t have to figure out the actual measurements. You prevent this setup line from being printed by terminating it with \kill rather than \\. (When testing this example the entries came out very close together, so two extra letters were added to open up the spacing a little.) Then you type your entries, using \> to move to the next tab position; each line except the last must be terminated with two backslashes. There are a number of other tricks you can do with tabbing, for which see the standard LaTeX guidebooks; what we have given here will cover most needs.

The tabular environment

Creating a simple tabular

LaTeX provides a \begin{tabular . . . \end{tabular} environment that is easy to use, provided that your information is short and simple. You specify how many columns you want and whether each one should be left aligned, centered, or right aligned, putting this information in curly brackets. So this command

\begin{tabular}{ c c l r }

will produce a four-column tabular in which the first two columns are centered, the third left aligned, and the fourth right aligned. LaTeX will automatically calculate the necessary widths for the cells, based on their contents. Enter each row on a separate line, terminated with a double backslash (\\), and use an ampersand (&) to move from one column to another. (Because & is used as a column delimiter, it is one of several characters in TeX that cannot be used in ordinary text. If you need to enter an actual ampersand, use the command \&.) Forgetting to include the ampersand before each column’s text and the double backslash at the end of each line are the most common mistakes in setting up a tabular, and you will get error messages when you compile if you omit any of them.

Horizontal lines are created with the command \hline, which is valid only as the first item in a tabular or after the line-ending \\ command. Here’s a sample:

\begin{tabular}{r l l r}
Part No. &Date &Bin No. &Price \\
10034 &January 1998 &A-4 &10.94 \\
2994 &March 2006 &B-2 &8.74 \\

Note that LaTeX ignores blank spaces in tabular source material, so you can use the spacebar or the tab key to help you keep track of the columns as you edit, if that is helpful to you. LaTeX’s treatment of blank horizontal spaces may take some getting used to for those accustomed to word processors, where the space on the screen is duplicated in the printed output. The source below produces the same result as that given above and is easier to read (including keeping track of those essential ampersands and double backslashes):

\begin{tabular}{r l l r}
Part No. & Date         & Bin No. & Price  \\
10034    & January 1998 & A-4     & 10.94  \\
2994     & March 2006   & B-2     & 8.74   \\

Note that experienced TeX users usually put the \hline right after the line break, like this:

2994     & March 2006   & B-2     & 8.74   \\  \hline 

But it may help you to keep track of things if you put the \hline by itself.

If you want vertical lines between cells, you can add them by inserting the pipe character | as part of your setup, like this:

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

which will put a vertical line at the left and right edges of the table as well as between each column. Word processors and web page editors have accustomed us to seeing lots of vertical lines in tabular material, but traditional typesetting generally frowns on them, except perhaps at the edges. Try using horizontal lines only and see if the results are legible.

If you want the entire table centered horizontally on the page, enclose it in the center environment like this:

    \begin{tabular}{r l l r}
        . . . 

Wrapping text inside a cell

As long as your entries are short, LaTeX does a nice job setting up the table; it will figure out how wide each column needs to be to contain the longest entry and will calculate the column widths for you. Suppose, however, that one or more of your items is lengthy; maybe a description of one of the parts on sale, such as “Added in 1998 but discontinued five years later due to low demand and manufacturing difficulties.” Using c, r, and l column specifiers, LaTeX will make a table as wide as the textblock and set up the columns as best it can, then just chop off what doesn’t fit from the long item(s). To solve this problem, use the p column specifier that allows text to wrap inside a cell. We could add a description to our table above as follows:

\begin{tabular}{r l l r p{2in} }
  Part No. & Date         & Bin No. & Price & Description \\
  10034    & January 1998 & A-4     & 10.94 & Added in 1998 but discontinued five years later due to low demand. \\
  2994     & March 2006   & B-2     & 8.74  & Replaced part no. 2998 for use in vacuum cleaners.

The p specifier must be followed by the desired width inside curly brackets. It may take some experimentation to find the right width for the p column, balancing the width of the other columns, the overall width of the tabular, etc. Don’t worry about the fact that the final column does not look right in the source code (it flows into a second line, unless you are using a very wide monitor); it will be typeset properly when you compile the document.

Additional features of tabular

The information above will enable you to set up basic tabulars. The LaTeX tabular environment can do a number of additional things, such as create multicolumn entries (sometimes useful for headings), control the spacing between columns, and much else. See the standard LaTeX references for information about these more advanced features.

Another solution to text wrapping: the tabularx package

Instead of specifying in advance the width of columns in which text may wrap (the p{ } option), another approach is to give the width of the entire table and let LaTeX adjust the width of one or more columns. This is done using the tabularx package, which implements the tabularx environment. This extended version (hence the -x in its name) of tabular is included in all TeX distributions. It works just like tabular except that you specify the total width of the table in curly brackets before the column setup and use the X column specifier (note uppercase X) to indicate columns whose width LaTeX may adjust:

 \begin{tabularx}{5in}{r l l r X } 

In this case TeX will calculate the required widths for the first four columns and then use the remaining space in the table for a column whose contents can wrap to accommodate long lines. Don’t forget to add \usepackage{tabularx} to your preamble if you want to set things up this way and to finish your tabular with \end{tabularx} (not \end{tabular}!). Note that columns created with the X specifier, like those made with p, are set fully justified. To get ragged right X columns, set up your tabular this way:

 \begin{tabularx}{5in}{r l l r >{\raggedright\arraybackslash}X } 

The >{ . . . } is a formatting command that you can also use in other ways; see the section below on formatting text inside a tabular. This command works with both the p and X column specifiers. You cannot get automatic hyphenation in tabular columns, whether justified or ragged right.

Tabulars that take more than one page

Neither tabular nor tabularx can break tabular material to flow onto additional pages, if required. For such lengthy tables you can use the longtable package. This package also lets you specify a header row to appear at the top of each page of tabular material, as well as some additional options. The documentation for the longtable package is straightforward, so we refer you to it for all the details. Note that longtable may require two or three runs of LaTeX in order to sort everything out. If you compile the document once and your longtable looks incorrect, don’t worry; just recompile. This is explained in the longtable documentation, but we mention it here because seeing the table looking wrong after the first pass can be frustrating if you don’t realize the need for multiple passes.

Formatting text in a tabular

If you want, let’s say, your whole table in a smaller point size, you might think that you could just enclose the whole tabular inside {\small . . . }. However, this will not work using the standard LaTeX size commands. Using regular LaTeX size commands you will need to specify the formatting for each column when you set up the tabular. For example, the following:

 \begin{tabular}{ >{\small}l >{\small}l >{\small\bfseries}r } 

would produce a three-column tabular in which each column is set smaller than normal and the righthand column is boldface. In other words, before each column specifier (l, r, c, p) you insert a greater-than sign followed by the desired formatting commands inside curly brackets. Note that you need to use the declaration form of the commands (e.g., \itshape not \textit for italics); see REFERENCE for explanation of this difference.

If this seems too much hassle, you can also use the relsize package, which does let you issue font size change commands in the form of the \smaller and \larger commands. Unlike the LaTeX \small, \large, etc. commands these are relative to your current font size:


More information on the relsize package can be found here.

Finally, you can also specify your font size explicitly, using the low level LaTeX \fontsize and \selectfont command pair:

\fontsize{20}{22} % arguments: {font size}{baseline skip}

Coloring your tables

If you want to use a colored background for your cells, there are several package options available. The colortbl package, found here, and the built-in shading of the longtable package already mentioned are good places to start.

Tabulars in memoir

Note that the memoir class (see REFERENCE) includes both the tabular and tabularx environments, along with some further refinements of its own. See Chapter 11 of the memoir manual for details.

tabular_material.txt · Last modified: 2014/11/05 19:54 by