Contents

Creating Tables in LaTeX

Creating tables in LaTeX\LaTeX is different from creating them in Excel or a spreadsheet application. In spreadsheets, row and column borders are already visible. In LaTeX\LaTeX, tables are written with specific syntax so the result matches the layout you need.

The most common environment is tabular. After \begin{tabular}, you write the column specification using:

  • l for left alignment,
  • c for centered alignment,
  • r for right alignment,
  • p{...} for a paragraph column with a fixed width.

Columns are separated by &, and each row ends with \\. Horizontal lines are created with \hline, while vertical lines are added with | inside the column specification.

Here is a simple example of a table without borders:

\begin{table}[h!]
\centering
\begin{tabular}{cllll}
No & Name  & Class & City          & Score \\
1  & Andi  & VII A & Lampung       & 80    \\
2  & Budi  & VII A & Metro         & 78    \\
3  & Citra & VII B & Pringsewu     & 85    \\
4  & Dini  & VII B & Bandar Jaya   & 88    \\
5  & Eko   & VII C & Gunung Sugih  & 82    \\
\end{tabular}
\end{table}

The result looks like this.

Table without lines in LaTeX

In {cllll}, the first column is centered and the remaining columns are left aligned. If you want visible borders between rows and columns, use vertical bars in the specification and \hline inside the table.

\begin{table}[h!]
\centering
\begin{tabular}{|c|l|l|l|l|}
\hline
No & Name  & Class & City         & Score \\ \hline
1  & Andi  & VII A & Lampung      & 80    \\ \hline
2  & Budi  & VII A & Metro        & 78    \\ \hline
3  & Citra & VII B & Pringsewu    & 85    \\ \hline
4  & Dini  & VII B & Bandar Jaya  & 88    \\ \hline
5  & Eko   & VII C & Gunung Sugih & 82    \\ \hline
\end{tabular}
\end{table}

Table with lines in LaTeX

To merge rows, use the multirow package.

\usepackage{multirow}

Example:

\begin{tabular}{|c|c|c|c|}
\hline
No & District & Village    & Note \\ \hline
\multirow{3}{*}{1} & \multirow{3}{*}{Bangunrejo} & Sidodadi  & A \\ \cline{3-4}
                   &                             & Srikaton  & B \\ \cline{3-4}
                   &                             & Mulyojati & C \\ \hline
\end{tabular}

The command \multirow{3}{*}{Bangunrejo} makes the cell span three rows. To draw a partial horizontal line, use \cline{3-4}.

Merging rows in LaTeX tables

To merge columns, use \multicolumn.

\begin{tabular}{|c|c|c|c|c|}
\hline
\multicolumn{3}{|c|}{Student Data} & \multicolumn{2}{c|}{Scores} \\ \hline
No & Name & Class & Math & Science \\ \hline
1  & Andi & VII A & 80   & 78      \\ \hline
2  & Budi & VII B & 85   & 81      \\ \hline
\end{tabular}

The command \multicolumn{3}{|c|}{Student Data} merges three columns into one centered cell with vertical lines on both sides.

Merging columns in LaTeX tables

Both techniques can be combined in the same table.

\begin{tabular}{|c|c|c|c|}
\hline
\multirow{2}{*}{No} & \multirow{2}{*}{Name} & \multicolumn{2}{c|}{Scores} \\ \cline{3-4}
                    &                       & Midterm & Final \\ \hline
1 & Andi & 80 & 85 \\ \hline
2 & Budi & 78 & 82 \\ \hline
\end{tabular}

Merging rows and columns in LaTeX tables

If the document uses landscape orientation or the table is too wide, you can rotate it with the rotating package.

\usepackage{rotating}

Example:

\begin{sidewaystable}
\centering
\begin{tabular}{|c|c|c|c|c|c|}
\hline
No & Name & Class & Score 1 & Score 2 & Score 3 \\ \hline
1  & Andi & VII A & 80 & 82 & 84 \\ \hline
\end{tabular}
\end{sidewaystable}

Landscape table in LaTeX

Another option is the adjustbox package.

\usepackage{adjustbox}
\begin{table}[h!]
\centering
\begin{adjustbox}{angle=90}
\begin{tabular}{|c|c|c|c|}
\hline
No & Name & Class & Score \\ \hline
1  & Andi & VII A & 80    \\ \hline
\end{tabular}
\end{adjustbox}
\end{table}

Rotated table with adjustbox in LaTeX

To control table placement, the usual form is:

\begin{table}[h!]
...
\end{table}

Common placement markers:

  • h = here, place the table as close as possible to the current location,
  • t = top, place it at the top of the page,
  • b = bottom, place it at the bottom of the page,
  • p = page, place it on a dedicated float page,
  • ! = relax some internal placement rules,
  • H = place it exactly here, usually with the float package.

If each column needs a fixed width, use the p{...} column type.

\begin{tabular}{|c|p{3cm}|p{5cm}|}
\hline
No & Name & Description \\ \hline
1 & Andi & A student with a longer description so the content wraps inside the cell. \\ \hline
2 & Budi & Another example of a fixed-width column. \\ \hline
\end{tabular}

Fixed column width in LaTeX

If you want to control the total width of the table and let the columns stretch automatically, use tabularx.

\usepackage{tabularx}
\begin{tabularx}{\textwidth}{|c|X|X|}
\hline
No & Name & Description \\ \hline
1 & Andi & Columns of type X automatically share the remaining horizontal space. \\ \hline
2 & Budi & This is useful for tables with longer text content. \\ \hline
\end{tabularx}

Table width with tabularx in LaTeX

To color table cells or rows, use the xcolor package with the table option.

\usepackage[table]{xcolor}

Example:

\begin{tabular}{|c|l|c|}
\hline
\rowcolor{gray!30}
No & Name & Score \\ \hline
1 & Andi & 80 \\ \hline
2 & Budi & 85 \\ \hline
\rowcolor{yellow!20}
3 & Citra & 90 \\ \hline
\end{tabular}

Colored table in LaTeX

That concludes this short guide to creating tables in LaTeX\LaTeX. You can expand these examples further to match the document you are writing.

Related Content