Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scientific notation from esttab, compatible with siunitx

    Hello.

    I am using esttab from Ben Jann (available at the SSC archive). I produce regression tables with mixed format for coefficients (including scientific notation). I would like to use the siunitx package for my tables in latex. After a bit of search on the web, the solution that I found to avoid latex compilation errors is to wrap estimates in scientific notation with the command \num{}.

    I am trying to find a way to output regression estimates in scientific notation from esttab with \num{} around them. Here's a MWE

    Code:
    sysuse auto, clear
    qui reg price weight mpg
    esttab , b(3 3 %10.7e) t(3)
    The esttab output is
    HTML Code:
    ----------------------------
                          (1)   
                        price   
    ----------------------------
    weight              1.747**
                      (2.723)   
    
    mpg               -49.512   
                     (-0.575)   
    
    _cons           1.946e+03   
                      (0.541)   
    ----------------------------
    N                      74   
    ----------------------------
    My goal for the .tex file that esttab generates would be to output "\num{1.946e+03}" for the intercept estimate instead. I tried to play with esttab/estout options such as transform or layout, but only got syntax errors. Any feedback would be appreciated.

    Regards,
    Georgios

  • #2
    You can use the -cell()- option (see -help estout-) to construct cell contents that includes stuff like \num{}. Example:
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . qui reg price weight mpg
    
    . esttab , cell(b(f(3 3 %10.7e) par(\num{ })) t(f(3) par((\num{ }))))
    
    -------------------------
                          (1)
                        price
                          b/t
    -------------------------
    weight        \num{1.747}
                 (\num{2.723})
    mpg          \num{-49.512}
                 (\num{-0.575})
    _cons        \num{1.946e+03}
                 (\num{0.541})
    -------------------------
    N                      74
    -------------------------

    Comment


    • #3
      Great! Thank you very much Ben.

      Comment


      • #4
        Hi,
        why not change the alignment of the table column to S (a special column variant of the siunitx package for parsing table contents)? You can change parsing and display of the table's contents either with an option to the column specifier (S[table-format=3.2]), or globally with \sisetup{}.

        From the TeX perspective, I think it's more complicated to parse each number separately (as you want to) instead of the whole table column.

        Regards
        Bela
        Last edited by Daniel Bela; 26 Oct 2017, 02:43.

        Comment


        • #5
          Hi Daniel.

          Thanks for your feedback. I use S[table-format=2.4] and \num{} around the numbers with scientific format allows them to print properly, although not aligned at the decimal point. This would be fine by me, provided that the rest of the betas and t-stats in decimal format align properly. Ben's solution above wraps all numbers with \num{}, so it's not quite what I intended to have because it messes up the alignment for all numbers in the table. I tried to use something like S[table-format=-1.2e-1] instead, but I get latex compilation errors. I can't find on the web any \sisetup{} options that allow mixed formats that are also aligned at the decimal and work with the longtable package that I use.

          I understand that this is a Stata forum, but if you have any latex/siunitx syntax recommendations I would appreciate it.

          Regards,
          Georgios

          Comment


          • #6
            Hi again,

            you should read the package documentation to siunitx, available on CTAN. It's a worth read giving insights into what siunitx does, the option "table-format=X.Y" was just an example that I quickly came up with; there is a vast mass of other, more detailed options that can be set using \sisetup{} in order to fix alignment and parsing of numbers, including scientific notation -- I did not encounter a table yet that could _not_ be properly aligned using these mechanisms.

            Regards
            Bela

            Comment

            Working...
            X