The tabular
environment is the most basic way to create a table in LaTeX and doesn't require any other packages.
\begin{tabular}{|lcr||}
left aligned column & center column & right column \\
\hline
text & text & text \\
text & text & text \\
\end{tabular}
The parameter (|lcr||
in the example) is called the table specification and tells LaTeX how many columns there are and how they are supposed to be formatted. Each letter represents a single column. Possible values are:
Character | Meaning |
---|---|
l | left aligned column |
c | centered column |
r | right aligned column |
p{'width'} e.g. p{5cm} | paragraph column with defined width |
| (pipe character) | vertical line |
|| (2 pipes) | 2 vertical lines |
Cells are seperated by the &
character. A row is ended by 2 back slashes \\
.
Horizontal lines can be inserted by using the \hline
command.
Tables are always formatted to be wide enough to include all the content. If a table is to big, LaTeX will print overfull hbox
warnings. Possible solutions include using the p{'width'}
specifier or other packages like tabularx
.
A table with column headings spanning over several columns can be created using the command \multicolumn{cols}{pos}{text}
.
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
&\multicolumn{3}{|c|}{Income Groups}\\
\cline{2-4}
City&Lower&Middle&Higher\\
\hline
City-1& 11 & 21 & 13\\
City-2& 21 & 31 &41\\
\hline
\end{tabular}
\end{center}
Note that the command \multicolumn
has three mandatory arguments: the first argument specifies the number of columns over which the heading spans; the second argument specifies the position of the heading(l,c,r)
; and the third argument is the text for heading. The command \cline{2-4}
specifies the the starting column(here, 2) and ending column(here, 4) over which a line is to be drawn.