层次表

层次表

有人能帮我创建这个表吗?我知道它需要 \multicolumns,我也看过其他帖子,但我仍然无法像这里一样设置列。很抱歉我对 latex 的了解只是基础。谢谢。在此处输入图片描述

这是我尝试过的:

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

答案1

也许这个代码可以帮助你开始:

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

您需要 8 列才能正确缩进。啊,我改进了代码,而不是\hrulefill更好地使用\cmidrule(如另一个答案中所建议的,虽然已删除,但很好)。

好像:

标签图片


编辑:

我更改了代码中的一行:

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

到:

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

我很惊讶它竟然能起作用 (@David:数组很棒!)!不过,现在您的n = ...意愿已经适应了这一行。

答案2

您的表格的更多“高级”替代代码:

  • threeparttable对于表格注释(如表格图片所示)
  • siunitx对于S列类型,可以实现更好的数字格式

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

在此处输入图片描述

相关内容