9

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}
1
  • Is it a problem that the table width, as indicated by your calculations, is actually quite a bit wider than \textwidth?
    – Mico
    Mar 17, 2014 at 18:46

4 Answers 4

5

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}
4

Declare a new column type for the centered ones (if required, you can always override the definition for a particular cell using \multicolumn):

\documentclass[draft]{article}
\usepackage{booktabs, threeparttable}
\usepackage{array}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\newlength\mylen
\setlength\mylen{\dimexpr0.135\textwidth-2\tabcolsep\relax}

\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\setlength\tabcolsep{5pt}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabular}{
  @{}
  p{0.18\textwidth}
  *{2}{C{\mylen}}
  *{2}{L{\mylen}}
  *{2}{C{\mylen}}@{}
}
& \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}
& \multicolumn{1}{l}{F, S, H}  & \multicolumn{1}{l}{W}  & \multicolumn{1}{l}{F, S, H}  & \multicolumn{1}{l}{W} & \multicolumn{1}{l}{F, S, H} & \multicolumn{1}{l@{}}{W}  \\
& \multicolumn{1}{l}{Courses} &  \multicolumn{1}{l}{Courses}  &  \multicolumn{1}{l}{Courses}  &  \multicolumn{1}{l}{Courses}  &  \multicolumn{1}{l}{Courses}  &  \multicolumn{1}{l@{}}{Courses} \\
\midrule
First-year & 18 & 36 & \small Variable & \small Variable & 18 & 36  \\
Second-year & 18 & 36 & \small Variable & \small Variable & 24 & 48 \\
Third-year & 18 & 36 & \small Variable & \small Variable & 36 & 72 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}

\end{document}

enter image description here

To prevent the table from protruding into the right margin, I re-calculated to column width and reduced \tabcolsep.

If more sophisticated alignment is required for the numeric values, you could use the dcolumn or siunitx packages.

3
  • Observe that the OP's method for calculating the widths of columns 2 through 7 -- 0.15\textwidth-2\tabcolsep -- results in a table that's overall quite a bit wider than \textwidth.
    – Mico
    Mar 17, 2014 at 18:59
  • 1
    @Mico You're right. I've updated my answer. Thanks for noticing it. Mar 17, 2014 at 19:24
  • 1
    Here for the lr trimming (+1). All these days I was inserting dummy columns to get a similar effect. Thank you for that.
    – Ébe Isaac
    Sep 17, 2018 at 5:23
2

A solution using the makecell package, which lets you furher customise your cells. As a demonstration, I ask in the following code that numbers be printed in boldface, and that some vertyical space be added above and below the cell. In addition, every group of two columns, counting from the second, has the same width – that of the first group:

        \documentclass{article}            
        \centering\setlength{\tabcolsep}{4.4pt}
        \usepackage{booktabs, threeparttable}
        \usepackage{array}
        \newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
        \usepackage{makecell}
        \renewcommand{\cellgape}{\bfseries\Gape[2pt]}
        \renewcommand{\theadalign}{lc}
        \renewcommand{\theadfont}{\normalsize}
        \newlength{\headwd}
        \settowidth{\headwd}{\bfseries Commerce Faculty}

        \begin{document}

        \begin{table}[htbp]

        \begin{threeparttable}
        \caption{Number of Credits per Course by Year}
        \label{table:nc}
        \begin{tabular}{@{}l*{6}{p{\dimexpr 0.5\headwd-\tabcolsep \relax}}@{}}
        \addlinespace
        \toprule
        & \multicolumn{2}{c}{\bfseries Commerce Faculty} &
        \multicolumn{2}{c}{\bfseries EBE Faculty} &
        \multicolumn{2}{c}{\bfseries Science Faculty} \\
        \cmidrule(lr){2-3}\cmidrule(lr){4-5}  \cmidrule(lr){6-7}
        &\thead{ F, S, H \\ Courses} & \thead{W\\ Courses }&\thead{ F, S, H \\ Courses } & \thead{W\\ Courses} &\thead{ F, S, H\\ Courses} & \thead{W\\
          Courses}   \\
        \cmidrule(lr){2-3}\cmidrule(lr){4-5}  \cmidrule(lr){6-7}
        First-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{18} & \makecell{36}  \\
        Second-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{24} & \makecell{48} \\
        Third-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{36} & \makecell{72} \\
        \bottomrule
        \end{tabular}
        \end{threeparttable}
        \end{table}

        \end{document} 

enter image description here

7
  • The resulting table is considerably wider than the text block.
    – Mico
    Mar 17, 2014 at 18:42
  • @Mico: I'check that. Do you yhink it comes from makecel, or is it just that I let the parameters given by the O.P. as they were?
    – Bernard
    Mar 17, 2014 at 18:47
  • I think it's the OP's (slightly incorrect) calculations of the width of columns 2 through 7, viz., \dimexpr0.15\textwidth-2\tabcolsep\relax}, that are giving rise to the excessive width of the entire table.
    – Mico
    Mar 17, 2014 at 18:49
  • @Mico:Yes. I checked my version (with \makecell) is exactly the same width as the O.P.'s version. I'll modify my code to obtain something more reasonable.
    – Bernard
    Mar 17, 2014 at 18:55
  • @Mico: Now, every group of 2 columns (2-3, 4-5, 6-7, have the same width.
    – Bernard
    Mar 17, 2014 at 21:21
0

Why? I don't know, but I liked the tap idea. Probably far from OK, mixing Plain TeX and LaTeX, but still something to play with.

\documentclass{scrartcl}

\input{tap}
\usepackage{kantlipsum}

\begin{document}
\kant[1]

\medskip
\thistable{\desiredwidth\hsize}
%\moveright\parindent
\begintable
    \begintableformat & \left \endtableformat
    \B"              !                                        @6 \=                                         \E!
    \B":             ! @2 \textbf{Commerce Faculty} | @2 \textbf{EBE Faculty} | @2 \textbf{Science Faculty} \E!
    \B"              !                                        @6 \-                                         \E!
    \B"^             ! F, S, H       | W            | F, S, H    | W          | F, S, H      | W            \E!
    \B"_             ! Courses       | Courses      | Courses    | Courses    | Courses      | Courses      \E!
    \B!                                                @7 \=                                                \E!
    \B!^ First-year  |  \center{18}  | \center{36}  | Variable   | Variable   | \center{18}  | \center{36}  \E!
    \B!+ Second-year |  \center{18}  | \center{36}  | Variable   | Variable   | \center{24}  | \center{48}  \E!
    \B!_ Third-year  |  \center{18}  | \center{36}  | Variable   | Variable   | \center{36}  | \center{72}  \E!
    \=
\endtable
\medskip

\kant[4]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .