The \cmidrule
command in LaTeX is part of the ‘booktabs’ package, which is commonly used to create professional-looking tables. \cmidrule
allows you to insert a partial horizontal line across selected columns in a table. Here’s a complete guide on how to use \cmidrule
:
Step 1: Load the ‘booktabs’ Package
Make sure to include the booktabs
package in the preamble of your LaTeX document. You can do this by adding the following line:
\usepackage{booktabs}
Step 2: Create Your Table
Create a table using the tabular
environment. You can define the number of columns and their alignment within the curly braces {}
. Specify the columns using the alignment options, such as l
for left-aligned, c
for center-aligned, and r
for right-aligned.
documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[]
\centering
\begin{tabular}{cccc}
\toprule
Name & City & Age & Gender \\
\midrule
Akram & Los Angeles & 18 & Male \\
Nancy & New York & 21 & Female \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Output
Step 3: Add ‘\cmidrule’ to Specify Partial Horizontal Lines
To insert a partial horizontal line using \cmidrule
, use the following syntax:
\cmidrule[trim]{start-end}
trim
: This optional argument allows you to adjust the length of the rule. It can take values likel
,r
, or a combination likelr
. This trims the line on the left, right, or both sides, respectively.start-end
: Specify the columns where the\cmidrule
should start and end. For example,\cmidrule{2-3}
inserts a line from the second to the third column.
Example of Using ‘\cmidrule’
documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[]
\centering
\begin{tabular}{cccc}
\toprule
Name & City & Age & Gender \\
\cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
Akram & Los Angeles & 18 & Male \\
Nancy & New York & 21 & Female \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Output
In this example, partial horizontal lines are added in all columns.
Adjusting Thickness and Styling
You can customize the thickness of the \cmidrule line using the optional argument for line width, like \cmidrule[0.8pt](lr){2-3}
. Here, 0.8pt
represents the line width. For example:
documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[]
\centering
\begin{tabular}{cccc}
\toprule
Name & City & Age & Gender \\
\cmidrule[0.8pt](lr){1-1} \cmidrule[0.8pt](lr){2-2} \cmidrule[0.8pt](lr){3-3} \cmidrule[0.8pt](lr){4-4}
Akram & Los Angeles & 18 & Male \\
Nancy & New York & 21 & Female \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Output
Trim Options
The trim option in \cmidrule allows you to control the space before and after the rule. For example, \cmidrule(l{7pt}r{4pt}){1-1}
trims 7pt from the left and 4pt from the right in the first column.
documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[]
\centering
\begin{tabular}{cccc}
\toprule
Name & City & Age & Gender \\
\cmidrule(l{7pt}r{4pt}){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
Akram & Los Angeles & 18 & Male \\
Nancy & New York & 21 & Female \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Output
This guide should help you get started with using \cmidrule
in LaTeX tables. In summary, \cmidrule
is a versatile command that allows for the creation of professional-looking tables with varying line styles, thicknesses, and positions. Experimenting with different options and combining \cmidrule
with other table formatting commands from the booktabs
package provides a powerful toolset for designing aesthetically pleasing tables in LaTeX.