11 Tables

A table is a rectangular array of cells, arranged in rows and columns, and the relevant LaTeX environment is the tabular environment. I recommend you usually nest this inside a table environment, which will make sure tables are floated to the top (or bottom) of a page, just like the figure environment does for graphics, and enables \caption as well - however this is not strictly necessary.

11.1 Basic Tables

The tabular environment uses a very similar syntax to the array environment we used for matrices, so there isn't much more to be said. In addition to the usual l, c and r you may find the p alignment useful; p takes a mandatory width argument, e.g. p{0.8\textwidth} and sets out the column's contents as if it were a paragraph of text - in other words it breaks lines for you, and justifies the text properly.

\begin{table}[t] % put at top of page if possible 
\begin{tabular}{l|c|c}
Run & Displacement & Force \\
\hline
1 & 3.5cm & 1.8N \\
2 & 3.0cm & 1.6N \\
3 & 2.5cm & 1.35N
\end{tabular}
\caption{Results of the weighted spring experiment}\label{table:spring}
\end{table}

The vertical bars in the argument of the tabular environment tell LaTeX to separate those columns with a vertical line, and the \hline command creates a horizontal line right across the table; if you only the horizontal line across certain columns you can use the \cline command and tell it which columns you want,e.g. .

\begin{table}[t] % put at top of page if possible 
\begin{tabular}{l|c|c}
Run & Displacement & Force \\
\hline
1 & 3.5cm & 1.8N \\ \cline{2-3}
  & 3.55cm & 1.82N \\ \cline{2-3}
  & 3.48cm & 1.79N \\ \cline{2-3}
2 & 3.0cm & 1.6N   \\ \cline{3-3}
  &       & 1.602N \\ \cline{3-3}
3 & 2.5cm & 1.35N
\end{tabular}
\caption{Results of the weighted spring experiment}\label{table:spring}
\end{table}

11.2 Spanning Multiple Columns

Sometimes you might want an entry in a table to span several columns. If you use the multicols package you can do this with the \multicolumn command. It takes three arguments: the first says how many columns to span; the second argument is the alignment to use (the usual l, c, or r, and possibly |); and the third is the content for those columns.

\begin{table}[t] % put at top of page if possible 
\begin{tabular}{l|c|c}
Run & Displacement & Force \\
\hline
1 & 3.5cm & 1.8N \\
2 & 3.0cm & 1.6N \\
3 & 2.5cm & 1.35N\\
4 & \multicolumn{2}{c}{My idiot lab partner dropped the weight}
\end{tabular}
\caption{Results of the weighted spring experiment}\label{table:spring}
\end{table}

11.3 Labelling Tables

If you want to reference a table, the best thing to do is put the \label command immediate after the caption. If you put it anywhere else, then you can get peculiar numbering. Aside from that, you can reference tables just like you reference anything else in LaTeX.

11.4 Other Tables

There are some things that you can't do with ordinary tables. For example, an ordinary table can never spread over more than one page, but you can use the longtables package for this. Also you might have a table which is just a bit too wide, and you want to set it out length-ways on the page - use the rotating package, which defines a sidewaystable for just this eventuality.

Summary


Previous page Next page