6

I'm testing Stata → LaTeX integration, and reading some guides, I was satisfied with the results. Unfortunately, it seems that one can only use the dcolumn package in Stata. Knowing that the siunitx package is the one that many people prefer around here, I changed that part of the code manually. However, for some reason the stars ( *** ) on 3673.1 are far away, and the ( of the corresponding standard error estimate is not properly aligned. I am not sure what is wrong? Don't worry about the double estimates, I by mistake did not clear the eststo function.

Here is a picture:

enter image description here

The code doesn't look particularly clean to me (it is produced using the esttab command in Stata), but it does its job.

MWE

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{input-symbols = ()}
\begin{document}
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{This is a regression table\label{tab1}}
\begin{tabular}{l*{4}{S}}
\toprule
                &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\\
                &\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}\\
\midrule
Weight (lbs.)       &       1.747\sym{**} &       3.465\sym{***}&       1.747\sym{**} &        3.465\sym{***}\\
                &     (0.641)         &     (0.631)         &     (0.641)         &       (0.631)         \\
\addlinespace
Mileage (mpg)       &      -49.51         &       21.85         &      -49.51         &       21.85         \\
                &     (86.16)         &     (74.22)         &     (86.16)         &     (74.22)         \\
\addlinespace
Car type            &                     &      3673.1\sym{***}&                     &      3673.1\sym{***}\\
                &                     &     (684.0)         &                     &     (684.0)         \\
\addlinespace
Constant            &      1946.1         &     -5853.7         &      1946.1         &     {-5853.7}         \\
                &    (3597.0)         &    (3377.0)         &    (3597.0)         &    (3377.0)         \\
\midrule
Observations        &          {74}         &          {74}         &          {74}         &          {74}         \\
Adjusted \(R^{2}\)  &       0.273         &       0.478         &       0.273         &       0.478         \\
\bottomrule
\multicolumn{5}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{5}{l}{\footnotesize Source: UCT Institutional Planning Department}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \   (p<0.001\)}\\
\end{tabular}
\end{table}
\end{document}

2 Answers 2

6

You need to tell siunitx two things:

  1. Reserve the space for note marks. This is done by the command \sisetup{table-space-text-post = \sym{***}}

  2. Do not align the additional text, so it may be put close to the numbers: \begin{tabular}{l*{4}{S[table-align-text-post=false]}}

Here is the result

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\pagestyle{empty}
\sisetup{input-symbols = ()}
\begin{document}
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{This is a regression table\label{tab1}}
\sisetup{table-space-text-post = \sym{***}}
\begin{tabular}{l*{4}{S[table-align-text-post=false]}}
\toprule
                &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\\
                &\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}\\
\midrule
Weight (lbs.)       &       1.747\sym{**} &       3.465\sym{***}&       1.747\sym{**} &        3.465\sym{***}\\
                &     (0.641)         &     (0.631)         &     (0.641)         &       (0.631)         \\
\addlinespace
Mileage (mpg)       &      -49.51         &       21.85         &      -49.51         &       21.85         \\
                &     (86.16)         &     (74.22)         &     (86.16)         &     (74.22)         \\
\addlinespace
Car type            &                     &      3673.1\sym{***}&                     &      3673.1\sym{***}\\
                &                     &     (684.0)         &                     &     (684.0)         \\
\addlinespace
Constant            &      1946.1         &     -5853.7         &      1946.1         &     {-5853.7}         \\
                &    (3597.0)         &    (3377.0)         &    (3597.0)         &    (3377.0)         \\
\midrule
Observations        &          {74}         &          {74}         &          {74}         &          {74}         \\
Adjusted \(R^{2}\)  &       0.273         &       0.478         &       0.273         &       0.478         \\
\bottomrule
\multicolumn{5}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{5}{l}{\footnotesize Source: UCT Institutional Planning Department}\\
\multicolumn{5}{l}{\footnotesize \sym{*} $p<0.05$, \sym{**} $p<0.01$, \sym{***} \   ($p<0.001$}\\
\end{tabular}
\end{table}
\end{document}

enter image description here

2
  • Thanks, looks good. Any idea about those brackets? I find it strange they the only ones which look funny. Commented May 23, 2014 at 16:48
  • siunitx tries to align the numbers in brackets. Try S[table-align-text-post=false, parse-numbers=false] - the result is aligned differently, but brackets are typeset closely to the numbers.
    – Boris
    Commented May 23, 2014 at 18:52
3

Since siunitx Version 3, a place holder for text before and after numeric values can be input to table-format directly. This should be the widest text used in the column to reserve propper spacing. The usage of \sym becomes obsolete.

\begin{tabular}{ S[table-format=1.3{***} ]

Alignment can be controlled via the options table-align-text-before and table-align-text-after:

\begin{tabular}{ S[table-format=1.3{***}, table-align-text-after=false ]

This yields:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\pagestyle{empty}
\sisetup{input-symbols = ()}
\begin{document}
    \begin{table}\centering
        \caption{This is a regression table\label{tab1}}
        \begin{tabular}{l*{4}{S[table-format=-4.3{***}, table-align-text-after=false]}}
            \toprule
            &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\\
            &\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}&\multicolumn{1}{c}{Price}\\
            \midrule
            Weight (lbs.) & 1.747{**}&  3.465{***}&   1.747{**}&   3.465{***}\\
            & (0.641)   &   (0.631)   &   (0.641)   & (0.631)     \\
            \addlinespace
            Mileage (mpg) &    -49.51   & 21.85   &    -49.51   & 21.85     \\
            & (86.16)   &   (74.22)   &   (86.16)   &   (74.22)     \\
            \addlinespace
            Car type  &     &    3673.1{***}&       &    3673.1{***}\\
            &       &   (684.0)   &     &   (684.0)     \\
            \addlinespace
            Constant  &    1946.1   &   -5853.7   &    1946.1   &   -5853.7     \\
            &    (3597.0)   &  (3377.0)   &  (3597.0)   &  (3377.0)     \\
            \midrule
            Observations  &    {74}   &    {74}   &    {74}   &    {74}     \\
            Adjusted \(R^{2}\)& 0.273   & 0.478   & 0.273   & 0.478     \\
            \bottomrule
            \multicolumn{5}{l}{\footnotesize Standard errors in parentheses}\\
            \multicolumn{5}{l}{\footnotesize Source: UCT Institutional Planning Department}\\
            \multicolumn{5}{l}{\footnotesize {*} $p<0.05$, {**} $p<0.01$, {***} \   $p<0.001$}\\
        \end{tabular}
    \end{table}
\end{document}

result from the code snipped above

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .