2

I use Stata's esttab command to create various tables with t-test results. In Latex, I use siunitx for a proper layout of the table. However, I am unable to code the table as such does it flush right. There is always some blank space between my last column and the length of the table.

Here is my MWE:

\documentclass[a4paper,11pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[main=english, german, spanish]{babel}
\usepackage[T1]{fontenc} 
\usepackage{mathtools} 
\usepackage[np]{numprint} 
 \npthousandsep{,}\npthousandthpartsep{}\npdecimalsign{.} 
\usepackage{graphicx} 
\usepackage{float} 
\usepackage{array, booktabs}
\usepackage{longtable} 
\usepackage{tabularx}
\usepackage{dcolumn} 
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{array,ragged2e}
\setlength{\intextsep}{8.0pt plus 1.0pt minus 1.0pt} % To change the space between figure/table [ht] and text (underneath)
\setlength{\textfloatsep}{8.0pt plus 1.0pt minus 1.0pt} % To change the space between two floats (figure/table...) and text (underneath)    
\setlength{\belowcaptionskip}{5.0pt plus 1.0pt minus 1.0pt} % To change the space below float caption
\setlength{\abovecaptionskip}{5.0pt plus 1.0pt minus 1.0pt} % To change the space below float caption


\usepackage{siunitx} 
\sisetup{
        group-digits        = false ,
        input-signs     =-+ ,
        input-symbols       = ( ) [ ] - + {*} {**} {***},
        table-align-text-post   = false, 
        table-number-alignment = center, 
        table-figures-decimal=4,
        table-figures-exponent=4
        }



\begin{document}

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{MWE}
\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{3}{S[table-format=1.3] S[table-format=3] S[table-format=1.3] S[table-format=3] S[table-format=1.3,  table-space-text-post =\sym{***}] S[table-format=1.3] S[table-format=3]}}
\toprule
            &\multicolumn{2}{c}{High ratios}&\multicolumn{2}{c}{Low ratios}&\multicolumn{3}{c}{\textit{t} test}            \\\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-8}
            &      {Mean}&      {Obs.}&      {Mean}&      {Obs.}&     {Diff.}         &        {SE}&      {Obs.}\\
\midrule
\textit{divDm}&       0.397&         315&       0.418&         304&       0.021         &       0.040&         619\\
\textit{divRto}&       0.144&         315&       0.156&         304&       0.013         &       0.025&         619\\
\textit{divYld}&       0.012&         314&       0.012&         300&      -0.000         &       0.002&         614\\
\textit{intCov}&       3.663&         315&       7.267&         304&       3.604\sym{*}  &       1.458&         619\\
\textit{intCovAdj}&       1.433&         313&       1.687&         304&       0.254\sym{*}  &       0.111&         617\\
\textit{lvrg1}&       0.368&         315&       0.428&         306&       0.060\sym{***}&       0.015&         621\\
\textit{lvrg1Adj1}&       0.468&         315&       0.533&         304&       0.065\sym{***}&       0.013&         619\\
\textit{lvrg2}&       0.421&         315&       0.425&         306&       0.005         &       0.015&         621\\
\textit{lvrg2Adj1}&       0.511&         315&       0.517&         304&       0.006         &       0.012&         619\\
\textit{lvrg3}&       0.744&         315&       0.768&         306&       0.025         &       0.015&         621\\
\textit{lvrg3Adj1}&       0.781&         315&       0.805&         304&       0.024         &       0.013&         619\\
\textit{prfMrg}&       0.011&         315&       0.021&         306&       0.010         &       0.007&         621\\
\textit{prfMrgAdj}&       0.041&         313&       0.060&         304&       0.019\sym{**} &       0.006&         617\\
\textit{roa}&       0.016&         315&       0.030&         306&       0.014\sym{**} &       0.005&         621\\
\textit{roaAdj}&       0.026&         313&       0.039&         303&       0.013\sym{**} &       0.004&         616\\
\bottomrule
\multicolumn{8}{l}{\footnotesize * p<0.05, ** p<0.01, *** p<0.001}\\
\end{tabular*}
\end{table}

\end{document}

I guess there must be something wrong in the siunitx-setup or in the way I handle the table-format option. Could somebody suggest how I can fix the code so that the table aligns with the textwidth/length of table? I would like to maintain working with esttab.

2

