# table environments - tables with LaTeX

StartLaTeXfont sizearrowsminipageitalicsboldtext colormatrixfontslistingstables

tabular     columns     rows     tabular ⋆     tabbing     array

## Tables with LaTeX

The area of tables in LaTeX is such an extensive area that a short introduction like this can and will only give a rough overview of the topic. Here the possibilities are shown which exist within standard LaTeX, i.e. without additional packages.

### tabular environment

The tabular environment is one of the standard LaTeX environments and is used to create tables.
\begin{tabular}[optional position]{column_1column_2...column_n}
column 1 entry & column 2 entry & ... & column n entry \\
...
\end{tabular}


The position determines the vertical alignment of the table. Three options are available:

• The alignment is done on the basis of the top row.
• The vertical alignment is based on the middle of the table. This is also the default value for tables, so it is not necessary to specify c.
• Here the alignment takes place at the bottom line.

Alignment of tables
\begin{tabular}[t] \begin{tabular}[c] \begin{tabular}[b]
 Top Top Top A B C 1 2 3
 A B C Center Center Center 1 2 3
 A B C 1 2 3 Bottom Bottom Bottom

#### column formatting

The columns are formatted individually. On the one hand, the desired alignment can be set:

• for a left-justified column.
• for a centered column.
• for a right-justified column.

\begin{tabular}{lcr}
1 column & 2 column & 3 column \\
left-justified & centered & right-justified \\
left-justified column & centered column & right-justified column\\
\end{tabular}

 1 column 2 column 3 column left-justified centered right-justified left-justified column centered column right-justified column

The width of the column depends on the widest cell (and thus on the widest entry in a cell) within the column.

• @{Text} This creates a column in which each line automatically contains the entry Text.
• p{width}This creates a column with a fixed or predefined width. If the text within a cell, this column, is too long, it will be wrapped automatically. If a line break is to be set manually, it must be set with the \newline command.

By using the pipe sign, you can insert a visible seperator (a line) between the individual table columns.

• The pipe sign | draws a vertical line over the entire height of the table. If it is placed between two columns, the separator between them results. However, it can also be set at the beginning and end of the column definition to indicate the left or right margin of the table.

Within a table, you can also mix columns with different alignment and fixed width and fixed contents.

\begin{tabular}{|l|c|r|p{1.5cm}|@{ column 5 }|}
left & center & right & fixed width with 1.5cm\\
\end{tabular}

 |   left   | center |   right |   fixed |column 5   | |           | | |   width |                | |           | | |   with |                | |           | | |   1.5cm |                |
##### Align all columns equally

If the table requires only one type of column or if a type repeats itself, the star operator * can also be used.

\begin{tabular}{*{number n}{column type}}
...
\end{tabular}

\begin{tabular}{*{3}{p{2cm}}}
1 column & 2 column & 3 column \\
left-justified & centered & right-justified \\
left-justified column & centered column & right-justified column\\
\end{tabular}

 1 column left-justified left-justified column 2 column centered centered column 3 column right-justified right-justified column

#### Rows

• The & sign separates the different columns within a row.
• The \hline command creates a horizontal line across the entire width of the table. If it is written before the line, there is a line above the line; if it is written after the line, there is a line below it.
• With the command \cline{i-j} a horizontal line is drawn from column i to column j.
• With \multicolumn{number n}{orientation}{content} n columns (within a row) are combined to a new cell. The alignment of the cell can be l, c, r or p{width}. In this new cell the content will be set.
• \vline creates a vertical line over the height of the row in which the command stands.
##### Error message columns

A typical error message for a table is the following message:

! Extra alignment tab has been changed to \cr.

It appears if more columns were used in a table than were defined. Either too few columns were simply defined or an entry contains & sign as a character and this was not masked with backslash \ (for example \&).

##### Horizontal lines

In a case that horizontal lines appear too wide, that is too far beyond the left or right end of the table there is an opportunity by means of @{} to limit this extent.

\begin{tabular}{lll}
\hline
A & B & C \\
\end{tabular}

A   B   C
\begin{tabular}{@{}lll@{}}
\hline
A & B & C \\
\end{tabular}

