Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The horizontal lines in tabular environment can be constructed. What is the difference between \toprule, \midrule, \bottomrule and specifying horizontal line using \hline?

share|improve this question
add comment

1 Answer

up vote 40 down vote accepted

Short answer.

\toprule, \midrule and \bottomrule are improved versions of \hline; the standard \hline behaves (roughly) the same whether it is placed at the top, at the bottom or in the middle of a table. The improved versions \toprule and \bottomrule are thicker (in fact, \midrule is also slightly thicker) and they handle vertical spaces around them in a better way. As the documentation of booktabs states:

The routines described below are to enable the easy production of tables such as should appear in published scientific books and journals. What distinguishes these from plain LaTeX tables is the default use of additional space above and below rules, and rules of varying ‘thickness’.

Not so short answer.

Lines produced with \hline have a default width controlled by \arrayrulewidth which is set (by the standard classes) to 0.4pt. If you want to change the thickness, you need to set a different value for \arrayrulewidth.

On the other side, \toprule and \bottomrule have a default thicknes controlled by \heavyrulewidth which is set to 0.08em by default; the default thicknes for \midrule is given by the length \lightrulewidth, with an initial value of 0.05em. So, by default, \toprule and \bottomrule are approximately twice as thick as a line drawn with \hline and \midrule is slightly thicker than a line produced with \hline.

Additionally, \toprule, \midrule and \bottomrule have an optional argument

\toprule[<length>]
\bottomrule[<length>]
\middlerule[<length>]

allowing to easily modify their thickness.

The lines defined by booktabs also have a special management of the space below and above them; this space is controlled by the following lengths:

  1. For \toprule: the space above is given by \abovetopsep (set to 0pt by default); the space below is given by \belowrulesep (default 0.65ex).

  2. For \bottomrule: the space above is given by \aboverulesep (set to 0.4ex by default) and the space below is given by \belowbottomsep (default 0pt).

  3. For \midrule, the space above is controlled by \aboverulesep and the space below is given by \belowrulesep.

The fact that the length parameters \heavyrulewidth, \lightrulewidth, and \cmidrulewidth are specified in fractions of em, and the spaces above and below the rules are given in fracions of ex, is very nice, typographically speaking, because the rule widths and their spacing increase automatically if the default font size is changed from 10pt to 11pt or 12pt. E.g., \heavyrulewidth is .8pt, .876pt, and .94pt, respectively, for default font sizes of 10pt, 11pt, and 12pt. This is a major difference to \hline, which equals 0.4pt by default regardless of the default font size.

The booktabs package also offers \cmidrule which is an improved version of the standard LaTeX \cline command designed to draw a sub-rule to extend over only some of the columns.

One of the disadvantages of the standard \cline command is that it draws a rule that extends to the full width of the columns specified in the argument, so two consecutive \clines will crash into each other (unless, for example, extra precaution is taken with \extracolsep). To easily prevent this crash, the improved \cmidrule offers optional trimming specifications; these specifications go in parentheses with no spaces separating them. The possible specifications are r, r{<length>}, l and l{<length>} (or any combination of these), where r and l indicate whether the right and/or left ends of the rule should be trimmed. The forms without explicit argument are equivalent to r{\cmidrulekern} and l{\cmidrulekern}, where \cmidrulekern defaults to 0.5em.For example,

\cmidrule(lr{.75em}){1-3} 

gives a rule extending from column one to column three with a default left trim of 0.5em and 0.75em right trim.

\cmidrule has also an optional argument to change the thickness; the complete syntax is

\cmidrule[<length>](<trimming>){a–b}

The default value for <length> is \cmidrulewidth (0.03em); the last argument gives the column numbers to be spanned.

Even with simple tables you can see the difference between the results obtained using the default LaTeX \hline and the rules produced with the help of booktabs:

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\noindent\begin{tabular}{*{3}{c}}
\hline
Header1 & Header 2 & Header3 \\
\hline
Column1a & Column2a & Column3a \\
Column1b & Column2b & Column3b \\
Column1c & Column2c & Column3c \\
Column1d & Column2d & Column3d \\
\hline
\end{tabular}\quad
\begin{tabular}{*{3}{c}}
\toprule
Header1 & Header 2 & Header3 \\
\midrule
Column1a & Column2a & Column3a \\
Column1b & Column2b & Column3b \\
Column1c & Column2c & Column3c \\
Column1d & Column2d & Column3d \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

share|improve this answer
2  
One can also trim them from either left or right with \cmidrule. –  Harish Kumar Jan 27 at 4:59
3  
Great answer! You might want to add that the length parameters \heavyrulewidth, \lightrulewidth, and \cmidrulewidth are specified in fractions of em (.08em, .05em, and .03em, resp) by default. This is very nice, typographically speaking, because the rule widths increase automatically if the default font size is changed from 10pt to 11pt or 12pt. E.g., \heavyrulewidth is .8pt, .876pt, and .94pt, resp., for default font sizes of 10, 11, and 12 pt. This is a major difference to \hline, which equals .4pt by default regardless of the default font size. –  Mico Jan 27 at 5:52
    
@HarishKumar I added some information about \cmidrule to my edited answer. Thanks for the suggestion. –  Gonzalo Medina Jan 27 at 22:33
    
@Mico I've incorporated (almost verbatim) your comment into my edited answer. Thanks for the suggestion. –  Gonzalo Medina Jan 27 at 22:33
    
I would move the picture from the end nearer to the top in the "short answer". I actually think the picture is the shortest answer there is! –  Juan A. Navarro Jan 29 at 9:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.