2

Can someone please help me create this table? I know it requires \multicolumns and I've looked at other threads, but I'm still unable to set up the columns as they are here. I'm sorry my knowledge of latex is only basic. Thanks.enter image description here

Here's what I've tried:

\documentclass[a4paper,12pt]{book}
\usepackage{amsmath}
\usepackage{apacite}
\usepackage{graphicx}
\usepackage[usenames, dvipsnames]{color}
\usepackage{bm}
\pagestyle{plain}

\usepackage{setspace}
\onehalfspacing
\raggedbottom

\usepackage{mathtools}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{table*}
    \begin{tabular}{p{1.2cm}p{1.2cm}p{1.3cm}p{1.2cm}p{1.2cm}p{1.2cm}p{1.2cm}}
        \hline
        &\multicolumn{1}{l}{Variables}&&\multicolumn{6}{l}{Type of employment}\\
        \cline{2-8}
        &\multicolumn{2}{c}{Informal}&&\multicolumn{2}{c}{Formal}&&\multicolumn{2}{c}{Total}\\
        \cline{2-4}\cline{6-8}
        &\textit{n}=6036&\%&&\textit{n}=876&\%&&\textit{n}=6912&\%&100\%\\
        \hline
        Age 15-24&&663&20.944&&19&5.791&&682&20.019\\

        \hline
    \end{tabular}   
\end{table*}

\end{document}
Keks Dose
  • 30,892
user151731
  • 89
  • 1
  • 6
  • 3
    welcome to tex.se! please show us what you try so far (in form of small but complete document beginning with \documentclass{...} and ending with \end{document}. re typing your table from scratch is not fun ... – Zarko May 17 '18 at 04:48
  • Hello, thanks for your reply. I will appreciate if you can just set up the table structure above with the column headings 'variables', 'Type of contract', 'Informal" etc. You need not enter the data. This is what I've tried: – user151731 May 17 '18 at 10:42
  • \begin{table*} \begin{tabular}{p{1.2cm}p{1.2cm}p{1.3cm}p{1.2cm}p{1.2cm}p{1.2}p{1.2}} \hline &\multicolumn{1}{l}{Variables}&&\multicolumn{6}{l}{Type of employment}\ \cline{2-8} &\multicolumn{2}{c}{Informal}&&\multicolumn{2}{c}{Formal}&&\multicolumn{2}{c}{Total}\ \cline{2-4}\cline{6-8} &\textit{n}=6036&%&&\textit{n}=876&%&&\textit{n}=6912&%&100%\ \hline Age 15-24&&663&20.944&&19&5.791&&682&20.019\

    \hline \end{tabular} \end{table*}

    – user151731 May 17 '18 at 10:46
  • To mark up code properly (as you see from my edit), select the code and hit Ctrl + K, or click the button marked {}. – Torbjørn T. May 17 '18 at 10:54
  • Hello Torbjørn, thanks for your comment. Was trying to figure that out. Please see my initial post for the code. Thanks. – user151731 May 17 '18 at 11:00

2 Answers2

3

Maybe this code may help you to start:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{array, booktabs, ragged2e}

\begin{document}

\begin{tabular}{p{1ex}>{\RaggedRight}p{4cm}*{3}{p{1.8cm}p{1cm}}}\toprule
\multicolumn{2}{l}{Variables} & \multicolumn{2}{l}{Type of job contract} & \multicolumn{4}{c}{} \\\cmidrule{3-8}
 &           & \multicolumn{2}{l}{Informal} & \multicolumn{2}{l}{Formal}  & \multicolumn{2}{l}{Total}\\
  \cmidrule{3-4} \cmidrule(lr){5-6} \cmidrule{7-8}

 &            & n = 1234 & \% & n = 1234 & \% & n = 1234 & \% \\\midrule
\multicolumn{2}{l}{Female} & \\

 & Age under 22 years old & 101 & 13.1 & 52 & 8.7 & 153 & 11.2\\


\end{tabular}
\end{document}

You need 8 columns to get the indentation right. Ah, and I improved the code, instead of \hrulefill better use \cmidrule (as suggested in the other answer, which was deleted, but good).

Looks like:

tab pic


EDIT:

I changed one line in the code:

\begin{tabular}{p{1ex}>{\RaggedRight}p{4cm}*{6}{p{1.5cm}}}\toprule

to:

\begin{tabular}{p{1ex}>{\RaggedRight}p{4cm}*{3}{p{1.8cm}p{1cm}}}\toprule

I'm very surprised that this works (@David: array is great!)! However, now your n = ... will fit into the line.

Keks Dose
  • 30,892
  • Thanks a lot. But the problem now is that I'm using \documentclass[a4paper,12pt]{book} and get errors when I include the packages you have used here in the preamble of my document. – user151731 May 17 '18 at 12:11
  • @DK1 And what is the error? – Keks Dose May 17 '18 at 12:12
  • Sorry I mixed things up. Putting the three packages you used here in my preamble does not result in any errors. But the n=1234 %....n=1234 % column headings do not neatly fit in one line as it is here. I'm not sure how to upload the picture into this comment. – user151731 May 17 '18 at 12:51
  • Trying to give an idea of what the issue is without a picture. The 1234 moves to the next line. So what I have is n = % and 1234 below it. – user151731 May 17 '18 at 13:07
  • @DK1I made the n=... broader and the %thiner. – Keks Dose May 17 '18 at 13:13
  • An alternative was to delete the spaces in n = 1234. But your adjustment is much better. – user151731 May 17 '18 at 13:17
2

more "advanced" alternative code for your table:

  • threeparttable for `table notes (as shown on your table image)
  • siunitx for S column type, which enable better number formating

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array, booktabs, threeparttable}
\usepackage{siunitx}

\begin{document}
    \begin{threeparttable}
\caption{Characteristic \dots}
\label{tab:characteristic}
\begin{tabular}{@{}
        p{1em}l*{3}{S[table-format=3]S[table-format=2.1]}
                @{}}
    \toprule
\multicolumn{2}{@{} l}{Variables}
    & \multicolumn{6}{l}{Type of job contract}          \\
    \cmidrule{3-8}
    &   & \multicolumn{2}{l}{Informal}
            & \multicolumn{2}{l}{Formal}
                & \multicolumn{2}{l}{Total}\\
    \cmidrule{3-4}
    \cmidrule(l){5-6}
    \cmidrule(l){7-8}
    &   & {$n = 1517$}    & {\%}    & {$n = 1390$}    & {\%}    & {$n = 2907$}  & {\%}  \\
    \midrule
\multicolumn{8}{@{} l}{Female}                                                  \\
    & Age under 22 years old\tnote{a}
        & 101           & 13.1  & 52            & 8.7       & 153 & 11.2    \\
    \bottomrule
\end{tabular}
\begin{tablenotes}[flushleft]\footnotesize
\item[a]    bla bla bla
\item[b]
\end{tablenotes}
\end{threeparttable}
\end{document}

enter image description here

Zarko
  • 296,517