Tables – Centering Individual Cells in Booktabs

tables

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).

enter image description here

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 a tabular 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, the L column-type is for ragged-right material, while the C column type is for center-set material; both column types are based on the X column type of the tabularx package. The main column type is C since columns 2 to 7 in most rows should be centered; the L 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.

enter image description here

\documentclass{article}
\usepackage{booktabs, threeparttable}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabularx}{\textwidth}{@{}l*{6}{C}@{}}
\toprule
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c@{}}{\bfseries Science Faculty} \\ % note use of "c@{}"
\cmidrule(lr){2-3} \cmidrule(lr){4-5}  \cmidrule(l){6-7} % left- and right-trimming
& \multicolumn{1}{L}{F, S, H Courses} & 
\multicolumn{1}{L}{W Courses} & 
\multicolumn{1}{L}{F, S, H Courses} & 
\multicolumn{1}{L}{W Courses}& 
\multicolumn{1}{L}{F, S, H Courses} & 
\multicolumn{1}{L@{}}{W Courses} \\  % note use of "L@{}"
\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{tabularx}
\end{threeparttable}
\end{table}
\hrule  % just to indicate the width of the text block
\end{document}
Related Question