我正在尝试创建如下所示的层次表:
我正在使用的代码如下:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{array, booktabs, ragged2e}
\title{table}
\author{}
\date{}
\begin{document}
\maketitle
\section{Introduction}
\begin{tabular}{p{1ex}>{\RaggedRight}p{4cm}*{3}{p{1.8cm}p{1cm}}}\toprule
\multicolumn{2}{c}{Variables} & \multicolumn{2}{c}{Level of precipitation} &
\multicolumn{4}{c}{} \\\cmidrule{3-8}
& & \multicolumn{2}{c}{Monthly} & \multicolumn{2}{c}{Yearly} \\
\cmidrule{3-4} \cmidrule(lr){5-6}
& & City-A & City-B & City-A & City-B & & \\\midrule
%\multicolumn{2}{c}{Female} & \\
& 1 & - & - & - & - & - & - \\ \midrule
& 2 & - & - & - & - & - & - \\ \midrule
\end{tabular}
\end{document}
有人可以帮忙修复此代码以获得所需的输出吗?
答案1
由于你正在使用booktabs
,下面旨在使用该格式来复制表格。
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{ *{5}{c} }
\toprule
& \multicolumn{4}{c}{\bfseries Level of precipitation} \\
\cmidrule(lr){2-5}
& \multicolumn{2}{c}{\itshape Monthly} & \multicolumn{2}{c}{\itshape Yearly} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
\bfseries Variables & City-A & City-B & City-A & City-B \\
\midrule
X & - & - & - & - \\
Y & - & - & - & - \\
\bottomrule
\end{tabular}
\end{document}