2

Please I'm trying to create a multicolumn table but I'm not getting anywhere. I want two columns, female and male, with informal, formal and total under as the picture shows. This is my code, but the output is not what I want. Thanks.

\documentclass[a4paper,12pt]{book}
\usepackage{amsmath}
\usepackage{bm}

\pagestyle{plain}
\usepackage{setspace}
\raggedbottom

\usepackage{mathtools}
\usepackage{longtable}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array, booktabs, ragged2e}
\usepackage{threeparttablex}


{\renewcommand*{\arraystretch}{1.5}
\begin{spacing}{.85}

\begin{ThreePartTable}
\begin{TableNotes}
\footnotesize
\item [a] Data source: SLLFS 2014
\item [b] Percentage values in the table are percentages of the number of women (xxxx) and men (xxxx) in the sample
\end{TableNotes}


\begin{longtable}{p{1ex}>{\RaggedRight}p{1.95cm}*{3}{p{1.45cm}p{2cm}}}
\caption{Characteristics of study sample by job type and gender\tnote{a}}\label{chap1:tab1}\\\toprule


\endfirsthead
\multicolumn{7}{p{4cm}} {Table 1 (\emph{continued})}\\ \toprule 
&           & \multicolumn{2}{l}{Female} & \multicolumn{2}{l}{Male} & \\
\cmidrule{3-4} \cmidrule(lr){5-6} \cmidrule{7-8}
&            & Informal & Formal & Total & Informal & Formal & Total \\\midrule
\endhead
\hline\multicolumn{7}{r}{\tablename\ \thetable\ -- \textit{Continued on next page}} \\
\endfoot
\hline
\insertTableNotes  % tell LaTeX where to insert the contents of "TableNotes"
\endlastfoot 

&           & \multicolumn{2}{l}{Female} & \multicolumn{2}{l}{Male} &\\
\cmidrule{3-4} \cmidrule(lr){5-6} \cmidrule{7-8}

&            & Informal & Formal & Total & Informal & Formal & Total \\\midrule
\multicolumn{2}{l}{\textit{Age}} & \\

& 15-24 & 663 & 20.994 & 19 & 5.791 & 682 & 20.019\\
& 25-34 & 1071 & 32.091 & 114 & 37.959 & 1185 & 32.383\\
& 35-44 & 866 & 23.826 & 86 & 28.394 & 952 & 24.053\\
& 45-54 & 530 & 15.416 & 58 & 18.217 & 588 & 15.555\\
& 55-64 & 251 & 7.721 & 29 & 9.636 & 280 & 7.816\\
\bottomrule 

\end{longtable}
\end{ThreePartTable}    
\end{spacing}
}

enter image description here

Bernard
  • 271,350
user151731
  • 89
  • 1
  • 6
  • You've just used \multicolumn{2} rather than \multicolumn{3} for the 'Female' and 'Male' headings. Just change those and then similarly for the rules - you're just one off in each case. – cfr May 24 '18 at 00:27
  • But that code must give you errors: you're using at least 8 columns, but you've only defined 6! If you want help with the errors, include them in your post. Otherwise, please correct them. – cfr May 24 '18 at 00:30
  • when i use \multicolumn{3}, i get the error "extra alignment tab has been changed to \cr....}{l}{female} & multicolumn{3}{l}{male} &". I went back to 2 and didn't get any errors, but the result is not what i want. – user151731 May 24 '18 at 00:55
  • Aren't the Formal-Informal Male columns swapped? You have decimal numbers in the Informal column, whereas decimal numbers are to be found in the Female Formal column. – Bernard May 24 '18 at 01:11
  • Don't bother about the numbers you see there, they are meaningless. I was using another table as a template to create this one. I want to get the structure of the table right before I put in the correct data. – user151731 May 24 '18 at 01:26

3 Answers3

2

mostly off-topic:

  • table content is not consistent and clear. does it contain decimal numbers or is . group separator?
  • which column contains percents?
  • code for column headers are not consistent
  • the package mathtools also load amsmath, therefore load it again is not neccessary
  • it is sufficient load each package only once
  • use of \renewcommand*{\arraystretch}{1.5} and than \begin{spacing}{.85} is unclear. isn't it sufficient to use just \renewcommand*{\arraystretch}{1.2}?
  • for last six columns i suggest to use S column type provided by package siunitx. it enable better aligning of numbers (at decimal point)

