Tables

This guide explains how to create and customize tables in LaTeX: changing size/spacing, merging cells, applying color to rows or cells, and so on.

circle-info

One thing you should know: The tabularenvironment is to typeset the tabular material (which is what is most commonly referred to as “the table”), whereas the table environment is a container that handles the automatic positioning of the tabular material on the page. The table environment will “float” such that the resulting page breaks are ideal. Referencesarrow-up-right here!

table environment

In summary, the table environment is just a "wrapper" of the tabular environment - which is real "table" environment. table will take responsibility for setting the position, caption, label, alignment of the table, and more... Let's look at table syntax:

\begin{table}[position]
    \centering
    \begin{tabular}{...}
    \end{tabular}
    \caption{This is a table!}
    \label{table:1}
\end{table}

Let's breakdown the syntax:

  • position: The parameter for setting position. There are several values that can be passed in:

value
meaning

h

Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot - this can be weird sometimes)

t

Position at the top of the page.

b

Position at the bottom of the page.

p

Put on a special page for floats only.

!

Override internal parameters LaTeX\LaTeX uses for determining "good" float positions.

H

Places the float at precisely the location in the LATEX code. Requires the float package. This is somewhat equivalent to h!.

The default placement identifier is [btp], which means LaTeX\LaTeX is allowed to place the figure at the bottom of the page/column; top of the page/column; or if the float is quite tall (including the caption), all on its own on a float page without any text. Therefore, in order to achieve the absolute positioning that you want, you may need to use H from the float package.

circle-info

float sometimes may mess up with the caption positioning, make it move to bottom of the table instead of the default top position. In order to fix it, use this snippet:

  • \centering: Center the table relative to the float container element (the table environment is a container). However, this may make all the text below the table centered, so the best way should be use the table environment.

  • tabular: the environment for typesetting "table".

  • \caption: Set the caption for the table.

  • \label: If you need to reference the table within your document, set a label with this command. Check #cross-ref

circle-info

\label must be placed below \caption.

tabular environment

Let's start with an example of typesetting a table:

Let's breakdown the syntax:

  • {|c|c|c|} - everything inside this pair of curly brackets represents the layout of the table.

    • Every pipes (|) represent a vertical line in the table, which creates columns.

    • c means center, which means your content in that column will be centered (alternative values are l for left and r for right).

    --> So in this case, there are 3 columns, and the text inside each one of them are centered.

  • \hline - creates a horizontal line for the table. (there is no restriction on the number of times you can use this command.)

  • The & sign splits the content to its own cell, and end a row with \\.

tabular environment with fixed-width

If you want your table to have fixed width instead of dynamically adjusted, you can achieve that by modifying the layout (the second curly brackets)

p{'width'}

paragraph column with text vertically aligned at the top

m{'width'}

paragraph column with text vertically aligned in the middle (requires array package)

b{'width'}

paragraph column with text vertically aligned at the bottom (requires array package)

Combining rows and columns

Columns can be merged to create larger table cells using \multicolumn command.

The \multicolumn command use the following syntax:

  • num_cols - the number of columns to merge

  • alignment - how the content inside that merged cell be aligned

circle-info

Using \cline{i-j} can create partial horizontal line beginning in column i and ending in column j. (which may be useful for creating table that omits a specific cell)

To combine rows, you need to import the multirow package. Here is how it works:

Customize your tables

Line width and cell padding

We can adjust the line width and cell padding by overriding some internal LaTeX\LaTeX command.

command
meaning

\setlength{\arrayrulewidth}

Sets the thickness of the borders of the table.

\setlength{\tabcolsep}

The space between the text and the left/right border of its containing cell.

\renewcommand{\arraystretch}

The height of each row.

References

Last updated