3

I have the following issue: I'm exporting regression output from Stata and inputing into my LaTex document. My goal is to write headers and footers in latex and simply input the raw "table-data" with \input{}. I use tabularx and align at the decimal. But I also want to print the number of observations in my table. These are round about 250-1500, they are aligned at the decimal so to speak, but since they have no decimal they throw of the visual effect of decimal alignment. I would rather instead center them in the column. I know this is possible by putting a \mulitcolumn{}{}{} environment around it, but this would require manual changes every time I update the table that is created by Stata. I'm looking for a scalable solution. Is it possible to somehow define a columntype that is flexible enough to align numbers between {-1,1} at the decimal, and center numbers without decimals.

Here is an example of my table:

\newcolumntype{Y}{D..{1.5}}
\newcolumntype{L}[1]{>{\raggedright}p{#1}}
\begin{table}
\begin{minipage}{\textwidth}
{\footnotesize
\lipsum[5]}
\end{minipage}
\begin{tabularx}{\textwidth}{L{3.8cm} @{\extracolsep{\fill}}*{2}{Y}}
\toprule
\multicolumn{1}{l}{} & \multicolumn{1}{c}{(1)}  & \multicolumn{1}{c}{(2)}  \\
\midrule
\input{tab}
\midrule
Firm FE & \multicolumn{1}{c}{X}& \multicolumn{1}{c}{X}    \\
Time FE & \multicolumn{1}{c}{X}& \multicolumn{1}{c}{X} \\
\bottomrule
\end{tabularx}
\end{table}

where the file tab.tex is saved directly from Stata and contains the following code:

Variable 1 & 0.012 & -0.542 \\
Variable 2 & 0.523 & 0.235 \\
\midrule
No. of Obs. & 1459 & 255 \\
3

I've tried to do something similar recently, and didn't find a way to solve it from the LaTeX side. From the Stata side it can be done by modifying the output table a bit. Here is the Stata code which uses esttab:

clear all
set more off
set obs 1000
gen x1 = rnormal()
gen x2 = rnormal()
gen y = 1+2*x1+3*x2+rnormal()
gen z = 1+3*x1+2*x2+rnormal()

eststo clear
eststo: reg y x1 x2
estadd local firm "\multicolumn{1}{c}{X}"
estadd local year "\multicolumn{1}{c}{X}"
estadd local obs "\multicolumn{1}{c}{`e(N)'}"
eststo: reg z x1 x2
estadd local firm "\multicolumn{1}{c}{X}"
estadd local year "\multicolumn{1}{c}{X}"
estadd local obs "\multicolumn{1}{c}{`e(N)'}"
esttab using table.tex, replace b(a3) nonum mtitles("y" "z") ///
       starlevels($^{*}$ .1 $^{**}$ .05  $^{***}$ 0.01) ///
       se label nonotes noobs booktabs align(S) ///
       scalars("obs No. of Obs." "firm Firm FE" "year Year FE") 

As you can see, instead of reporting just the number of observations it emits all the \multicolumn code. Also, it uses the S alignment character which assumes usage of siunitx. The corresponding LaTeX code can be the following (the key siunitx setting is input-symbols={()}, without it the standard errors cannot be parsed):

\documentclass[twocolumn]{article}
\usepackage{siunitx,booktabs}
\sisetup{group-digits=false,parse-numbers=true,input-symbols={()},table-align-text-post=false,
         table-number-alignment=center,table-figures-integer=3,table-figures-decimal=5}
\begin{document}
\begin{table}[ht]
\input{table}
\end{table}
\end{document}

And for the reference, I also show the generated table.tex:

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{2}{S}}
\toprule
                    &\multicolumn{1}{c}{y}&\multicolumn{1}{c}{z}\\
\midrule
x1                  &       1.952$^{***}$&       2.971$^{***}$\\
                    &    (0.0318)        &    (0.0314)        \\
\addlinespace
x2                  &       3.013$^{***}$&       1.992$^{***}$\\
                    &    (0.0308)        &    (0.0304)        \\
\addlinespace
Constant            &       1.067$^{***}$&       0.987$^{***}$\\
                    &    (0.0310)        &    (0.0306)        \\
\midrule
No. of Obs.         &\multicolumn{1}{c}{1000}        &\multicolumn{1}{c}{1000}        \\
Firm FE             &\multicolumn{1}{c}{X}        &\multicolumn{1}{c}{X}        \\
Year FE             &\multicolumn{1}{c}{X}        &\multicolumn{1}{c}{X}        \\
\bottomrule
\end{tabular}
}

Another idea could be in using some kind of a postprocessor and add the \multicolumn stuff after the table is generated.

The result is: enter image description here

1
  • 1
    Thanks a lot! Very helpful. – user444417 Oct 30 '19 at 12:46
2

You can use siunitx. This requires some massage to the table code, but the result is much better.

Entries you want centered in their S column are simply entered bracing them.

\documentclass[twocolumn]{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}[ht]

\begin{tabular*}{\columnwidth}{
  @{\extracolsep{\fill}}
  l
  S[table-format=1.3]
  S[table-format=-1.3]
}
\toprule
 & {(1)} & {(2)}  \\
\midrule
Variable 1 & 0.012 & -0.542 \\
Variable 2 & 0.523 & 0.235 \\
\midrule
No. of Obs. & {1459} & {255} \\
\midrule
Firm FE & {X}& {X} \\
Time FE & {X}& {X} \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

I used twocolum, hoping you have a two column paper (a three column table as wide as the global text width would be very difficult to read).

enter image description here

1
  • 1
    Thanks very much. – user444417 Oct 30 '19 at 12:46

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.