我正在构建一个结果表,其中我想让列标题由一行(第一行和最后两行)组成,并与由两行组成的第二行标题对齐。
我知道我没有表达清楚所以我将举一个我想要获得的例子:
我希望“Outcome”、“Bandiwdth”和“N”位于线的正上方,因为它们与多行列标题的第二行对齐。
我的代码如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{adjustbox}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[margin=0.5in]{geometry}
\begin{document}
\begin{table}[h]
\caption{RD Treatment Effects on Schooling}
\adjustbox{max width=\columnwidth}{
\begin{tabular}{lccccccc} \hline\hline \\[-1em]
& (1) & (2) & (3) & (4) & (5) & & \\[0.1em] \hline \\[-1em]
Outcome & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Quadratic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Cubic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $\hat{h}/2$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $2\hat{h}$ bandwidth} & Bandwidth & N \\ \hline
& & & & & & \\
Years of schooling & 0.340* & 0.305 & 0.320 & 0.238 & -0.00418 & 26 & 3,716\\
& (0.183) & (0.289) & (0.306) & (0.229) & (0.126) \\[1.2ex]
Completed primary school & 0.0193* & 0.0321** & 0.00435 & 0.406* & -0.0605 & 43 & 6,023 \\
& (0.0115) & (0.0142) & (0.0206) & (0.232) & (0.117) \\[1.2ex]
Completed secondary school & 0.00864 & 0.00882 & -0.0219 & 0.410* & 0.0229 & 25 & 3,453\\
& (0.0263) & (0.0355) & (0.0589) & (0.215) & (0.133) \\[1.2ex]
Completed higher education & -0.00269 & -0.0324* & -0.0408* & 0.336* & 0.0833 & 44 & 6,373 \\
& (0.0116) & (0.0178) & (0.0240) & (0.190) & (0.108) \\[1.2ex]
Literacy & 0.0273 & 0.0417 & 0.0238 & 0.253 & -0.0613 & 36 & 5,149 \\
& (0.0245) & (0.0369) & (0.0534) & (0.225) & (0.122) \\[1.2ex]
\hline \hline \\[-1em]
\multicolumn{6}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}}
\end{table}
\end{document}
答案1
我强烈建议您不要使用\adjustbox
来使表格材料“适合”文本块:这样做很可能会在表格中生成非常小且几乎难以辨认的字符。换句话说:不要使用,\adjustbox
除非您想向读者表明您并不真正关心他们是否会尝试仔细阅读表格的内容。
相反,我建议你学习如何利用环境tabularx
。在下面的代码中,我将tabularx
环境与包的功能相结合dcolumn
,以便数字数据在各自的小数点标记上对齐。并且,使用宏的线条绘制宏booktabs
来生成间距适当的水平线。
就我个人而言,我不会将“结果”、“带宽”和“N”这几个字向下移动一行。但是,如果您觉得这样做确实有必要,只需\renewcommand{\tabularxcolumn}[1]{b{#1}}
在加载tabularx
包后插入指令即可。
以下屏幕截图显示了包含建议修改的表格,后面是用于\adjustbox
将材料塞入文本块宽度的原始表格代码。我希望你会同意上面的表格更具可读性。:-)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,tabularx,dcolumn,adjustbox}
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of 'X' col. type
\newcolumntype{d}[1]{D..{#1}}
\usepackage[font=small,labelfont=bf,skip=.333\baselineskip]{caption}
\usepackage[margin=0.5in]{geometry} % those are awfully narrow margins...
% two handy shortcut macros:
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
\newcommand\mC[1]{\multicolumn{1}{C}{#1}}
\begin{document}
\begin{table}[t]
\caption{\texttt{tabularx} table, \texttt{dcolumn} and \texttt{booktabs} packages}
\begin{tabularx}{\textwidth}{@{} l *{5}{d{2.5}} cc @{}}
\toprule
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)} \\
\addlinespace
% use the 'C' column type on the five headers to allow automatic line-breakiing
Outcome & \mC{Linear RD $\hat{h}$~b'width}
& \mC{Quadratic~RD $\hat{h}$~b'width}
& \mC{Cubic~RD $\hat{h}$~b'width}
& \mC{Linear~RD $\hat{h}/2$~b'width}
& \mC{Linear~RD $2\hat{h}$~b'width}
& Bandwidth & $N$ \\
\midrule
Years of schooling & 0.340^{*} & 0.305 & 0.320 & 0.238 & -0.00418 & 26 & 3,716\\
& (0.183) & (0.289) & (0.306) & (0.229) & (0.126) \\ \addlinespace
Completed primary school & 0.0193^{*} & 0.0321^{**} & 0.00435 & 0.406^{*} & -0.0605 & 43 & 6,023 \\
& (0.0115) & (0.0142) & (0.0206) & (0.232) & (0.117) \\ \addlinespace
Completed secondary school & 0.00864 & 0.00882 & -0.0219 & 0.410^{*} & 0.0229 & 25 & 3,453\\
& (0.0263) & (0.0355) & (0.0589) & (0.215) & (0.133) \\ \addlinespace
Completed higher education & -0.00269 & -0.0324^{*} & -0.0408^{*} & 0.336^{*} & 0.0833 & 44 & 6,373 \\
& (0.0116) & (0.0178) & (0.0240) & (0.190) & (0.108) \\ \addlinespace
Literacy & 0.0273 & 0.0417 & 0.0238 & 0.253 & -0.0613 & 36 & 5,149 \\
& (0.0245) & (0.0369) & (0.0534) & (0.225) & (0.122) \\
\bottomrule
\addlinespace
\multicolumn{6}{c}{ $^{***}\ p<0.01$; $^{**}\ p<0.05$; $^{*}\ p<0.1$} \\
\end{tabularx}
\end{table}
\begin{table}[h]
\caption{For comparison, the original form of the table}
\adjustbox{max width=\columnwidth}{
\begin{tabular}{l*{7}{c}} \hline\hline \\[-1em]
& (1) & (2) & (3) & (4) & (5) & & \\[0.1em] \hline \\[-1em]
Outcome & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Quadratic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Cubic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $\hat{h}/2$ bandwidth} & \multicolumn{1}{p{3cm}}{\centering Linear RD \\ $2\hat{h}$ bandwidth} & Bandwidth & N \\ \hline
& & & & & & \\
Years of schooling & 0.340* & 0.305 & 0.320 & 0.238 & -0.00418 & 26 & 3,716\\
& (0.183) & (0.289) & (0.306) & (0.229) & (0.126) \\[1.2ex]
Completed primary school & 0.0193* & 0.0321** & 0.00435 & 0.406* & -0.0605 & 43 & 6,023 \\
& (0.0115) & (0.0142) & (0.0206) & (0.232) & (0.117) \\[1.2ex]
Completed secondary school & 0.00864 & 0.00882 & -0.0219 & 0.410* & 0.0229 & 25 & 3,453\\
& (0.0263) & (0.0355) & (0.0589) & (0.215) & (0.133) \\[1.2ex]
Completed higher education & -0.00269 & -0.0324* & -0.0408* & 0.336* & 0.0833 & 44 & 6,373 \\
& (0.0116) & (0.0178) & (0.0240) & (0.190) & (0.108) \\[1.2ex]
Literacy & 0.0273 & 0.0417 & 0.0238 & 0.253 & -0.0613 & 36 & 5,149 \\
& (0.0245) & (0.0369) & (0.0534) & (0.225) & (0.122) \\[1.2ex]
\hline \hline \\[-1em]
\multicolumn{6}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}}
\end{table}
\end{document}
答案2
\thead
您可以使用from 命令轻松获得所需内容。我使用 from和 的列类型makecell
对您的表格进行了一些改进。我删除了通常不应与表格一起使用的命令:S
siunits
booktabs
\adjustbox
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{siunitx, makecell, booktabs}
\renewcommand{\theadfont}{\small}
\renewcommand{\theadalign}{cb}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[margin=0.5in, showframe]{geometry}
\begin{document}
\begin{table}[!hb]
\sisetup{table-format =-1.5, table-number-alignment =center, table-space-text-pre=(, table-space-text-post =)**, table-align-text-pre=false, table-align-text-post=false}
\setlength\tabcolsep{4pt}
\caption{RD Treatment Effects on Schooling}
\small\centering
\begin{tabular}{l*{5}{S}cc}
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & & \\
\addlinespace
\toprule
Outcome & {\thead{Linear RD \\ $\hat{h}$ bandwidth}} & {\thead{Quadratic RD \\ $\hat{h}$ bandwidth}} & {\thead{Cubic RD \\ $\hat{h}$ bandwidth}} & {\thead{Linear RD \\ $\hat{h}/2$ bandwidth}} & {\thead{Linear RD \\ $2\hat{h}$ bandwidth}} & Bandwidth & N \\
\midrule
\addlinespace[2ex]
Years of schooling & 0.340* & 0.305 & 0.320 & 0.238 & -0.00418 & 26 & 3,716 \\
& {(}0.183{)} & {(}0.289{)} & {(}0.306{)} & {(}0.229{)} & {(}0.126{)} \\
\addlinespace
Completed primary school & 0.0193* & 0.0321** & 0.00435 & 0.406* & -0.0605 & 43 & 6,023 \\
& {(}0.0115{)} & {(}0.0142{)} & {(}0.0206{)} & {(}0.232{)} & {(}0.117{)} \\
\addlinespace
Completed secondary school & 0.00864 & 0.00882 & -0.0219 & 0.410* & 0.0229 & 25 & 3,453 \\
&{(}0.0263{)} & {(}0.0355{)} & {(}0.0589{)} & {(}0.215{)} & {(}0.133{)} \\
\addlinespace
Completed higher education & -0.00269 & -0.0324* & -0.0408* & 0.336* & 0.0833 & 44 & 6,373 \\
& {(}0.0116{)} & {(}0.0178{)} & {(}0.0240{)} & {(}0.190{)} & {(}0.108{)} \\
\addlinespace
Literacy & 0.0273 & 0.0417 & 0.0238 & 0.253 & -0.0613 & 36 & 5,149 \\
& {(}0.0245{)} & {(}0.0369{)} & {(}0.0534{)} & {(}0.225{)} & {(}0.122{)} \\
\addlinespace
\bottomrule
\addlinespace
\multicolumn{6}{l}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
\end{table}
\end{document}
答案3
您可以使用array
包和b
列类型(有用的链接)。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{adjustbox}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[margin=0.5in]{geometry}
\usepackage{array}
\begin{document}
\begin{table}[h]
\caption{RD Treatment Effects on Schooling}
\adjustbox{max width=\columnwidth}{
\begin{tabular}{lccccccc} \hline\hline \\[-1em]
& (1) & (2) & (3) & (4) & (5) & & \\[0.1em] \hline \\[-1em]
Outcome & \multicolumn{1}{b{3cm}}{\centering Linear RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{b{3cm}}{\centering Quadratic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{b{3cm}}{\centering Cubic RD \\ $\hat{h}$ bandwidth} & \multicolumn{1}{b{3cm}}{\centering Linear RD \\ $\hat{h}/2$ bandwidth} & \multicolumn{1}{b{3cm}}{\centering Linear RD \\ $2\hat{h}$ bandwidth} & Bandwidth & N \\ \hline
& & & & & & \\
Years of schooling & 0.340* & 0.305 & 0.320 & 0.238 & -0.00418 & 26 & 3,716\\
& (0.183) & (0.289) & (0.306) & (0.229) & (0.126) \\[1.2ex]
Completed primary school & 0.0193* & 0.0321** & 0.00435 & 0.406* & -0.0605 & 43 & 6,023 \\
& (0.0115) & (0.0142) & (0.0206) & (0.232) & (0.117) \\[1.2ex]
Completed secondary school & 0.00864 & 0.00882 & -0.0219 & 0.410* & 0.0229 & 25 & 3,453\\
& (0.0263) & (0.0355) & (0.0589) & (0.215) & (0.133) \\[1.2ex]
Completed higher education & -0.00269 & -0.0324* & -0.0408* & 0.336* & 0.0833 & 44 & 6,373 \\
& (0.0116) & (0.0178) & (0.0240) & (0.190) & (0.108) \\[1.2ex]
Literacy & 0.0273 & 0.0417 & 0.0238 & 0.253 & -0.0613 & 36 & 5,149 \\
& (0.0245) & (0.0369) & (0.0534) & (0.225) & (0.122) \\[1.2ex]
\hline \hline \\[-1em]
\multicolumn{6}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}}
\end{table}
\end{document}