Find the table you need. Copy the code. Change the cells. Each example shows the LaTeX code and the table that LaTeX makes from it.
Do you prefer not to write the code? Make the table in the LaTeX Tables Generator and copy the code it makes. Each example below tells you how.
Cells go from left to right. Rows go from top to bottom.
The shaded columns show the alignment only. LaTeX does not draw them.
A table float must fit on one page. For a table that continues on
the next page, see Long tables.
Write the cells row by row. The letters after tabular align the columns.
\begin{tabular}{lcr}
Name & Type & Price \\
Apple & Fruit & 1.20 \\
Leek & Vegetable & 0.85 \\
\end{tabular}
{lcr} makes three columns: left, centre and right aligned.& between the cells and \\ at the end of each row.& signs to make the code easy to read.tabular alone has no caption and no number. To add them, put it in a table float, as in the next example.In our editor Type in the cells. Click the alignment buttons on the toolbar to align the text. The code under the table changes as you type.
Save this code as table.tex. Compile it two times with pdflatex table.tex,
or open it in an online editor such as Overleaf. The second run puts the table number in the text.
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption} % space under a caption above a table
\begin{document}
Table~\ref{tab:prices} shows the prices.
\begin{table}[htbp]
\centering
\caption{Prices of fruit}
\label{tab:prices}
\begin{tabular}{lr}
\toprule
Fruit & Price \\
\midrule
Apple & 1.20 \\
Pear & 1.50 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
\usepackage) before \begin{document}.\label after \caption. Give each table a different label.Table~\ref{tab:prices} prints the table number. The ~ keeps the word and the number on one line.\caption after the tabular to put the caption below the table.caption package, a caption above a table touches its top line.In our editor In the options under the code, select Caption: Above or Below and type the text and the label. Select Complete document to get the document with all the packages it needs.
The letters after \begin{table} tell LaTeX where it can put the table.
LaTeX uses the first place that is possible. The letters do not force a place.
| Letter | LaTeX can put the table |
|---|---|
h | here, where the code is, if it fits |
t | at the top of a page |
b | at the bottom of a page |
p | on a page that has only floats |
! | with fewer limits (for example, a larger table at the top) |
H | exactly here. Needs \usepackage{float}. |
Start with [htbp]: it lets LaTeX use all four places.
A table with no place that it fits goes to the end of the chapter or document.
Draw a grid when each cell needs a border.
\begin{tabular}{|l|c|r|}
\hline
Name & Type & Price \\
\hline
Apple & Fruit & 1.20 \\
\hline
Leek & Vegetable & 0.85 \\
\hline
\end{tabular}
| in the column letters draws a vertical line.\hline draws a horizontal line across all columns.\hline before the first row and after each \\.In our editor Click the all-borders button on the toolbar, or select Table › Add all borders.
Draw a line under some columns only, or between two columns.
\begin{tabular}{l|rr}
Name & 2024 & 2025 \\
\hline
Apple & 120 & 135 \\
Leek & 80 & 95 \\
\cline{2-3}
Total & 200 & 230 \\
\end{tabular}
\cline{2-3} draws a line under columns 2 to 3.{l|rr} draws one vertical line, between column 1 and column 2.\multicolumn{1}{|l|}{text} (see Align one cell).In our editor Click the border editing button on the toolbar. Then click the edges of the cells to add or remove lines. Hold Shift to change a full row or column.
Use double lines to separate the header from the data.
\begin{tabular}{|l||r|r|}
\hline
Name & 2024 & 2025 \\
\hhline{|=#=|=|}
Apple & 120 & 135 \\
\hline
Leek & 80 & 95 \\
\hline
\end{tabular}
|| in the column letters draws a double vertical line.\hline\hline draws a double horizontal line.\hhline so that the lines cross cleanly:
= is a double line under a column, - a single line,
| a vertical line, # a double line crossing a double vertical line.In our editor In border editing, select the double line button next to the border editing button. Then click the edges.
Use three horizontal rules and no vertical lines, as journals and books do.
\begin{tabular}{lrr}
\toprule
Name & 2024 & 2025 \\
\midrule
Apple & 120 & 135 \\
Leek & 80 & 95 \\
\bottomrule
\end{tabular}
\toprule goes above the header. \midrule goes under it.\bottomrule goes after the last row.| with booktabs. The rules have space around them, so vertical lines have gaps.@{} before and after the column letters, for example {@{}lrr@{}}, to make the rules end at the text.In our editor In the options under the code, select Style: Booktabs.
Change the font of one cell.
\begin{tabular}{ll}
\textbf{Bold} & \textit{Italic} \\
\uline{Underlined} & \texttt{Monospaced} \\
\textcolor{red}{Red text} & \textbf{\textit{Both}} \\
{\small Small} & {\large Large} \\
\end{tabular}
\uline needs \usepackage[normalem]{ulem}. Without normalem, ulem changes \emph to underline.\textcolor needs \usepackage{xcolor}.{\small Small} keep the size change in that cell.>{\bfseries}l. It needs \usepackage{array}.In our editor Select the cells. Click B, I or U, or the text colour button, on the toolbar.
Align one cell differently from its column.
\begin{tabular}{|l|l|}
\hline
\multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Taste} \\
\hline
Apple & Sweet \\
\hline
\multicolumn{1}{|r|}{Leek} & Mild \\
\hline
\end{tabular}
\multicolumn{1}{c}{Name} makes a cell of one column with its own alignment.{|c|} for the first column, {c|} for the next ones.In our editor Select the cells. Click the alignment buttons on the toolbar.
Make one cell across two or more columns, for example a header over a group of columns.
\begin{tabular}{lrrrr}
\toprule
& \multicolumn{2}{c}{2024} & \multicolumn{2}{c}{2025} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
Name & Sold & Price & Sold & Price \\
\midrule
Apple & 120 & 1.10 & 135 & 1.20 \\
Leek & 80 & 0.80 & 95 & 0.85 \\
\bottomrule
\end{tabular}
\multicolumn{2}{c}{2024} makes one centred cell across two columns.& less for each column it covers.\cmidrule(lr){2-3} draws a booktabs rule under columns 2 to 3. (lr) makes it shorter at the left and the right, so that two rules do not touch.In our editor Drag the mouse over the cells to select them. Click the merge button on the toolbar. Click the split button next to it to undo it.
Make one cell across two or more rows.
\begin{tabular}{|l|l|r|}
\hline
Type & Name & Price \\
\hline
\multirow{2}{*}{Fruit} & Apple & 1.20 \\
\cline{2-3}
& Pear & 1.50 \\
\hline
Vegetable & Leek & 0.85 \\
\hline
\end{tabular}
\multirow{2}{*}{Fruit} makes a cell across 2 rows. * makes it as wide as its text.&.\cline{2-3}, not \hline, so that the line does not cross the merged cell.*, for example {2cm}, or {=} in a column with a width.In our editor Select the cells in a column and click the merge button on the toolbar.
Make one cell across columns and rows together.
\begin{tabular}{|c|c|c|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*}{Block}} & A \\
\cline{3-3}
\multicolumn{2}{|c|}{} & B \\
\hline
C & D & E \\
\hline
\end{tabular}
\multirow in a \multicolumn.\multicolumn of the same width: \multicolumn{2}{|c|}{}.In our editor Select the block of cells and click the merge button on the toolbar.
Break the text of a cell into lines where you want.
\begin{tabular}{|l|l|}
\hline
\begin{tabular}[c]{@{}l@{}}Common\\ name\end{tabular}
& \makecell{Price\\ (per kg)} \\
\hline
Apple & 1.20 \\
\hline
Leek & 0.85 \\
\hline
\end{tabular}
\\ ends the row. To break a line in a cell, put the cell's text in a small tabular (as our editor does) or in \makecell.@{} removes the space around the small tabular, so the text lines up with the other cells.\makecell needs \usepackage{makecell}. It centres the lines. Write \makecell[l]{...} to align them left.p{3cm}), use \newline.In our editor Double-click a cell to edit it. Press Enter to start a new line in the cell.
Give a column a width. Its text wraps to more lines.
\begin{tabular}{|l|p{3.5cm}|>{\raggedright\arraybackslash}p{3.5cm}|}
\hline
Name & Justified text & Ragged right text \\
\hline
Apple & A sweet fruit that grows on trees in cool climates.
& A sweet fruit that grows on trees in cool climates. \\
\hline
\end{tabular}
p{3.5cm} makes a column 3.5 cm wide. The text wraps and aligns at the top.p column. In a narrow column, this makes wide spaces.>{\raggedright\arraybackslash} before p aligns the text left. \arraybackslash makes \\ end the row again. This needs \usepackage{array}.m{3.5cm} centres the text vertically and b{3.5cm} aligns it at the bottom. Both need array.l, c and r columns never wrap their text.In our editor Drag the right edge of the column header above the table, or right-click the header and select Set column width. Double-click the edge to remove the width.
Make a table as wide as the text on the page. Some columns wrap to fill the width.
\begin{tabularx}{\textwidth}{|l|X|X|}
\hline
Name & Description & Use \\
\hline
Apple & A sweet fruit that grows on trees. & Eat raw, bake or press for juice. \\
\hline
Leek & A mild vegetable of the onion family. & Cook in soups and stews. \\
\hline
\end{tabularx}
tabularx needs \usepackage{tabularx}. Its first argument is the width of the table.X columns wrap their text. They share the width that the other columns leave.\textwidth is the width of the text on the page. In a two-column document, use \columnwidth.>{\hsize=1.5\hsize}X and >{\hsize=0.5\hsize}X. The shares must add up to the number of X columns.In our editor In the options under the code, select Width: Wrap to text width. The columns with a width share the space in proportion to their widths.
Make the whole table larger or smaller, its text too, to fit a width.
\resizebox{\textwidth}{!}{%
\begin{tabular}{lrrrr}
\toprule
Name & 2022 & 2023 & 2024 & 2025 \\
\midrule
Apple & 101 & 110 & 120 & 135 \\
Leek & 62 & 71 & 80 & 95 \\
\bottomrule
\end{tabular}%
}
\resizebox{\textwidth}{!}{...} scales the table to the text width. ! keeps the proportions. It needs \usepackage{graphicx}.% signs at the line ends stop extra spaces.In our editor In the options under the code, select Width: Scale to text width or Scale to column width.
Make the rows higher or the space between the columns larger.
{ % The braces keep the changes local
\renewcommand{\arraystretch}{1.4}
\setlength{\tabcolsep}{12pt}
\begin{tabular}{lr}
\toprule
Name & Price \\
\midrule
Apple & 1.20 \\
Pear & 1.50 \\
\addlinespace
Leek & 0.85 \\
\bottomrule
\end{tabular}
}
\arraystretch multiplies the height of the rows. 1.4 is 40% higher.\tabcolsep is the space at each side of a column. The default is 6pt.\addlinespace (booktabs) adds space between two rows.table float, to change only this table.Give a cell or a row a background colour.
\begin{tabular}{lrr}
\rowcolor{gray!25}
Name & 2024 & 2025 \\
Apple & 120 & \cellcolor{green!25}135 \\
Leek & 80 & \cellcolor{red!25}75 \\
\end{tabular}
\usepackage[table]{xcolor}.\rowcolor goes at the start of the row. \cellcolor goes in the cell.green!25 is 25% green and 75% white.\definecolor{mygreen}{HTML}{C8F2C8} in the preamble.>{\columncolor{gray!15}}l in the column letters.In our editor Select the cells. Click the background colour button on the toolbar.
Give the rows two colours that alternate, so that long rows are easy to follow.
\begin{table}[htbp]
\centering
\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr}
Name & Price \\
\hline
Apple & 1.20 \\
Leek & 0.85 \\
Pear & 1.50 \\
Plum & 2.10 \\
\end{tabular}
\end{table}
\rowcolors{2}{gray!15}{white}: from row 2, odd rows are grey and even rows are white.\usepackage[table]{xcolor}.table float. If not, it changes all the tables after it.In our editor Give the rows alternating background colours. Our editor writes \rowcolors for four or more such rows.
Align the numbers of a column at the decimal point.
\begin{tabular}{lS[table-format=3.2]}
\toprule
Test & {Time (s)} \\
\midrule
A & 2.5 \\
B & 12.30 \\
C & 105.07 \\
\bottomrule
\end{tabular}
S column (needs \usepackage{siunitx}) aligns numbers at the decimal point.table-format=3.2 keeps space for 3 digits before the point and 2 after it.{Time (s)}, so that siunitx does not read it as a number.Write formulas in cells. Escape the characters that LaTeX uses for its code.
\begin{tabular}{ll}
Formula & $E = mc^2$, $\alpha \le \beta$ \\
Percent & 50\% \\
Ampersand & R\&D \\
Underscore & file\_name \\
Dollar & \$10 \\
Hash & \#1 \\
Braces & \{ \} \\
Backslash & \textbackslash{} \\
Tilde & \textasciitilde{} \\
Caret & \textasciicircum{} \\
\end{tabular}
$ signs.& ends the cell and a plain % starts a comment: the rest of the line is lost. Write \& and \%.In our editor In the options under the code, Escape special characters escapes them for you. Clear it to type LaTeX, such as $x^2$, in the cells.
Add notes under a table and marks in the cells that refer to them.
\begin{table}[htbp]
\centering
\begin{threeparttable}
\caption{Prices}
\begin{tabular}{lr}
\toprule
Name & Price \\
\midrule
Apple\tnote{a} & 1.20 \\
Pear & 1.50 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\footnotesize
\item[a] Price in March.
\end{tablenotes}
\end{threeparttable}
\end{table}
threeparttable makes the caption and the notes as wide as the table.\tnote{a} puts the mark in a cell. \item[a] gives the note's text.\footnote in a tabular: LaTeX does not print its text.Let a table with many rows continue on the next pages.
\begin{longtable}{lr}
\caption{Prices of fruit}\label{tab:prices} \\
\toprule
Name & Price \\
\midrule
\endfirsthead
\multicolumn{2}{l}{Table \thetable{} -- continued} \\
\toprule
Name & Price \\
\midrule
\endhead
\midrule
\multicolumn{2}{r}{Continued on next page} \\
\endfoot
\bottomrule
\endlastfoot
Apple & 1.20 \\
Pear & 1.50 \\
% ... many more rows ...
\end{longtable}
longtable needs \usepackage{longtable}. Do not put it in a table float: it makes its own caption.\endfirsthead start the table. The rows before \endhead start each next page.\endfoot end each page. The rows before \endlastfoot end the table.X columns, use xltabular.In our editor In the options under the code, select Long table: Split across pages. You can also repeat the header rows and add a "continued" line.
Put a table that is too wide on a page turned sideways.
\begin{landscape}
\begin{table}[htbp]
\centering
\caption{Monthly sales}
\begin{tabular}{l*{12}{r}}
Name & Jan & Feb & Mar & Apr & May & Jun
& Jul & Aug & Sep & Oct & Nov & Dec \\
\hline
Apple & 120 & 118 & 125 & 130 & 128 & 140
& 151 & 149 & 160 & 171 & 166 & 180 \\
\end{tabular}
\end{table}
\end{landscape}
landscape needs \usepackage{pdflscape} or \usepackage{lscape}. With pdflscape, PDF viewers show the page turned.\small, or make \tabcolsep smaller.table* in place of table to put the table across both columns.In our editor In the options under the code, select Placement: Rotate to a landscape page.
| Letters | Column |
|---|---|
l c r | Left, centred, right. As wide as the text. |
p{3cm} | 3 cm wide. Text wraps, aligned at the top. |
m{3cm} | Text wraps, centred vertically. Needs array. |
b{3cm} | Text wraps, aligned at the bottom. Needs array. |
X | Shares the free width. Text wraps. Needs tabularx. |
S | Numbers aligned at the decimal point. Needs siunitx. |
| || | A single or double vertical line. |
@{} | No space at this side of the column. |
*{4}{r} | The same as rrrr. |
>{\bfseries}l | Applies a command to all cells of the column. Needs array. |
To use a ragged-right column with a width many times, define it in the preamble (needs array):
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
Then write L{3cm} in the column letters.
| Package | For |
|---|---|
| booktabs | \toprule, \midrule, \bottomrule, \cmidrule |
| multirow | \multirow |
| hhline | \hhline: lines that cross cleanly |
| array | m, b, >{...} columns |
| tabularx | tabularx, X columns |
| xcolor | Colours; with [table]: \rowcolor, \cellcolor, \rowcolors |
| graphicx | \resizebox |
| makecell | \makecell: lines in a cell |
| siunitx | S columns |
| threeparttable | Notes under a table |
| longtable | Tables across pages |
| pdflscape | Landscape pages |
| float | The [H] placement |
In Beamer presentations, use \usepackage{colortbl} for colours in tables:
Beamer loads xcolor itself.
These characters are part of LaTeX's code. To print them in a cell, write:
\&\%\$\#\_\{\}\textasciitilde{}\textasciicircum{}\textbackslash{}\textless{}\textgreater{}| LaTeX says or does | Do this |
|---|---|
| Extra alignment tab has been changed to \cr | A row has too many & signs. A row of n cells has n − 1. Count a merged cell as the columns it covers. |
| Misplaced \noalign | A \hline or rule is not after a \\. Add the \\ at the end of the row before it. |
| Undefined control sequence \toprule (or \multirow, …) | Add the package to the preamble. See Packages. |
| Unknown column type X (or m, S, …) | Add the package. X also needs the tabularx environment, not tabular. |
| Overfull \hbox: the table goes into the margin | Wrap long text in a column with a width, or make the table as wide as the text. |
| The table is on another page | A float moves. Use [htbp]. For "exactly here", use [H] with the float package. |
| The reference shows ?? or a wrong number | Put \label after \caption. Compile again. |
| The text after % is missing | % starts a comment. Write \%. |
| The table goes past the bottom of the page | Use a long table. |
Open the LaTeX Tables Generator and select File › From LaTeX code. Paste the code of your table. Edit the table, then copy the new code.