Search code examples
latexstata

Variable labels on multiple lines in LaTeX when exporting results with esttab


I am trying to export some regression results to LaTeX using the community-contributed command estate in Stata. I want to use variable labels instead of variable names, but some of the labels are so long that the width of the table does not fit nicely on the page once I export it to LaTeX.

Example data can be found below:

* Example generated by -dataex-. To install: ssc install dataex
clear
input double areakey float(state d1 d2) int(bfunemp otnenon bmunemp)
1015001100 1 0 1   0 26  0
1033020701 1 0 1  27  1 33
1073003803 1 0 1 208  0 78
1073005702 1 0 1  76  0 88
1073011904 1 0 1  35  0 44
1073013200 1 0 1  11  0 59
1097006407 1 0 0   0  0  9
1097007300 1 0 1   9  0  0
1111000600 1 0 0   6  0  1
1115040201 1 0 0   4  0  2
1115040204 1 0 0   3  5  6
1117030317 1 1 1   0  0  0
1117030320 1 0 0  11  0  0
1117030336 1 0 0   0  0  0
1125011600 1 0 1  91  0 88
1125011701 1 0 1  73  0 41
4013112100 4 0 0   0  0  0
4013112301 4 0 1   8  0 25
4013112601 4 0 1   0  0  0
4013112700 4 0 0   0  0  0
end
label var areakey "GEOID10" 
label var state "State FIPS code" 
label var d1 "Distance <= 1 Dummy" 
label var d2 "Distance <= 2 Dummy" 
label var bfunemp "Unemployed Black/Afr. Am. females 16+ yo." 
label var otnenon "Pers. 18-64 yo. other lang. not well/no Eng." 
label var bmunemp "Unemployed Black/Afr. Am. males 16+ yo." 

In addition, the code I am using is the following:

global xvars bfunemp otnenon bmunemp

eststo mreg1: quietly areg d1 $xvars, absorb(state) robust /// 
   cluster(state)
eststo mreg2: quietly areg d2 $xvars, absorb(state) robust /// 
   cluster(state)

keep if indicatorvar > 0

eststo mreg3: quietly areg d1 $xvars, absorb(state) robust /// 
   cluster(state)
eststo mreg4: quietly areg d2 $xvars, absorb(state) robust /// 
   cluster(state)

esttab mreg1 mreg3 mreg2 mreg4 using /// 
   "$repodir/output/tables/tract_mregs.tex", ///
   replace booktabs longtable mtitles("y1" "y1" "y2" "y2") /// 
   s(modelsample modelobs, label("Sample" "N")) se /// 
   noconstant nonumbers nonotes label star(* 0.10 ** 0.05 *** 0.01)

The result after I compile the TeX file is a table where the left margin is fine but the contents of the table extend off the right side of the page such that there is no margin.

Is there a way of specifying the format so that the labels can extend to multiple lines?


Solution

  • Substituting option longtable with wrap will produce the desired output after you compile:

    esttab mreg1 mreg3 mreg2 mreg4, tex wrap ///
       mtitles("y1" "y1" "y2" "y2") /// 
       s(modelsample modelobs, label("Sample" "N")) se /// 
       noconstant nonumbers nonotes label star(* 0.10 ** 0.05 *** 0.01)
    
    {
    \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
    \begin{tabular}{l*{4}{c}}
    \hline\hline
                        &\multicolumn{1}{c}{y1}&\multicolumn{1}{c}{y1}&\multicolumn{1}{c}{y2}&\multicolumn{1}{c}{y2}
    > \\
    \hline
    Unemployed          &  -0.0000359         &  -0.0000359         &   -0.000489         &   -0.000489         \\
    Black/Afr. Am. females 16+ yo.& (0.0000876)         & (0.0000876)         &  (0.000770)         &  (0.000770)   
    >       \\
    [1em]
    Pers. 18-64 yo.     &    -0.00555\sym{**} &    -0.00555\sym{**} &      0.0244\sym{**} &      0.0244\sym{**} \\
    other lang. not well/no Eng.&  (0.000143)         &  (0.000143)         &   (0.00126)         &   (0.00126)     
    >     \\
    [1em]
    Unemployed          &    -0.00180\sym{*}  &    -0.00180\sym{*}  &      0.0109         &      0.0109         \\
    Black/Afr. Am. males 16+ yo.&  (0.000221)         &  (0.000221)         &   (0.00194)         &   (0.00194)     
    >     \\
    \hline
    Sample              &                     &                     &                     &                     \\
    N                   &                     &                     &                     &                     \\
    \hline\hline
    \end{tabular}
    }
    

    In addition, the option varwidth() can also help adjusting the length of the first column.