Table borders and lines in LaTeX (hline, cline, booktabs)
A | in the column argument draws a vertical line, \hline a horizontal line across the table and \cline{2-3} one under columns 2 to 3. The booktabs package offers \toprule, \midrule and \bottomrule for tables without vertical lines.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{booktabs}Draw a full grid
Put a | at every place of the column argument where a vertical line belongs, and \hline after every \\. The first \hline stands before the first row.
\begin{tabular}{|l|c|r|}
\hline
Item & Color & Price \\
\hline
Apple & red & 0.50 \\
\hline
Banana & yellow & 0.25 \\
\hline
\end{tabular}Draw a line under some columns only
\cline{2-3} draws the line from column 2 to column 3. For one column, write the same number twice: \cline{2-2}.
\begin{tabular}{|l|c|c|}
\hline
& Test 1 & Test 2 \\
\cline{2-3}
Anna & 12 & 15 \\
\hline
\end{tabular}Use the booktabs rules
\toprule and \bottomrule are thicker than \midrule, and all three leave more space above and below than \hline. \cmidrule(lr){2-3} draws a rule under columns 2 to 3; (lr) shortens it on the left and right so that two neighboring rules do not touch.
\begin{tabular}{lcc}
\toprule
& \multicolumn{2}{c}{Score} \\
\cmidrule(lr){2-3}
Name & Test 1 & Test 2 \\
\midrule
Anna & 12 & 15 \\
Ben & 9 & 14 \\
\bottomrule
\end{tabular}Double lines and thicker lines
\hline\hline and || draw double lines. The length \arrayrulewidth sets the thickness of \hline, \cline and |; its default is 0.4 pt. The braces around the example limit the change to this table.
{\setlength{\arrayrulewidth}{1pt}
\begin{tabular}{|l||c|}
\hline
Name & Score \\
\hline\hline
Anna & 12 \\
\hline
\end{tabular}}Pitfalls
\hline,\clineand thebooktabsrules have to stand at the start of the table or directly after\\. Anywhere else LaTeX stops with Misplaced \noalign.- Do not combine the
booktabsrules with|. The rules add space above and below themselves, and the vertical lines do not cross that space, so they get gaps. \multicolumn{1}{|c|}{text}adds or removes the vertical lines of a single cell.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
booktabs - Package
array - How-to Create a table in LaTeX
- How-to Color table rows, columns and cells in LaTeX
- Error Misplaced \noalign