table environments - tables with LaTeX

StartSearchLaTeX for Beginners F.A.Q.LaTeXfont 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:

Alignment of tables
\begin{tabular}[t] \begin{tabular}[c] \begin{tabular}[b]
 
 
TopTopTop
ABC
123
 
ABC
CenterCenterCenter
123
 
ABC
123
BottomBottomBottom
 
 

column formatting

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

\begin{tabular}{lcr}
 
1 column & 2 column & 3 column \\  
left-justified & centered & right-justified \\  
left-justified column & centered column & right-justified column\\  
\end{tabular}
Column alignment in tables
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.

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

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}
columnn formating
|   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

Error message columns

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

! Extra alignment tab has been changed to \cr.
 
<recently read> \endtemplate
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}

Table example

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.

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 \\.

Questions and problems

Contact   Privacy Policy   disclosures

accept decline

This website uses cookies and pixel tags to ensure you get the best experience on our website. By using this website, you consent to the use of cookies.learn more about cookies see also our privacy policy