4

So my problem is this: some variables in my table have multiple categories, while others do not. However, I'd like to have more space between the separate variables, but not between the rows which have the different categories of a variable. As an example, I'd like there to be a bigger gap between "Students registered" and "Gender(%)", but a small gap between "Male and Female". Likewise, I'd like a gap (same as between "Students registered and Gender") between "Female" and "Race(%), but a smaller one (same as between Male & Female) between White/Black/Coloured/Indian/Asia. Then a bigger gap (same as between Students Registered/Gender & Gender/Race) between Indian/Asian with Financial Aid.

enter image description here

My MWE (don't worry about the column widths looking funny, I can change those myself!)

\documentclass{article}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\usepackage{verbatim}  % Needed for the "comment" environment to make LaTeX comments
\renewcommand\arraystretch{1.4}
\begin{document}
\begin{table}[H]
\renewcommand\arraystretch{1.4}
\begin{threeparttable}
\caption{Summary Statistics}
\label{table:ssna}
\begin{tabular}{p{0.30\textwidth} p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}}
\toprule
& \multicolumn{5}{c}{\bfseries Cohort} \\
\cmidrule(l){2-6}
& 2006 & 2007 & 2008 & 2009 & 2010 \\
\midrule
{\bfseries Students registered} & 1535 & 1584 & 1767 & 2037 & 1518 \\
{\bfseries Gender (\%)} & & & & & \\
Male & 61.1 & 64.5 & 57.7 & 55.5 & 55.3 \\
Female & 38.9 & 35.5 & 42.3 & 44.5 & 44.7 \\
{\bfseries Race (\%)} & & & & & \\
White & 43.3 & 43.4 & 40.6 & 36 & 39.8 \\
Black & 29.8 & 33.4 & 34.8 & 38 & 35.4 \\
Coloured & 16 & 13 & 13.8 & 12.9 & 11.5 \\
Indian/Asian & 10.8 & 10.1 & 10.8 & 13.1 & 13.4 \\
{\bfseries Financial Aid (\%)} & 19.5 & 22.3 & 25.2 & 29.9 & 24.5 \\
{\bfseries AD Programme (\%)} & 24 & 20.3 & 18.6 & 20.6 & 21.9 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item Source: UCT Institutional Planning Department
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document} 
2
  • 2
    maybe will help use \[1em] (or other ampunt of space) after row, where you like to have bigger space.
    – Zarko
    Commented May 20, 2014 at 7:32
  • 2
    Zarko meant to use e.g. \\[1em] instead of plain \\ command.
    – Malipivo
    Commented May 20, 2014 at 7:34

2 Answers 2

5

I think it's wrong to have \arraystretch set to 1.4 to begin with. The table is too spread out and adding vertical space between different groups will not help. Also the columns should be as wide as they need to be, not wider.

Without setting \arraystretch you can either use \addlinespace or add a \midrule:

\documentclass{article}
\usepackage{booktabs,siunitx}
\usepackage[flushleft]{threeparttable}

\begin{document}
\begin{table}

\begin{threeparttable}

\caption{Summary Statistics}
\label{table:ssna}

\begin{tabular}{
  l
  *{5}{S[table-format=2.1]}
}

\toprule
& \multicolumn{5}{c}{\bfseries Cohort} \\
\cmidrule(l){2-6}
& {2006} & {2007} & {2008} & {2009} & {2010} \\
\midrule
\bfseries Students registered & {1535} & {1584} & {1767} & {2037} & {1518} \\
  \addlinespace
\bfseries Gender (\%) \\
Male                         & 61.1 & 64.5 & 57.7 & 55.5 & 55.3 \\
Female                       & 38.9 & 35.5 & 42.3 & 44.5 & 44.7 \\
  \addlinespace
\bfseries Race (\%) \\
White                        & 43.3 & 43.4 & 40.6 & 36   & 39.8 \\
Black                        & 29.8 & 33.4 & 34.8 & 38   & 35.4 \\
Coloured                     & 16   & 13   & 13.8 & 12.9 & 11.5 \\
Indian/Asian                 & 10.8 & 10.1 & 10.8 & 13.1 & 13.4 \\
  \addlinespace
\bfseries Financial Aid (\%) & 19.5 & 22.3 & 25.2 & 29.9 & 24.5 \\
  \addlinespace
\bfseries AD Programme (\%)  & 24   & 20.3 & 18.6 & 20.6 & 21.9 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item Source: UCT Institutional Planning Department
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document} 

enter image description here

This is what you'd get with \midrule in place of \addlinespace:

enter image description here

If you want to have the table as wide as the text width, you can use tabular*:

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

\usepackage{lipsum} % just for the example

\begin{document}
\lipsum[2]

\begin{table}[htp]

\begin{threeparttable}

\caption{Summary Statistics}
\label{table:ssna}

\begin{tabular*}{\textwidth}{
  !{\extracolsep{\fill}}
  l
  *{5}{S[table-format=2.1]}
}

\toprule
& \multicolumn{5}{c}{\bfseries Cohort} \\
\cmidrule(l){2-6}
& {2006} & {2007} & {2008} & {2009} & {2010} \\
\midrule
\bfseries Students registered & {1535} & {1584} & {1767} & {2037} & {1518} \\
  \midrule