too long that I can fit to comment:

  • Welcome to TeX.SE!
  • you define 22 column, but use only 8!
  • your table nicely fit into text width (well, after repairing @{\hskip\tabcolsep\extracolsep\fill} to more correct @{\hspace*{\tabcolsep}\extracolsep{\fill}}
  • so I can't figured out what is your problem ...

after done small changes to your code, I obtain the following result:

\documentclass[a4paper,11pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[main=english, german, spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\usepackage[np]{numprint}
 \npthousandsep{,}\npthousandthpartsep{}\npdecimalsign{.}
\usepackage{graphicx}
\usepackage{float}
\usepackage{array, booktabs, longtable, tabularx}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{ragged2e}
\setlength{\intextsep}{8.0pt plus 1.0pt minus 1.0pt} % To change the space between figure/table [ht] and text (underneath)
\setlength{\textfloatsep}{8.0pt plus 1.0pt minus 1.0pt} % To change the space between two floats (figure/table...) and text (underneath)
\setlength{\belowcaptionskip}{5.0pt plus 1.0pt minus 1.0pt} % To change the space below float caption
\setlength{\abovecaptionskip}{5.0pt plus 1.0pt minus 1.0pt} % To change the space below float caption
\usepackage{siunitx}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}

\begin{table}[htbp]
\sisetup{table-align-text-post = false}
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{MWE}
\begin{tabular*}{\hsize}{@{\hspace*{\tabcolsep}\extracolsep{\fill}}
            >{\itshape}l
                S[table-format=1.3] S[table-format=3] S[table-format=1.3] S[table-format=3]
                S[table-format=1.3,
                table-space-text-post =\sym{***}]
                S[table-format=1.3] S[table-format=3]}
\toprule
            &\multicolumn{2}{c}{High ratios}&\multicolumn{2}{c}{Low ratios}&\multicolumn{3}{c}{\textit{t} test}            \\\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-8}
            & {Mean}    & {Obs.}    & {Mean}    & {Obs.}    & {Diff.}   & {SE}  & {Obs.}    \\
\midrule
divDm       & 0.397     & 315       & 0.418     & 304       &  0.021    & 0.040 & 619       \\
divRto      & 0.144     & 315       & 0.156     & 304       &  0.013    & 0.025 & 619       \\
divYld      & 0.012     & 314       & 0.012     & 300       & -0.000    & 0.002 & 614       \\
intCov      & 3.663     & 315       & 7.267     & 304   & 3.604\sym{*}  & 1.458 & 619       \\
intCovAdj   & 1.433     & 313       & 1.687     & 304   & 0.254\sym{*}  & 0.111 & 617       \\
lvrg1       & 0.368     & 315       & 0.428     & 306   & 0.060\sym{***}& 0.015 & 621       \\
lvrg1Adj1   & 0.468     & 315       & 0.533     & 304   & 0.065\sym{***}& 0.013 & 619       \\
lvrg2       & 0.421     & 315       & 0.425     & 306   & 0.005         & 0.015 & 621       \\
lvrg2Adj1   & 0.511     & 315       & 0.517     & 304   & 0.006         & 0.012 & 619       \\
lvrg3       & 0.744     & 315       & 0.768     & 306   & 0.025         & 0.015 & 621       \\
lvrg3Adj1   & 0.781     & 315       & 0.805     & 304   & 0.024         & 0.013 & 619       \\
prfMrg      & 0.011     & 315       & 0.021     & 306   & 0.010         & 0.007 & 621       \\
prfMrgAdj   & 0.041     & 313       & 0.060     & 304   & 0.019\sym{**} & 0.006 & 617       \\
roa         & 0.016     & 315       & 0.030     & 306   & 0.014\sym{**} & 0.005 & 621       \\
roaAdj      & 0.026     & 313       & 0.039     & 303   & 0.013\sym{**} & 0.004 & 616       \\
\bottomrule
\multicolumn{8}{l}{\footnotesize * p<0.05, ** p<0.01, *** p<0.001}\\
\end{tabular*}
\end{table}

\end{document}

enter image description here

|improve this answer|||||
  • Dear Zarko, thank you so much, with the edit of the \hspace command my code works perfectly! So after all it is an esttab problem which means that I will manually adapt the code after having used Stata. Just of out of interest: what do you mean by defining 22 columns? Where did I define 22? – Britta Jul 31 '19 at 8:56
  • @Britta, in your table preamble. with *{3}{...} you declare that definitions in ... are repeated three times. Since there you have declaration for seven columns, all together you have 21 and plus defined before (l). About Stata I cant help you, I'm not familiar with it. If you find answer useful, you can accept it (by clicking on the check mar at the top left side of answer) :-) – Zarko Jul 31 '19 at 9:04

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.