A   B   C

##### cline

The \cline command works similarly to the \hline command. The difference is that you have to specify a start point and an end point. The specification is made in the form of the column numbers. \cline{< number of beginning column > - < number of column to end >}

\begin{tabular}{llll}
\hline
A & B & C & D\\
\cline{1-3}
1 & 2 & 3 & 4\\
\cline{1-2}
D & C & B & A \\
\cline{1-1}
4 & 3 & 2 & 1 \\
\cline{1-1}\cline{3-4}
\end{tabular}

A   B   C   D
1   2    3   4
D   C   B   A
4    3   2   1

##### multicolumn

When using multicolumn, it must always be noted that if vertical lines were used, these must be set again in the multicolumn command.
Example: \multicolumn{number n}{orientation|}{content}

\begin{tabular}{|c|c|c|l|r|}
\hline
\multicolumn{3}{l}{test} & A & B \\
\hline
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}

test             A   | B   |
| 1   | 2 | 3 | 4   | 5   |
\begin{tabular}{|c|c|c|l|r|}
\hline
\multicolumn{3}{|l}{test} & A & B \\
\hline
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}

|test             A   | B   |
| 1   | 2 | 3 | 4   | 5   |

##### vline

The \vline command sets a vertical line at cell height to the position where it was set. It can also be set multiple times in a cell.

\begin{tabular}{llll}
A & B \vline & C\hspace*{1cm} \vline \ \vline & D\vline \\
1 & 2 & 3 & 4 \\
\end{tabular}

A     B |  C        || D
1     2     3           4

#### Example

\begin{tabular}{|l|c|r|p{5cm}|@{ column 5 }|}
\hline
left & center & right & With fixed width of 5 cm and line break \\
\cline{1-3}
column 1 & column 2 & column 3 & Column 5 \newline always shows the same\\
\hline
\multicolumn{3}{|c|}{not much} & but so are many tables\\
\cline{4-4}
Still & one & row & without much content -- but that was not in the foreground\\
\hline
An & empty & row & is also possible \\
& & & \\
Even & twice & & \\
\hline
& & & \\
& & & \\
Just & the & automatic & column is a little bit different \\
\hline
And & again & with & vline \vline also \vline it looks \vline weird\\
\hline
\end{tabular}


### tabular ⋆ environment

The difference between tabular and tabular is that in the tabular ⋆ environment the width of the table can be set.

\begin{tabular*}{width}[optional position]{column_1column_2...column_n}
column 1 entry & column 2 entry & ... & column n entry \\
...
\end{tabular*}

Since the result often does not correspond to what the user imagines, this environment should not be used. Better use the packages tabulary or tabularx.

### tabbing environment

With the tabbing environment it is possible to set tables as it was usual on a typewriter in the past.

\begin{tabbing}
Text \= more text \= even more text \= last text \\
Second row \>  \> more content \\
...
\end{tabbing}

Especially beginners should not use tabbing, because there is a large selection of table packages and the handling of the commands within the tabbing environment is not very intuitive. For the sake of completeness, the commands are listed here, although I would advise against their use.
• With \= a Tab Stop is set.
• The command \> causes LaTeX to advance to the next tab stop.
• With \< it is possible to set something to the left of the local border without changing the border.
• \+ This moves the left edge of the next and all following commands one tab to the right.
• \- Moves the left border of the next and all following commands one tab to the left.
• With \‘ the text to the left of it is set before the next tab stop and the text to the right of it is set at the tab stop.
• With the command \~ everything in the column is moved to the previous column.
• With \kill at the end of the line, the set tab stop for the following lines is considered, but the text or the content in the line ending with \kill is not printed.

### array environment

The array environment can be used to set mathematical tables respectively table with math content within the math mode.

\begin{array}{column_1column_2...column_n}
column 1 entry & column 2 entry & ... & column n entry \\
...
\end{array}

The number of columns and their orientation are defined by the letters l (left-aligned), c (centered) and r (right-aligned) as in the tabular environment. Again, the individual columns are separated by the & sign and the line ends with a line break \\.