\bfseries Gender (\%) \\
Male                         & 61.1 & 64.5 & 57.7 & 55.5 & 55.3 \\
Female                       & 38.9 & 35.5 & 42.3 & 44.5 & 44.7 \\
  \midrule
\bfseries Race (\%) \\
White                        & 43.3 & 43.4 & 40.6 & 36   & 39.8 \\
Black                        & 29.8 & 33.4 & 34.8 & 38   & 35.4 \\
Coloured                     & 16   & 13   & 13.8 & 12.9 & 11.5 \\
Indian/Asian                 & 10.8 & 10.1 & 10.8 & 13.1 & 13.4 \\
  \midrule
\bfseries Financial Aid (\%) & 19.5 & 22.3 & 25.2 & 29.9 & 24.5 \\
  \midrule
\bfseries AD Programme (\%)  & 24   & 20.3 & 18.6 & 20.6 & 21.9 \\
\bottomrule
\end{tabular*}
\begin{tablenotes}
\small
\item Source: UCT Institutional Planning Department
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document} 

Note that \lipsum is used just to provide some context. I'm certain that this is worse than having the table at its “natural” width.

enter image description here

9
  • Hi @egreg - thanks alot. I'm relatively new to Latex (began using it in February!). I've always wondered why I had to spend endless hours to get the column widths correct so they could fit with the text. In order for columns to automatically adjust the longest text in the column, do I simply have to use the sinutix package? And which command tells Latex to set column widths automatically taking into account the length of text in a particular column? Thanks! I have other tables in my paper which go the full length of the page, but for no particular reason... Commented May 20, 2014 at 10:16
  • @Chris As I said, there's no need to have a table extend horizontally to cover the text width. Just make it as wide as it need to be. I'll add an example of a “widened” table so you can see what I'm saying.
    – egreg
    Commented May 20, 2014 at 10:19
  • Ah, I was interpreting the "Also the columns should be as wide as they need to be, not wider." as referring to the column width adjusting to the longest piece of text in that column. Hopefully the example will clear my confusion. Commented May 20, 2014 at 10:23
  • Wanted to test the code with other tables, but it is not working :(. My other two tables only have words [no decimals], so I removed the "*{5}{S[table-format=2.1]} }" and instead added 2 "ls" to the first "1" in your code: "lll }" As I have three columns in these tables. I thought the three "ls" would generate 3 left-aligned columns. This is the only part I changed. The code I'm using relates to the code of your latest table (the one which extends the table to the text width - still experimenting as to which one I like best). Commented May 20, 2014 at 16:57
  • 1
    @Chris *{5}{S[...]} means “five columns with the specified type”. If you want, say, three left aligned columns, just type \begin{tabular}{lll} or the shorthand \begin{tabular}{*{3}{l}}.
    – egreg
    Commented May 20, 2014 at 17:00
2

You can use makecell (and numprint to have column alignment on the decimal dot):

\documentclass{article}

\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\usepackage{verbatim} % Needed for the "comment" environment to make LaTeX comments
\renewcommand\arraystretch{1.4}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lc}
\renewcommand\theadgape{\Gape[10pt][0pt]}

\usepackage[autolanguage]{numprint}

\begin{document}
\begin{table}[H]
\renewcommand\arraystretch{1.4}
\renewcommand{\tabcolsep}{12pt}
\begin{threeparttable}
\caption{Summary Statistics}
\label{table:ssna}
\begin{tabular}{@{}p{0.30\textwidth}*{5}{n{2}{1}}@{}}
\toprule
& \multicolumn{5}{c}{\bfseries Cohort} \\
\cmidrule(l){2-6}
& {2006} & {2007} & {2008} &{ 2009}& {2010} \\
\midrule
\thead{Students registered} & {1535} & {1584} & {1767} & {2037} & {1518} \\
\thead{Gender (\%)} & & & & & \\
Male & 61.1 & 64.5 & 57.7 & 55.5 & 55.3 \\
Female & 38.9 & 35.5 & 42.3 & 44.5 & 44.7 \\
\thead{Race (\%)} & & & & & \\
White & 43.3 & 43.4 & 40.6 & 36 & 39.8 \\
Black & 29.8 & 33.4 & 34.8 & 38 & 35.4 \\
Coloured & 16& 13 & 13.8 & 12.9 & 11.5 \\
Indian/Asian & 10.8 & 10.1 & 10.8 & 13.1 & 13.4 \\
\thead{Financial Aid (\%)} & 19.5 & 22.3 & 25.2 & 29.9 & 24.5 \\
\thead{AD Programme (\%)} & 24. & 20.3 & 18.6 & 20.6 & 21.9 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item Source: UCT Institutional Planning Department
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document} 

enter image description here

1
  • Hi @Barnard, thanks for the reply! This looks nice. One question - could I extend the table to cover the entire page, but retain the column alignment on the decimal dot? Margins are: \setmarginsrb { 1.0in} % left margin { 0.6in} % top margin { 1.0in} % right margin { 0.8in} % bottom margin { 20pt} % head height {0.25in} % head sep { 9pt} % foot height { 0.3in} % foot sep Commented May 20, 2014 at 9:25

You must log in to answer this question.

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