[Tex/LaTex] Help with a booktabs table

booktabstables

I'm a relatively new user to Latex, and this is the first time I am attempting to construct a table for my thesis. I would like the table to look like this:

enter image description here

Following some research on the internet, I came up with the following code (ignore the differences in the column headings between the Latex code and the word document)

\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{lrrrr}
\toprule
\multicolumn{2}{c}{{BCom} &
\multicolumn{2}{c}{B.Bus.Sci} \\
\cmidrule{2-3} \cmidrule{4-5} &
Number of courses required to pass & Cumulative Total of Courses && Number of courses &         Cumulative Total of Courses
\midrule
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabular}
\label{table:mr}
\end{table}

it is not working at all!

Best Answer

A better version:

\documentclass{article}
\usepackage{booktabs}
\usepackage[margin=1in]{geometry}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{document}
  \begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{@{}p{0.12\textwidth}*{4}{L{\dimexpr0.22\textwidth-2\tabcolsep\relax}}@{}}
\toprule
& \multicolumn{2}{c}{BCom} &
\multicolumn{2}{c}{B.Bus.Sci} \\
\cmidrule(r{4pt}){2-3} \cmidrule(l){4-5}
& Number of courses required to pass & Cumulative Total of Courses & Number of courses &         Cumulative Total of Courses\\
\midrule
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabular}
\label{table:mr}
\end{table}
\end{document}

enter image description here

Why your code didn't compile?

  1. You missed a & at the begining of

    \multicolumn{2}{c}{{BCom} & \multicolumn{2}{c}{B.Bus.Sci} \\
    

    making the first entry to go in to the first column.

  2. You have a && in the line

    Number of courses required to pass & Cumulative Total of Courses &&
         Number of courses &         Cumulative Total of Courses
    

    It should be & and you should put an & at the start of line. And at the end of this line you missed \\ just before \cmidrule.

Now the modifications

After correcting all the above, I have introduced a new column type with the help of array package.:

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\raggedright is added to avoid ugly justification effects since the columns can be narrow and words can vary in length. This takes care of bad boxes also.

I have used this L column with a width

\dimexpr0.22\textwidth-2\tabcolsep\relax

through

*{4}{L{\dimexpr0.22\textwidth-2\tabcolsep\relax}}

instead of repeating.

As a side note, the \cmidrule can be shortened from left or right by

\cmidrule(r{4pt}){2-3}

Here {4pt} is the amount by which we shorten. It can be left off like \cmidrule(r){4-5}.

Related Question