我正在尝试制作一个由以下代码概述的表格,但我无法使用多列命令。我需要分别在男性、女性和汇总的第 2、3 和 4 列上使用标题“份额”。以及分别在男性、女性和汇总的第 5、6、7 列上使用标题“平均小时工资”。代码附在下面。
任何帮助是极大的赞赏!
\begin{document}
\documentclass[10.5pt,a4]{article}
\usepackage[margin=0.5in,a4paper]{geometry}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{float}
\usepackage{titlesec}
\usepackage{capt-of}
%dashed line
\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
%Widows & Orphans & Penalties
\widowpenalty500
\clubpenalty500
\clubpenalty=9996
\exhyphenpenalty=50 %for line-breaking at an explicit hyphen
\brokenpenalty=4991
\predisplaypenalty=10000
\postdisplaypenalty=1549
\displaywidowpenalty=1602
\floatingpenalty = 20000
\begin{table*}[] \centering
%\ra{1.3}
\begin{small}
\begin{tabular}{@{}lrrrrrrr@{}}\toprule
\textbf{Dependent Variables} & & \textbf{Share(\%)} & & \textbf{Avg hourly wage} & & & \textbf{Avg differential} \\ \hline
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} & \textbf{ Men } &
\textbf{Women} & \textbf{Pooled} \\ \hdashline
\\
\textbf{PANEL A: 2011-2012}\ \\ \midrule
\\
Panel A.1: Sector (Urban or Rural)\ \\ \hdashline
Rural & 51.5 & 54 & 52 & 28.54 & 18.83 & 26.92 & 51.5 \\ \hdashline
Urban & 48.4 & 46 & 48 & 45.19 & 41.86 & 45 & 7.8 \\ \hdashline
\\
Panel A.2: Sector (Public or Private) \\ \hdashline
Public & 18.6 & 27.7 & 20 & 75.55 & 55.44 & 71.1 & 36.2 \\ \hdashline
Private & 81.4 & 72.3 & 80 & 27.68 & 19.67 & 26.52 & 40.7 \\ \hdashline
\\
\\
%(further columns omitted)
\end{tabular}
\end{small}
\caption{Summary statistics 2}
\end{table*}
\end{document}
答案1
像这样?
变化:
- 引入了三个新包:(
malecell
用于两行标题)、multirow
用于多行单元格和siunitx
(用于列类型S
) - 列组的标题位于
\multicolumn{3}{l}{...}
- 用于中间头
\multicolumn{8}{l}{...}
- 在带有数字的列中,列类型更改为
S
\documentclass[11pt]{article}
\usepackage[margin=0.5in,a4paper]{geometry}
\usepackage{caption}
% \usepackage{float}
% \usepackage{titlesec}
% \usepackage{capt-of}
%dashed line
\usepackage{array, booktabs, makecell, multirow}
\renewcommand\theadfont{\bfseries\small}
\newcommand\mceight[1]{\multicolumn{8}{l}{#1}}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
\usepackage{siunitx}
\begin{document}
\begin{table}[ht] \centering
\small
\begin{tabular}{ l*{7}{S[table-format=2.2]} }
\toprule
\multirow[b]{2}{*}{\thead{Dependent\\ Variables}
& \multicolumn{3}{c}{\thead[b]{Share(\%)}}
& \multicolumn{3}{c}{\thead[b]{Avg hourly wage}}
& {\multirow[b]{2}{*}{\thead{Avg\\ differential}}} \\
\cmidrule(lr){2-4}\cmidrule(lr){5-7}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} \\
\hdashline \\
\mceight{\textbf{PANEL A: 2011-2012}} \\
\midrule
\\
\mceight{Panel A.1: Sector (Urban or Rural)} \\ \hdashline
Rural & 51.5 & 54 & 52 & 28.54 & 18.83 & 26.92 & 51.5 \\ \hdashline
Urban & 48.4 & 46 & 48 & 45.19 & 41.86 & 45 & 7.8 \\ \hdashline \\
\mceight{Panel A.2: Sector (Public or Private)} \\ \hdashline
Public & 18.6 & 27.7 & 20 & 75.55 & 55.44 & 71.1 & 36.2 \\ \hdashline
Private & 81.4 & 72.3 & 80 & 27.68 & 19.67 & 26.52 & 40.7 \\ \hdashline \\
\mceight{Panel A.3: Education level} \\ \hdashline
Illilerate & 11.7 & 27.7 & 14.3 & 21.11 & 13.92 & 18.87 & 51.6 \\ \hdashline
primary & 14.5 & 14.1 & 14.4 & 23.02 & 14.36 & 21.6 & 60.3 \\
\hdashline
secondary & 46.1 & 26.08 & 42.9 & 30.13 & 20.29 & 29.17 & 48.49 \\
\hdashline
higher secondary & 12.12 & 11.1 & 11.9 & 43.13 & 39.6 & 42.6 & 8.9 \\
\hdashline
graduate & 9.9 & 11.21 & 10.1 & 67.03 & 58.84 & 65.58 & 13.91 \\
\hdashline
post graduate & 5.45 & 9.7 & 6.1 & 89.82 & 74.42 & 85.51 & 20.69 \\
\bottomrule
\end{tabular}
\end{small}
\caption{Summary statistics 2}
\end{table}
\end{document}
笔记:
- 包中
caption
包含capt-of
功能,因此无需加载 - 重新考虑,如果所有虚线都是必要的(我会删除大部分)
附录:
如果您希望表格分布在更多页面上,则longtable
可以使用以下解决方案:
\documentclass{article}
\usepackage[margin=1in,a4paper]{geometry}
\usepackage{caption}
\usepackage{array, booktabs, longtable, makecell, multirow}
\renewcommand\theadfont{\bfseries\small}
\newcommand\mceight[1]{\multicolumn{8}{l}{#1}}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
\usepackage{siunitx}
\usepackage{lipsum}% for dummy text
\begin{document}
\lipsum[1-5]% text filler that table is two pages
\begin{longtable}{ l*{7}{S[table-format=2.2]} }
\caption{Summary statistics 2}
\label{tab:...} \\
\toprule
\multirow[b]{2}{*}{\thead{Dependent\\ Variables}}
& \multicolumn{3}{c}{\thead[b]{Share(\%)}}
& \multicolumn{3}{c}{\thead[b]{Avg hourly wage}}
& {\multirow[b]{2}{*}{\thead{Avg\\ differential}}} \\
\cmidrule(lr){2-4}\cmidrule(lr){5-7}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} \\
\hdashline \\
\endfirsthead
%%%%
\caption{Summary statistics 2 (cont.)} \\
\toprule
\multirow[b]{2}{*}{\thead{Dependent\\ Variables}}
& \multicolumn{3}{c}{\thead[b]{Share(\%)}}
& \multicolumn{3}{c}{\thead[b]{Avg hourly wage}}
& {\multirow[b]{2}{*}{\thead{Avg\\ differential}}} \\
\cmidrule(lr){2-4}\cmidrule(lr){5-7}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} \\
\midrule \\
\endhead
\midrule
\multicolumn{8}{r}{\footnotesize\textit{Continue on the next page}}
\endfoot
\endlastfoot
%%%%
\mceight{\textbf{PANEL A: 2011-2012}} \\
\midrule
\\
\mceight{Panel A.1: Sector (Urban or Rural)} \\ \hdashline
Rural & 51.5 & 54 & 52 & 28.54 & 18.83 & 26.92 & 51.5 \\
Urban & 48.4 & 46 & 48 & 45.19 & 41.86 & 45 & 7.8 \\ \\ \mceight{Panel A.2: Sector (Public or Private)} \\ \hdashline
Public & 18.6 & 27.7 & 20 & 75.55 & 55.44 & 71.1 & 36.2 \\ Private & 81.4 & 72.3 & 80 & 27.68 & 19.67 & 26.52 & 40.7 \\ \\ \mceight{Panel A.3: Education level} \\ \hdashline
Illilerate & 11.7 & 27.7 & 14.3 & 21.11 & 13.92 & 18.87 & 51.6 \\ primary & 14.5 & 14.1 & 14.4 & 23.02 & 14.36 & 21.6 & 60.3 \\
secondary & 46.1 & 26.08 & 42.9 & 30.13 & 20.29 & 29.17 & 48.49 \\
higher secondary & 12.12 & 11.1 & 11.9 & 43.13 & 39.6 & 42.6 & 8.9 \\
graduate & 9.9 & 11.21 & 10.1 & 67.03 & 58.84 & 65.58 & 13.91 \\
post graduate & 5.45 & 9.7 & 6.1 & 89.82 & 74.42 & 85.51 & 20.69 \\
\bottomrule
\end{longtable}
\end{document}
编辑:
对于longtable
现在的解决方案,我从中窃取了一些想法(关于第一列和最后一列标题)米科答案......并\midrule
添加\endfoot
。
答案2
这是一个解决方案,(a)使用longtable
环境而不是嵌套的table
/tabular
环境,(b)将数字列中的数字与小数点对齐,(c)为第一列提供更多结构。
请注意,我也放弃了所有\hdashline
指令:它们根本不需要传达内容表格的重点。虚线只会造成视觉混乱。消除混乱。您的读者会感激您的。:-)
\documentclass[10pt,a4paper]{article}
\usepackage[margin=2.5cm,a4paper]{geometry}
\usepackage{booktabs}
\usepackage[font=bf]{caption}
\usepackage{siunitx,ragged2e,longtable}
% Macro for typesetting multi-line header cells in bold
\newcommand{\boldblock}[1]{%
\smash{%
\begin{tabular}[t]{@{}>{\bfseries\arraybackslash}c@{}}
#1
\end{tabular}}}
% Set up a custom column type for the first column:
\newlength\mylength
\settowidth\mylength{Panel A.2: Sector (Public}
\newcolumntype{L}{@{}>{%
\RaggedRight\hangindent=1em\hangafter=1}p{\mylength}}
\begin{document}
\begin{longtable}{@{} L
*{2}{S[table-format=2.1]}
S[table-format=2.0]
*{3}{S[table-format=2.2]}
S[table-format=2.1] @{}}
%% Headers and footers
\caption{Summary statistics 2} \label{tab:sumstats}\\
\toprule
\textbf{Dependent Variables} &
\multicolumn{3}{c}{\textbf{Share (\%)}} &
\multicolumn{3}{c}{\textbf{Avg hourly wage}} &
\boldblock{Avg \\differential} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} \\
\midrule
\addlinespace
\endfirsthead
\multicolumn{8}{@{}l}{Table \ref{tab:sumstats}, cont'd}
\\ \addlinespace
\toprule
\textbf{Dependent Variables} &
\multicolumn{3}{c}{\textbf{Share (\%)}} &
\multicolumn{3}{c}{\textbf{Avg hourly wage}} &
\boldblock{Avg\\differential} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled}
& \textbf{Men} & \textbf{Women} & \textbf{Pooled} \\
\midrule
\addlinespace
\endhead
\midrule
\multicolumn{8}{r@{}}{(cont'd on following page)}\\
\endfoot
\bottomrule
\endlastfoot
%% Body of longtable
\textbf{PANEL A: 2011--2012} \\
\addlinespace
Panel A.1: Sector (Urban or Rural) \\
Rural & 51.5 & 54 & 52 & 28.54 & 18.83 & 26.92 & 51.5 \\
Urban & 48.4 & 46 & 48 & 45.19 & 41.86 & 45 & 7.8 \\
\addlinespace
Panel A.2: Sector (Public or Private) \\
Public & 18.6 & 27.7 & 20 & 75.55 & 55.44 & 71.1 & 36.2 \\
Private & 81.4 & 72.3 & 80 & 27.68 & 19.67 & 26.52 & 40.7 \\
\addlinespace
Panel A.3: Education level \\
Illiterate &&&&&&& 51.6\\
\dots\\
\dots\\
%% remaining rows omitted
\end{longtable}
\end{document}