I would like to centre only the numbers in the following table. I have attempted some variations of the centering command, but they affect the entire table (I'm relatively new to Latex).
MWE:
\documentclass{article}
\usepackage{booktabs, threeparttable}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabular}{@{}p{0.18\textwidth}*{6}{L{\dimexpr0.15\textwidth-2\tabcolsep\relax}}@{}}
\toprule
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c}{\bfseries Science Faculty} \\
\cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}
& F, S, H Courses & W Courses & F, S, H Courses & W Courses & F, S, H Courses & W Courses \\
\midrule
First-year & 18 & 36 & Variable & Variable & 18 & 36 \\
Second-year & 18 & 36 & Variable & Variable & 24 & 48 \\
Third-year & 18 & 36 & Variable & Variable & 36 & 72 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
Best Answer
You could use a
tabularx
environment -- pre-set to a width of\textwidth
-- instead of atabular
environment. Doing so would let you dispense with the tedious (and error-prone) hand calculations of the required widths of columns 2 through 7. In the example below, theL
column-type is for ragged-right material, while theC
column type is for center-set material; both column types are based on theX
column type of thetabularx
package. The main column type isC
since columns 2 to 7 in most rows should be centered; theL
column type is used for the column headers "F, S, H Courses" and "W Courses"; note that there's no need to specify line breaks in these headers explicitly.In the image below, the thin horizontal line below the
\bottomrule
is created by\hrule
; it's placed there purely to demonstrate that the table's width is equal to\textwidth
.