enter image description here

(red lines indicate text borders)

\documentclass[a4paper,12pt]{book}
\usepackage{mathtools}      % not used in this mwe
\usepackage{bm}             % not used in this mwe
\usepackage{setspace}       % not used in this mwe
\raggedbottom
\usepackage{siunitx}        % new
\usepackage{ragged2e}
\usepackage{array, booktabs, longtable,
            multirow, threeparttablex}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\pagestyle{plain}
    \begin{ThreePartTable}
\begin{TableNotes}\footnotesize
\item [a] Data source: SLLFS 2014
\item [b] Percentage values in the table are percentages of the number of women (xxxx) and men (xxxx) in the sample
\end{TableNotes}
    \renewcommand*{\arraystretch}{1.2}
    \setlength{\LTcapwidth}{\linewidth} % that caption width is equal table width
    \setlength\LTleft{0pt}
    \setlength\LTright{0pt}
    \setlength\tabcolsep{5pt}
\begin{longtable}{@{\extracolsep{\fill}}
                    p{1ex}
     >{\RaggedRight}p{3cm}
                    S[table-format=4.0]
                    S[table-format=2.3]
                    S[table-format=3.0]
                    S[table-format=2.3]
                    S[table-format=4.0]
                    S[table-format=2.3]
                  } \caption{Characteristics of study sample by job type and gender\tnote{a}}
\label{chap1:tab1}\\
    \toprule
    &   &   \multicolumn{3}{c}{Female}      &   \multicolumn{3}{c}{Male}        \\
    \cmidrule(r){3-5}
    \cmidrule(l){6-8}
    &   & {Informal} & {Formal} & {Total}   & {Informal} & {Formal} & {Total}   \\
    \midrule
\endfirsthead
\caption{Characteristics of study sample by job type and gender (\textit{continued})}\\
    \toprule
    &   &   \multicolumn{3}{c}{Female}      &   \multicolumn{3}{c}{Male}        \\
    \cmidrule(r){3-5}
    \cmidrule(l){6-8}
    &   & {Informal} & {Formal} & {Total}   & {Informal} & {Formal} & {Total}   \\
    \midrule
\endhead
    \midrule
\multicolumn{8}{r}{\textit{Continued on the next page}} \\
\endfoot
    \bottomrule
\insertTableNotes  % tell LaTeX where to insert the contents of "TableNotes"
\endlastfoot
\multicolumn{2}{l}{\textit{Age}}
                &       &        &      &        &      &           \\
    & 15-24     & 663   & 20.994 &  19  &  5.791 &  682 & 20.019    \\
    & 25-34     & 1071  & 32.091 & 114  & 37.959 & 1185 & 32.383    \\
    & 35-44     & 866   & 23.826 &  86  & 28.394 &  952 & 24.053    \\
    & 45-54     & 530   & 15.416 &  58  & 18.217 &  588 & 15.555    \\
    & 55-64     & 251   &  7.721 &  29  &  9.636 &  280 &  7.816    \\
\multicolumn{2}{l}{\textit{Education}}
                &       &        &      &        &      &           \\
    & Primary or less
                & 2789  & 88.027 &  51  & 17.534 & 2840 & 84.439    \\
\end{longtable}
    \end{ThreePartTable}
\end{document}
Zarko
  • 296,517
0

I changed \multicolumn{2}{l}{Female} to \multicolumn{3}{l}{Female} (same for {Male}) and removed a trailing &. Also changed cmidrules to the appropriate widths. In this case, \cmidrule(lr){3-5} and \cmidrule(l){6-8} do the trick.

enter image description here

\documentclass[a4paper,12pt]{book}
\usepackage{amsmath}
\usepackage{bm}
\pagestyle{plain}
\usepackage{setspace}
\raggedbottom
\usepackage{mathtools}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array, booktabs, ragged2e}
\usepackage{threeparttablex}

\begin{document}

