Tables
Tables
- A table uses the
tabularenvironment. You first describe the columns:
\begin{tabular}{l c r}
left & centre & right \\
\end{tabular}
{l c r}means three columns: left-aligned, centred, right-aligned.
Rows and cells
- Inside,
&jumps to the next column and\\starts a new row:
\begin{tabular}{l l}
Apple & 5 \\
Banana & 3 \\
\end{tabular}
- A row must not have more cells than the columns you declared (LaTeX stops with
Extra alignment tab). Fewer is allowed: the missing cells stay empty.
Lines (optional)
- Add
|between column letters for vertical lines, and\hlinefor horizontal ones:
\begin{tabular}{|l|l|}
\hline
Apple & 5 \\ \hline
\end{tabular}
- For a cleaner look, professionals use the
booktabspackage instead of many lines.
Common mistakes
- More cells than columns. In
{ll}, the rowa & b & c \\has three cells, which gives! Extra alignment tab has been changed to \cr. - No column spec.
\begin{tabular}on its own reads your first cell as the spec and gives! LaTeX Error: Illegal character in array arg. - Forgetting
\\at the end of a row. The next row's cells run on in the same row.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Build a small two-column table with \begin{tabular}{l l}. Separate the two cells in a row with &, and end each row with \\. Add the rows for Bob (30) and Mei (25).
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Add lines to the table. Put \hline at the top, after the header row, and at the bottom, and use |l|l| in the column spec for vertical lines.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Make a three-column table with the spec {l c r}: left, centred and right-aligned. Add the header row Item & Qty & Price \\ and the row Pen & 2 & 1.50 \\.