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.
Last updated
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.
Last updated
table
environmentIn 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:
Let's breakdown the syntax:
position
: The parameter for setting position. There are several values that can be passed in:
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.
!
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 is allowed to place the figure at the b
ottom of the page/column; t
op of the page/column; or if the float is quite tall (including the caption), all on its own on a float p
age without any text. Therefore, in order to achieve the absolute positioning that you want, you may need to use H
from the float
package.
\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
tabular
environmentLet'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-widthIf 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)
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
To combine rows, you need to import the multirow
package. Here is how it works:
\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.
Override internal parameters uses for determining "good" float positions.
We can adjust the line width and cell padding by overriding some internal command.