1

I have this table from Stata: enter image description here

EDIT: After taking Mico's suggestion I was able to create the table with the following codes:

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

However, the problem now is that they're not aligned properly like the original table. How do I do that?

  • @DavidCarlisle Not an exact duplicate, as I am doing the table in latex by hand. – OGC Feb 23 '15 at 19:13
  • the old q has gone so I deleted my comment:-) – David Carlisle Feb 23 '15 at 19:14
  • By "original table", do you mean the one printed by Stata, with a monospaced font and simple dashes in lieu of typographically appropriate "minus" symbols? – Mico Feb 25 '15 at 4:14
  • @Mico Yes, by 'original' I mean the Stata table. – OGC Feb 25 '15 at 4:20
3

I suggest you use the dcolumn package to align the numeric data on the decimal marker.

enter image description here

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

Addendum to address the OP's follow-up request: To make the table look (almost) like it was printed by Stata, (i) use r instead of d column types for the numeric columns, and (ii) switch to a monospaced font, e.g. by modifying the macro \familydefault.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.