我有这张来自 Stata 的表格:
编辑:采纳 Mico 的建议后,我能够使用以下代码创建表格:
\begin{table}[htbp]
\caption{Summary Statistics} \label{tab:1}
\centering
\setlength\tabcolsep{4.5pt} % default value: 6pt
\begin{tabular}{@{} l l|
d{2.7} d{1.7} d{2.7} d{2.7} |
>{$}r<{$} @{${}={}$} d{4.0} @{}} %
\multicolumn{2}{@{}l|}{Variable\Bstrut} & \mc{Mean} & \mc{Std.~Dev.} & \mc{Min} & \multicolumn{1}{c|}{Max} & \multicolumn{2}{r@{}}{Observ.}\\
\hline
\textit{lwage}\Tstrut & overall & 1.649147 & .5326094 & -3.579079 & 4.05186 & N & 4360\\
& between & & .3907468 & .3333435 & 3.174173 & n & 545\\
& within & & .3622636 & -2.467201 & 3.204687 & T & 8\\[1ex] % extra vertical whitespace
\textit{educ} & overall & 11.76697 & 1.746181 & 3 & 16 & N & 4360\\
& between & & 1.747585 & 3 & 16 & n & 545\\
& within & & 0 & 11.76697 & 11.76697 & T & 8\\[1ex] % extra vertical whitespace
\textit{black} & overall & .1155963 & .3197769 & 0 & 1 & N & 4360\\
& between & & .320034 & 0 & 1 & n & 545\\
& within & & 0 & .1155963 & .1155963 & T & 8\\[1ex] % extra vertical whitespace
\textit{hisp} & overall & .1559633 & .3628622 & 0 & 1 & N & 4360\\
& between & & .3631539 & 0 & 1 & n & 545\\
& within & & 0 & .1559633 & .1559633 & T & 8\\[1ex] % extra vertical whitespace
\textit{exper} & overall & 6.514679 & 2.825873 & 0 & 18 & N & 4360\\
& between & & 1.654918 & 3.5 & 14.5 & n & 545\\
& within & & 2.291551 & 3.014679 & 10.01468 & T & 8\\[1ex] % extra vertical whitespace
$exper^{2}$ & overall & 50.42477 & 40.78199 & 0 & 324 & N & 4360\\
& between & & 26.35134 & 17.5 & 215.5 & n & 545\\
& within & & 31.1431 & -44.07523 & 158.9248 & T & 8\\[1ex] % extra vertical whitespace
\textit{married} & overall & .4389908 & .4963208 & 0 & 1 & N & 4360\\
& between & & .3766116 & 0 & 1 & n & 545\\
& within & & .3236137 & -.4360092 & 1.313991 & T & 8\\[1ex] % extra vertical whitespace
\textit{union} & overall & .2440367 & .4295639 & 0 & 1 & N & 4360\\
& between & & .3294467 & 0 & 1 & n & 545\\
& within & & .2759787 & -.6309633 & 1.119037 & T & 8\\[1ex] % extra vertical whitespace
\end{tabular}
\end{table}
但是现在的问题是它们没有像原始表格那样正确对齐。我该怎么办?
答案1
我建议您使用该dcolumn
包将小数标记上的数字数据对齐。
\documentclass{article}
\usepackage{dcolumn,caption}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
%% define a few struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.9ex}} % "top" strut
\newcommand\Bstrut{\rule[-1.2ex]{0pt}{0pt}} % "bottom" strut
\begin{document}
\begin{table}[htbp]
\caption{Summary Statistics} \label{tab:summ_stats}
\centering
\setlength\tabcolsep{4.5pt} % default value: 6pt
\begin{tabular}{@{} l l|
d{2.7} d{1.7} d{2.7} d{2.7} |
>{$}r<{$} @{${}={}$} d{4.0} @{}} %
\multicolumn{2}{@{}l|}{Variable\Bstrut} & \mc{Mean} & \mc{Std.~Dev.} & \mc{Min} & \multicolumn{1}{c|}{Max} & \multicolumn{2}{r@{}}{Observ.}\\
\hline
lwage\Tstrut & overall & 1.649147 & .5326094 & -3.579079 & 4.05186 & N & 4360\\
& between & & .3907468 & .3333435 & 3.174173 & n & 545\\
& within & & .3622636 & -2.467201 & 3.204687 & T & 8\\[1ex] % extra vertical whitespace
educ & overall & 11.76697 & 1.746181 & 3 & 16 & N & 4360\\
etc\\
\end{tabular}
\end{table}
\end{document}
附录以满足 OP 的后续要求:为了使表格看起来(几乎)像是被 Stata 打印出来的,(i)使用列类型r
代替d
数字列,(ii)切换到等宽字体,例如通过修改宏\familydefault
。
\documentclass{article}
\usepackage{caption}
%% define some typographic struts
%% (from code by Claudio Beccari in TeX and TUG News, Vol. 2, 1993)
\newcommand\Tstrut{\rule{0pt}{2.9ex}} % "top" strut
\newcommand\Bstrut{\rule[-1.2ex]{0pt}{0pt}} % "bottom" strut
\begin{document}
\begingroup % localize the redefinition of \familydefault
\renewcommand{\familydefault}{\ttdefault}\selectfont
\begin{table}[htbp]
\caption{Summary Statistics} \label{tab:1}
\centering
\setlength\tabcolsep{4.5pt} % default value: 6pt
\begin{tabular}{@{} l l | rrrr | rr @{}} %
\multicolumn{2}{@{}l|}{Variable\Bstrut} & Mean & Std.Dev. & Min & Max & \multicolumn{2}{r@{}}{Observ.}\\
\hline
lwage\Tstrut & overall & 1.649147 & .5326094 & -3.579079 & 4.05186 & N & 4360\\
& between & & .3907468 & .3333435 & 3.174173 & n & 545\\
& within & & .3622636 & -2.467201 & 3.204687 & T & 8\\[1ex]
educ & overall & 11.76697 & 1.746181 & 3 & 16 & N & 4360\\
& between & & 1.747585 & 3 & 16 & n & 545\\
& within & & 0 & 11.76697 & 11.76697 & T & 8\\[1ex]
black & overall & .1155963 & .3197769 & 0 & 1 & N & 4360\\
& between & & .320034 & 0 & 1 & n & 545\\
& within & & 0 & .1155963 & .1155963 & T & 8\\[1ex]
hisp & overall & .1559633 & .3628622 & 0 & 1 & N & 4360\\
& between & & .3631539 & 0 & 1 & n & 545\\
& within & & 0 & .1559633 & .1559633 & T & 8\\[1ex]
exper & overall & 6.514679 & 2.825873 & 0 & 18 & N & 4360\\
& between & & 1.654918 & 3.5 & 14.5 & n & 545\\
& within & & 2.291551 & 3.014679 & 10.01468 & T & 8\\[1ex]
expersq & overall & 50.42477 & 40.78199 & 0 & 324 & N & 4360\\
& between & & 26.35134 & 17.5 & 215.5 & n & 545\\
& within & & 31.1431 & -44.07523 & 158.9248 & T & 8\\[1ex]
married & overall & .4389908 & .4963208 & 0 & 1 & N & 4360\\
& between & & .3766116 & 0 & 1 & n & 545\\
& within & & .3236137 & -.4360092 & 1.313991 & T & 8\\[1ex]
union & overall & .2440367 & .4295639 & 0 & 1 & N & 4360\\
& between & & .3294467 & 0 & 1 & n & 545\\
& within & & .2759787 & -.6309633 & 1.119037 & T & 8\\
\end{tabular}
\end{table}
\endgroup
\end{document}