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.
\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:
\begin{tabular}[t] | \begin{tabular}[c] | \begin{tabular}[b] | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
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}
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}
| left | | center | | right | | fixed | |column 5 | |
| | | | | | width | | | | |
| | | | | | with | | | | |
| | | | | | 1.5cm | | | |
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 |
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 \&).
<recently read> \endtemplate
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
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}A B C D
\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}
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 |
\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 |
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
\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}
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}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.
column 1 entry & column 2 entry & ... & column n entry \\
...
\end{tabular*}
With the tabbing environment it is possible to set tables as it was usual on a typewriter in the past.
\begin{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.
Text \= more text \= even more text \= last text \\
Second row \> \> more content \\
...
\end{tabbing}
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}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 \\.
column 1 entry & column 2 entry & ... & column n entry \\
...
\end{array}