{\renewcommand*{\arraystretch}{1.5}
\begin{spacing}{.85}

\begin{ThreePartTable}
\begin{TableNotes}
\footnotesize
\item [a] Data source: SLLFS 2014
\item [b] Percentage values in the table are percentages of the number of women (xxxx) and men (xxxx) in the sample
\end{TableNotes}

\begin{longtable}{p{1ex}>{\RaggedRight}p{1.95cm}*{3}{p{1.45cm}p{2cm}}}
\caption{Characteristics of study sample by job type and gender\tnote{a}}\label{chap1:tab1}\\\toprule

\endfirsthead
\multicolumn{7}{p{4cm}} {Table 1 (\emph{continued})}\\ 
\toprule 
& & \multicolumn{3}{l}{Female} & \multicolumn{3}{l}{Male}\\
\cmidrule(lr){3-5} \cmidrule(l){6-8}
& & Informal & Formal & Total & Informal & Formal & Total\\
\midrule
\endhead
\hline\multicolumn{7}{r}{\tablename\ \thetable\ -- \textit{Continued on next page}} \\
\endfoot
\hline
\insertTableNotes  % tell LaTeX where to insert the contents of "TableNotes"
\endlastfoot 
& & \multicolumn{3}{l}{Female} & \multicolumn{3}{l}{Male}\\
\cmidrule(lr){3-5} \cmidrule(l){6-8}
& & Informal & Formal & Total & Informal & Formal & Total\\
\midrule
\multicolumn{2}{l}{\textit{Age}} &\\
& 15-24 & 663 & 20.994 & 19 & 5.791 & 682 & 20.019\\
& 25-34 & 1071 & 32.091 & 114 & 37.959 & 1185 & 32.383\\
& 35-44 & 866 & 23.826 & 86 & 28.394 & 952 & 24.053\\
& 45-54 & 530 & 15.416 & 58 & 18.217 & 588 & 15.555\\
& 55-64 & 251 & 7.721 & 29 & 9.636 & 280 & 7.816\\
\bottomrule 

\end{longtable}
\end{ThreePartTable}    
\end{spacing}
}

\end{document}
Milo
  • 9,440
0

I propose this, using siunitx to align numbers on the decimal dot. Also I don't see the point of using the spacing environment for a table, as the table-specific command \arraystretch is available:

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{bm}

\pagestyle{plain}
\raggedbottom

\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array, siunitx, ragged2e}%
\usepackage{threeparttablex}

\begin{document}

{\renewcommand*{\arraystretch}{1.25}
\sisetup{table-format=2.3, table-number-alignment=center}
\newcolumntype{N}{S[table-format=4.0]}
\setlength{\LTcapwidth}{\linewidth}
\begin{ThreePartTable}
\begin{TableNotes}
\footnotesize
\item [a] Data source: SLLFS 2014
\item [b] Percentage values in the table are percentages of the number of women (xxxx) and men (xxxx) in the sample
\end{TableNotes}
\begin{longtable}{@{}p{1em}@{}c@{\hskip2pc}NSN@{\hskip3pc}SNS@{}}
\caption{Characteristics of study sample by job type and gender\tnote{a}}\label{chap1:tab1}\\
\toprule
& & \multicolumn{2}{c}{Female} & & \multicolumn{2}{c}{Male} \\
\cmidrule(r){3-4} \cmidrule(lr){6-7}
& & Informal & Formal & Total & Informal & Formal & Total \\
\midrule
\multicolumn{2}{@{}l}{\textit{Age}} & \\
\endfirsthead
\multicolumn{8}{l} {Table 1 (\emph{continued})}\\
\toprule
& & \multicolumn{2}{c}{Female} & & \multicolumn{2}{c}{Male} \\
\cmidrule{3-4} \cmidrule(lr){6-7}
& & Informal & Formal & Total & Informal & Formal & Total \\
\midrule
\multicolumn{2}{@{}l}{\textit{Age}} & \\
\endhead
\hline\multicolumn{8}{r}{\textit{Continued on next page}} \\
\endfoot
\bottomrule
\insertTableNotes % tell LaTeX where to insert the contents of "TableNotes"
\endlastfoot%
 & 15-24 & 663 & 20.994 & 19 & 5.791 & 682 & 20.019\\
& 25-34 & 1071 & 32.091 & 114 & 37.959 & 1185 & 32.383\\
& 35-44 & 866 & 23.826 & 86 & 28.394 & 952 & 24.053\\
& 45-54 & 530 & 15.416 & 58 & 18.217 & 588 & 15.555\\
& 55-64 & 251 & 7.721 & 29 & 9.636 & 280 & 7.816
\end{longtable}
\end{ThreePartTable}

\end{document} 

enter image description here

Bernard
  • 271,350