LaTeX
  • How I learn LaTeX?
  • Installation and Customization
    • Online
    • Offline
  • LaTeX Basics
  • Text Manipulation
    • Fonts, Size and Styles
  • Math Manipulation
  • Advanced Math Manipulation
  • Lists
  • Tables
  • Pictures, Figures
  • Unit in LaTeX
  • Document Structure
  • Document Formatting
    • Headers and Footers, Page Numbering
    • Spacing in Paragraphs
    • Page size, Margins
    • Coloring
    • Footnotes
  • Multi-file LaTeX project
Powered by GitBook
On this page
  • table environment
  • tabular environment
  • tabular environment with fixed-width
  • Combining rows and columns
  • Customize your tables
  • References

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.

PreviousListsNextPictures, Figures

Last updated 1 year ago

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. 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.

!

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\LaTeXLATE​X 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.

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:

latex here
  • \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

\label must be placed below \caption.

tabular environment

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

\begin{tabular}{|c|c|c|}
 \hline
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\  
 cell7 & cell8 & cell9 \\
 \hline    
\end{tabular}

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.

\begin{tabular}{|l|l|l|l|} 
  \hline
  \multicolumn{4}{|c|}{Weather Forecast} \\
  \hline
  \multicolumn{4}{|c|}{For an unknown place on Earth} \\
  \hline
  Day & Min & Max & Description \\
  \hline
  Monday & 11 & 21 & A clear day with lots of sunshine. \\
  \hline
  Tuesday & 9 & 19 & Cloudy with rain, across many southern regions.\\
  \hline
\end{tabular}

The \multicolumn command use the following syntax:

\multicolumn{num_cols}{alignment}{contents}
  • num_cols - the number of columns to merge

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

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:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{center}
\begin{tabular}{ |c|c|c|c| } 
\hline
col1 & col2 & col3 \\
\hline
\multirow{3}{4em}{Multiple row} & cell2 & cell3 \\ 
& cell5 & cell6 \\ 
& cell8 & cell9 \\ 
\hline
\end{tabular}
\end{center}
\end{document}

Customize your tables

Line width and cell padding

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

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

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

LaTeX\LaTeXLATE​X
Positioning tables and figures - Overleaf
Tables - Overleaf
LaTeX Tables - Wikibook
References