1

So this is the first time I am attempting to use the longtable package and for some reason, I can't even get MWE to run - Pdflatex gets stuck after loading the booktabs package. I honestly can't see why it won't even run, as it is not giving errors. The image below is the Excel file of my "table" (ignore the "Exluded" - minor typo)

enter image description here

I'd like the Latex table to have the following features:

  • Must fit the page width (personal preference)
  • Decimal alignment
  • Want the Headers on first page to be headers on the second page (I think I did that correctly in the code). Not sure though.

Despite my Latex compiler not working, I have attempted in my "MWE" to be as comprehensive as possible:

\documentclass{article}
\usepackage{longtable, booktabs, siuntix}
\begin{document}
\begin{longtable}{|c|c|c|c|c|c|}
\caption{Descriptive Statistics: Whole Sample ($N = \num{13 298}$)\label{table:DSWS}}
\toprule
{\bfseries Variable} & {\bfseries Proportion of Individuals in Sample} & {\bfseries Graduated (\%)} & {\bfseries Academically Excluded (\%)} & {\bfseries Transferred (\%)} & {\bfseries Censored} \\
\midrule
\endfirsthead
\midrule
{\bfseries Variable} & {\bfseries Proportion of Individuals in Sample} & {\bfseries Graduated (\%)} & {\bfseries Academically Excluded (\%)} & {\bfseries Transferred (\%)} & {\bfseries Censored} \\
\midrule
\endhead
Total & 100.00 & 40.98 & 13.39 & 6.02 & 39.61 \\
\addlinespace
\bfseries Gender (\%) \\
Male                 & 57.78 & 39.98 & 15.68 & 5.38 & 38.96 \\
Female               & 42.22 & 42.33 & 10.24 & 6.91 & 40.52 \\
\addlinespace
\bfseries Race (\%) \\
White & 39.34 & 52.48 & 4.15 & 5.91 & 37.46 \\                        
Black & 35.46 & 28.97 & 23.77 & 6.57 & 40.69 \\                        
Coloured & 13.21 & 39.56 & 14.34 & 6.72 & 39.39 \\                     
Indian/Asian & 11.98 & 40.30 & 11.93 & 4.02 & 43.75 \\                
\addlinespace
\bfseries Financial Aid (\%) \\
Ineligible for Financial Aid & 77.86 & 43.83 & 9.59 & 5.18 & 41.4 \\
Eligible for Financial Aid & 22.14 & 30.94 & 26.73 & 9 & 33.32 \\
\addlinespace
\bfseries Programme (\%) \\
Mainstream & 77.68 & 45.7 & 9.71 & 6.16 & 38.43 \\
Academic Development & 22.32 & 24.53 & 26.18 & 5.56 & 43.73 \\  
\addlinespace
\bfseries English Home Language (\%) \\
Yes & 65.21 & 47.22 & 7.74 & 5.75 & 39.29 \\
No & 34.79 & 29.27 & 23.97 & 6.53 & 40.23 \\
\addlinespace
\bfseries School Quintile (\%) \\
1 & 1.45 & 23.83 & 38.86 & 7.77 & 29.53 \\
2 & 2.8 & 25.74 & 32.44 & 5.36 & 36.46 \\
3 & 8.44 & 24.58 & 31.7 & 7.48 & 36.24 \\
4 & 5.29 & 26.28 & 28.98 & 6.11 & 38.64 \\
5 & 44.62 & 44.03 & 10.8 & 5.65 & 39.52 \\
Independent  & 37.38 & 44.92 & 7.7 & 6.12 & 41.26 \\
\addlinespace
\bfseries Province (\%) \\
Western Cape & 41.45 & 45.95 & 10.49 & 6.17 & 37.39 \\
Non- Western Cape & 58.55 & 37.45 & 15.44 & 5.92 & 41.19 \\
\addlinespace
\bfseries Year of First Registration
{2006} & 11.54 & 69.32 & 16.48 & 8.21 & 5.99 \\
{2007} & 11.91 & 70.39 & 17.05 & 6.5 & 6.06 \\
{2008} & 13.29 & 66.67 & 17.77 & 7.41 & 8.15 \\
{2009} & 15.32 & 54.05 & 20.86 & 10.7 & 14.38 \\
{2010} & 11.42 & 51.78 & 11.46 & 5.8 & 30.96 & \\
{2011} & 10.87 & 14.18 & 11.96 & 4.22 & 69.64 \\
{2012} & 13.11 & 0 & 6.94 & 3.56 & 89.51 \\
{2013} & 12.54 & 0 & 3 & 0.72 & 96.28 \\
\bottomrule
\end{longtable}
\end{document}
  • 3
    I'd start by spelling siunitx correctly, and then removing the last & in fourth last row. Edit: And add a \\ after the \caption. – Torbjørn T. Jun 6 '14 at 19:21
  • 1
    You say it is not giving errors, surely the code as posted gives you the error ! LaTeX Error: File `siuntix.sty' not found. followed by lots more if you don't stop at that point? – David Carlisle Jun 6 '14 at 20:12
  • Thanks @TorbjørnT. Would this be to get rid of the 'multiply defined labels' warning I am getting? Because I have tried: \caption \\ {Descriptive Statistics (\%): Whole Sample ($N = \num{13 298}$)} \label{table:DSWSS}\\ and \caption {Descriptive Statistics (\%): Whole Sample ($N = \num{13 298}$)} \\ \label{table:DSWSS}\\ and I still have those warnings (I'm using cfr's example) below. – The Statistician Magician Jun 7 '14 at 7:10
3

This cleans up the errors mentioned in the comments and makes some further adjustments. It doesn't do everything you should do but you need to make some further choices to fit the table to the page properly. (I don't know if you are using standard margins which you might reduce to make a little more room or perhaps you wish to use \small for the table. Etc.)

What I've done:

  • eliminated vertical rules (see booktabs documentation - don't use booktabs for the table if you need vertical rules)
  • switched \bfseries to \sffamily
  • used S for the columns with figures (see siunitx documentation)
  • used p for the remaining columns with \raggedright for the first
  • eliminated unnecessary repetition in the code
  • introduced \endfoot to handle \bottomrule pagewise
  • taken \% out of the table into the caption
  • maybe a couple more things I've forgotten

Remaining:

  • you need to eliminate the multiply defined labels caused by the use of \label in the caption
  • you need to pay attention to the remaining warnings, especially the 5 remaining overfull hboxes.

The code:

\documentclass{article}
\usepackage{array, longtable, booktabs, siunitx}
\newcommand*{\mysection}{\sffamily}
\begin{document}

\noindent\begin{longtable}{>{\raggedright\arraybackslash}p{.17\textwidth}*{5}{S}}% don't use vertical rules with booktabs (see the documentation)
    \caption{Descriptive Statistics (\%): Whole Sample ($N = \num{13 298}$)\label{table:DSWS}}\\
    \toprule
    % define \endfirsthead if first head differs from later heads
    {\mysection\hskip0pt Variable} & \multicolumn{1}{p{.16\textwidth}}{\sffamily\hskip0pt Proportion of Individuals in Sample} & \multicolumn{1}{p{.15\textwidth}}{\sffamily\hskip0pt Graduated} & \multicolumn{1}{p{.16\textwidth}}{\sffamily\hskip0pt Academically Excluded} & \multicolumn{1}{p{.15\textwidth}}{\sffamily\hskip0pt Transferred} & \multicolumn{1}{p{.12\textwidth}}{\sffamily\hskip0pt Censored}\\
    \midrule
    \endhead
    \bottomrule
    \endfoot
    \mysection Total & 100.00 & 40.98 & 13.39 & 6.02 & 39.61\\
    \addlinespace
    \mysection Gender\\
    Male                 & 57.78 & 39.98 & 15.68 & 5.38 & 38.96\\
    Female               & 42.22 & 42.33 & 10.24 & 6.91 & 40.52\\
    \addlinespace
    \mysection Race\\
    White & 39.34 & 52.48 & 4.15 & 5.91 & 37.46\\
    Black & 35.46 & 28.97 & 23.77 & 6.57 & 40.69\\
    Coloured & 13.21 & 39.56 & 14.34 & 6.72 & 39.39\\
    Indian/Asian & 11.98 & 40.30 & 11.93 & 4.02 & 43.75\\
    \addlinespace
    \mysection Financial Aid\\
    Ineligible for Financial Aid & 77.86 & 43.83 & 9.59 & 5.18 & 41.4\\
    Eligible for Financial Aid & 22.14 & 30.94 & 26.73 & 9 & 33.32\\
    \addlinespace
    \mysection Programme\\
    Mainstream & 77.68 & 45.7 & 9.71 & 6.16 & 38.43\\
    Academic Development & 22.32 & 24.53 & 26.18 & 5.56 & 43.73\\
    \addlinespace
    \mysection English Home Language\\
    Yes & 65.21 & 47.22 & 7.74 & 5.75 & 39.29\\
    No & 34.79 & 29.27 & 23.97 & 6.53 & 40.23\\
    \addlinespace
    \mysection School Quintile\\
    1 & 1.45 & 23.83 & 38.86 & 7.77 & 29.53\\
    2 & 2.8 & 25.74 & 32.44 & 5.36 & 36.46\\
    3 & 8.44 & 24.58 & 31.7 & 7.48 & 36.24\\
    4 & 5.29 & 26.28 & 28.98 & 6.11 & 38.64\\
    5 & 44.62 & 44.03 & 10.8 & 5.65 & 39.52\\
    Independent  & 37.38 & 44.92 & 7.7 & 6.12 & 41.26\\
    \addlinespace
    \mysection Province\\
    Western Cape & 41.45 & 45.95 & 10.49 & 6.17 & 37.39\\
    Non-Western Cape & 58.55 & 37.45 & 15.44 & 5.92 & 41.19\\
    \addlinespace
    \mysection Year of First Registration\\
    {2006} & 11.54 & 69.32 & 16.48 & 8.21 & 5.99\\
    {2007} & 11.91 & 70.39 & 17.05 & 6.5 & 6.06\\
    {2008} & 13.29 & 66.67 & 17.77 & 7.41 & 8.15\\
    {2009} & 15.32 & 54.05 & 20.86 & 10.7 & 14.38\\
    {2010} & 11.42 & 51.78 & 11.46 & 5.8 & 30.96\\
    {2011} & 10.87 & 14.18 & 11.96 & 4.22 & 69.64\\
    {2012} & 13.11 & 0 & 6.94 & 3.56 & 89.51\\
    {2013} & 12.54 & 0 & 3 & 0.72 & 96.28\\
\end{longtable}

\end{document}

The output:

Tabular on page

2

If you stick to (partial) vertical rules, here is a solution, using makecell to have multiline column headers.I made some sort of section headers inside the table, that interrupt the vertical lines:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{mathtools}

\usepackage[nomarginpar]{geometry}
\usepackage{longtable}
\usepackage{ltablex}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\cellalign{lc}
\setcellgapes{3pt}
\makegapedcells

\usepackage{ragged2e}
\usepackage[mode=text, detect-family]{siunitx}
\usepackage[x11names, tables]{xcolor}
\usepackage{colortbl}

\newcommand\hhead[1]{\multicolumn{5}{!{\bluethickvrule}l}{\Gape[8pt][3pt]{\color{CadetBlue3}\bfseries#1}}}
\newcommand*\thickvrule[1][1pt]{\vrule width #1}
\newcommand*\bluethickvrule[1][1pt]{{\color{CadetBlue3}\vrule width #1}}
\newcommand\bluethickhline{\arrayrulecolor{CadetBlue3}\hline\hline\noalign{\vskip-0.2pt}\hline\arrayrulecolor{black}}

\begin{document}

\sffamily
\begin{tabularx}{\linewidth}{!{\bluethickvrule} X|*{4}{S|}S!{\bluethickvrule}}
\caption{Descriptive Statistics: Whole Sample ($\mathsf{N = \num{13 298}}$)\label{table:DSWS}}\\
\bluethickhline
{\textbf{Variable}} &{\thead{Proportion of \\Individuals\\ in Sample}} & { \thead{Graduated \\(\%)}} &{\thead{Academically\\ Excluded \\(\%)}} & {\thead{Transferred \\(\%)}} & {\thead{Censored}}
\\
\hline
\endfirsthead
\multicolumn{6}{r}{\em Table \thetable\enspace (continued)}\\
\bluethickhline
{\textbf{Variable}} &{\thead{Proportion \\ of Individuals\\ in Simple}} & { \thead{Graduated \\(\%)}} &{\thead{Academically\\ Excluded \\(\%)}} & {\thead{Transferred \\(\%)}} & {\thead{Censored}}
\\
\hline
\endhead
\bluethickhline
\endfoot
\bluethickhline
\endlastfoot
Total & 100.00 & 40.98 & 13.39 & 6.02 & 39.61 \\
\hhead{Gender (\%)} & \\
Male & 57.78 & 39.98 & 15.68 & 5.38 & 38.96 \\
Female & 42.22 & 42.33 & 10.24 & 6.91 & 40.52 \\
\hhead{Race (\%)} & \\
White & 39.34 & 52.48 & 4.15 & 5.91 & 37.46 \\
Black & 35.46 & 28.97 & 23.77 & 6.57 & 40.69 \\
Coloured & 13.21 & 39.56 & 14.34 & 6.72 & 39.39 \\
Indian/Asian & 11.98 & 40.30 & 11.93 & 4.02 & 43.75 \\
\hhead{Financial Aid (\%)} & \\
\makecell{Ineligible for \\ Financial Aid} & 77.86 & 43.83 & 9.59 & 5.18 & 41.4 \\
\makecell{Eligible for \\ Financial Aid }& 22.14 & 30.94 & 26.73 & 9 & 33.32 \\
\hhead{Programme (\%)} & \\
Mainstream & 77.68 & 45.7 & 9.71 & 6.16 & 38.43 \\
\makecell{Academic\\Development} & 22.32 & 24.53 & 26.18 & 5.56 & 43.73 \\
\hhead{English Home Language (\%)} & \\
Yes & 65.21 & 47.22 & 7.74 & 5.75 & 39.29 \\
No & 34.79 & 29.27 & 23.97 & 6.53 & 40.23 \\
\hhead{School Quintile (\%)} & \\
\raggedleft 1 & 1.45 & 23.83 & 38.86 & 7.77 & 29.53 \\
\raggedleft 2 & 2.8 & 25.74 & 32.44 & 5.36 & 36.46 \\
\raggedleft 3 & 8.44 & 24.58 & 31.7 & 7.48 & 36.24 \\
\raggedleft 4 & 5.29 & 26.28 & 28.98 & 6.11 & 38.64 \\
\raggedleft 5 & 44.62 & 44.03 & 10.8 & 5.65 & 39.52 \\
Independent & 37.38 & 44.92 & 7.7 & 6.12 & 41.26 \\
\hhead{Province (\%)} & \\
Western Cape & 41.45 & 45.95 & 10.49 & 6.17 & 37.39 \\
\makecell{Non-Western\\Cape} & 58.55 & 37.45 & 15.44 & 5.92 & 41.19 \\
\hhead{Year of First Registration} & \\
{2006} & 11.54 & 69.32 & 16.48 & 8.21 & 5.99 \\
{2007} & 11.91 & 70.39 & 17.05 & 6.5 & 6.06 \\
{2008} & 13.29 & 66.67 & 17.77 & 7.41 & 8.15 \\
{2009} & 15.32 & 54.05 & 20.86 & 10.7 & 14.38 \\
{2010} & 11.42 & 51.78 & 11.46 & 5.8 & 30.96 \\
{2011} & 10.87 & 14.18 & 11.96 & 4.22 & 69.64 \\
{2012} & 13.11 & 0 & 6.94 & 3.56 & 89.51 \\
{2013} & 12.54 & 0 & 3 & 0.72 & 96.28 \\
\end{tabularx}

\end{document} ine headers. I managed to have "headers" inside the table while keeping alignment of numbers on the decimal dot:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{mathtools}

\usepackage[nomarginpar]{geometry}
\usepackage{longtable }
\usepackage{ltablex}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\cellalign{lc}
\setcellgapes{3pt}
\makegapedcells

\usepackage{ragged2e}
\usepackage[mode=text, detect-family]{siunitx}
\usepackage[x11names, tables]{xcolor}
\usepackage{colortbl}

\newcommand\hhead[1]{\multicolumn{5}{!{\bluethickvrule}l}{\Gape[8pt][3pt]{\color{CadetBlue3}\bfseries#1}}}
\newcommand*\thickvrule[1][1pt]{\vrule width #1}
\newcommand*\bluethickvrule[1][1pt]{{\color{CadetBlue3}\vrule width #1}}
\newcommand\bluethickhline{\arrayrulecolor{CadetBlue3}\hline\hline\noalign{\vskip-0.2pt}\hline\arrayrulecolor{black}}

\begin{document}

\sffamily
\begin{tabularx}{\linewidth}{!{\bluethickvrule} X|*{4}{S|}S!{\bluethickvrule}}
\caption{Descriptive Statistics: Whole Sample ($\mathsf{N = \num{13 298}}$)\label{table:DSWS}}\\
\bluethickhline
{\textbf{Variable}} &{\thead{Proportion of \\Individuals\\ in Sample}} & { \thead{Graduated \\(\%)}} &{\thead{Academically\\ Excluded \\(\%)}} & {\thead{Transferred \\(\%)}} & {\thead{Censored}}
\\
\hline
\endfirsthead
\multicolumn{6}{r}{\em Table \thetable\enspace (continued)}\\
\bluethickhline
{\textbf{Variable}} &{\thead{Proportion \\ of Individuals\\ in Simple}} & { \thead{Graduated \\(\%)}} &{\thead{Academically\\ Excluded \\(\%)}} & {\thead{Transferred \\(\%)}} & {\thead{Censored}}
\\
\hline
\endhead
\bluethickhline
\endfoot
\bluethickhline
\endlastfoot
Total & 100.00 & 40.98 & 13.39 & 6.02 & 39.61 \\
\hhead{Gender (\%)} & \\
Male & 57.78 & 39.98 & 15.68 & 5.38 & 38.96 \\
Female & 42.22 & 42.33 & 10.24 & 6.91 & 40.52 \\
\hhead{Race (\%)} & \\
White & 39.34 & 52.48 & 4.15 & 5.91 & 37.46 \\
Black & 35.46 & 28.97 & 23.77 & 6.57 & 40.69 \\
Coloured & 13.21 & 39.56 & 14.34 & 6.72 & 39.39 \\
Indian/Asian & 11.98 & 40.30 & 11.93 & 4.02 & 43.75 \\
\hhead{Financial Aid (\%)} & \\
\makecell{Ineligible for \\ Financial Aid} & 77.86 & 43.83 & 9.59 & 5.18 & 41.4 \\
\makecell{Eligible for \\ Financial Aid }& 22.14 & 30.94 & 26.73 & 9 & 33.32 \\
\hhead{Programme (\%)} & \\
Mainstream & 77.68 & 45.7 & 9.71 & 6.16 & 38.43 \\
\makecell{Academic\\Development} & 22.32 & 24.53 & 26.18 & 5.56 & 43.73 \\
\hhead{English Home Language (\%)} & \\
Yes & 65.21 & 47.22 & 7.74 & 5.75 & 39.29 \\
No & 34.79 & 29.27 & 23.97 & 6.53 & 40.23 \\
\hhead{School Quintile (\%)} & \\
\raggedleft 1 & 1.45 & 23.83 & 38.86 & 7.77 & 29.53 \\
\raggedleft 2 & 2.8 & 25.74 & 32.44 & 5.36 & 36.46 \\
\raggedleft 3 & 8.44 & 24.58 & 31.7 & 7.48 & 36.24 \\
\raggedleft 4 & 5.29 & 26.28 & 28.98 & 6.11 & 38.64 \\
\raggedleft 5 & 44.62 & 44.03 & 10.8 & 5.65 & 39.52 \\
Independent & 37.38 & 44.92 & 7.7 & 6.12 & 41.26 \\
\hhead{Province (\%)} & \\
Western Cape & 41.45 & 45.95 & 10.49 & 6.17 & 37.39 \\
\makecell{Non-Western\\Cape} & 58.55 & 37.45 & 15.44 & 5.92 & 41.19 \\
\hhead{Year of First Registration} & \\
{2006} & 11.54 & 69.32 & 16.48 & 8.21 & 5.99 \\
{2007} & 11.91 & 70.39 & 17.05 & 6.5 & 6.06 \\
{2008} & 13.29 & 66.67 & 17.77 & 7.41 & 8.15 \\
{2009} & 15.32 & 54.05 & 20.86 & 10.7 & 14.38 \\
{2010} & 11.42 & 51.78 & 11.46 & 5.8 & 30.96 \\
{2011} & 10.87 & 14.18 & 11.96 & 4.22 & 69.64 \\
{2012} & 13.11 & 0 & 6.94 & 3.56 & 89.51 \\
{2013} & 12.54 & 0 & 3 & 0.72 & 96.28 \\
\end{tabularx}

\end{document} 

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.