# Stata to Latex: Formatting a table - width, wrapping notes and caption position

I'm a newbie to stata and latex when it comes to direct export of tables. After reading some documentation, I used the following code (after labeling the variables):

eststo: quietly regress defgh hello, robust
eststo: quietly regress defgh hello lnGDP, robust
esttab using abc.tex, replace alignment(D{.}{.}{-1}) width(1\hsize) se ar2 title("abc") label addnotes("blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla")
eststo clear


(because some of you asked: No, abc, defgh, hello and blablabla... are not the real regressions. I just replaced my real names with these!)

I insert the table with:

\input{Tables/abc.tex}


I basically have 3 issues:

1) The table is too wide (prob. due to hsize) (how can I tell latex that it should only consider the wide of the first row?)

2) The caption of the second regression moves to somewhere else (also had it a bit more left, but still not where it belongs)

3) My table notes don't get wrapped... Is it also possible to put them in a nice box?

The tex-file (code) produced:

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else$$^{#1}$$\fi}
\caption{abc}
\begin{tabular*}{1\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{2}{D{.}{.}{-1}}}
\hline\hline
&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}\\
&\multicolumn{1}{c}{defgh}&\multicolumn{1}{c}{defgh}\\
\hline
Hello Everybody     &      -0.493\sym{***}&      -0.317\sym{***}\\
&     (0.106)         &    (0.0403)         \\
[1em]
ln(GDP per capita)  &                     &       6.466\sym{***}\\
&                     &     (0.349)         \\
[1em]
Constant            &       39.75\sym{***}&       26.45\sym{***}\\
&     (1.188)         &     (0.801)         \\
\hline
Observations        &         146         &         145         \\
Adjusted $$R^{2}$$  &       0.146         &       0.780         \\
\hline\hline
\multicolumn{3}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{3}{l}{\footnotesize blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla}\\
\multicolumn{3}{l}{\footnotesize \sym{*} $$p<0.05$$, \sym{**} $$p<0.01$$, \sym{***} $$p<0.001$$}\\
\end{tabular*}
\end{table}

• Welcome to TeX.SE. Please edit your posting to show the LaTeX code that gives rise to the screenshot you posted. Aside: Are you really trying to typeset ablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla....? – Mico Jun 5 '19 at 14:05
• Hi Mico! Added everything to my question you asked for! :) – Toni Jun 5 '19 at 14:55
• I'm afraid your edits didn't help clarify the main issues. If I understand your query correctly, you employ Stata to generate some regressions, and you provide some instructions so that Stata outputs the regression table as LaTeX code. So far, you've shown us the results of running that code through LaTeX. However, unless I've missed it completely, you haven't shown us the LaTeX code itself. This site is about TeX, LaTeX , and friends. If you seek help with the LaTeX code, you should show us the LaTeX code. If you need help with Stata, you should probably post your query somewhere else. – Mico Jun 5 '19 at 16:23
• Right, sorry for my misunderstanding! – Toni Jun 5 '19 at 19:59

1. Use fraction of \linewidth instead of \hsize. If you do not know the approximately width of the table, use ordinary tabular-environment instead of tabular*. LaTeX will then calculate the table width. Also, I have corrected your setup of extracolsep.

2. Load the caption package to format captions correctly. I also suggest to wrap the tabular in a threeparttable-environment, it is the easiest way to adjust the width of the caption to the width of the table. Additional benefit is that you get better control of you table notes.

3. You have to define the multicolumns that have the long text as p{<wd>}-columns with approximately the same width as the tabular. In addition, blablabla is one long word, and LaTeX cannot break it. You either have to make breakpoints (spaces, etc.) or use a package that will break long word automatically, for example seqsplit.

If your "blabla" is a sentence, just use the command:

\multicolumn{3}{@{}p{0.70\linewidth}@{}}{\footnotesize Write your sentence
here, and it can be many words as you desire. The problem with no-breakable
long word is not there in a sentence of words.}\\

1. I have loaded booktabs and changed the hlineto \toprule, \midrule and bottomrule. It gives more space above and below the rules. In addition, I have increased the space between text and rule by the \setlength{\extrarowheight}{1pt}. Also, I removed the side bearing left and right by adding @{} in the beginning and end of the column declaration.

\documentclass{article}
\usepackage{array, dcolumn, seqsplit, booktabs, threeparttable}
\usepackage[format=hang, justification=raggedright, textfont=it]{caption}
\setlength{\extrarowheight}{1pt}
\begin{document}
\noindent\begin{table}[tbp]
\def\sym#1{\ifmmode^{#1}\else$$^{#1}$$\fi}

\begin{threeparttable}
\caption{Abc - some more text to see if the captiontext wraps as I assumed}

\begin{tabular*}{0.75\linewidth}{@{}l@{\extracolsep{\fill}}*{2}{D{.}{.}{-1}}@{}}
\toprule
&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}\\
&\multicolumn{1}{c}{defgh}&\multicolumn{1}{c}{defgh}\\
\midrule
Hello Everybody     &      -0.493\sym{***}&      -0.317\sym{***}\\
&     (0.106)         &    (0.0403)         \\
[1em]
ln(GDP per capita)  &                     &       6.466\sym{***}\\
&                     &     (0.349)         \\
[1em]
Constant            &       39.75\sym{***}&       26.45\sym{***}\\
&     (1.188)         &     (0.801)         \\
\midrule
Observations        &         146         &         145         \\
Adjusted $$R^{2}$$  &       0.146         &       0.780         \\
\bottomrule
\multicolumn{3}{@{}l@{}}{\footnotesize Standard errors in parentheses}\\
\multicolumn{3}{@{}p{0.70\linewidth}@{}}{\framebox{\parbox{\linewidth
{\footnotesize
\seqsplit{blablablablablablalablablablablablablablablablablablablabla
blablablablablablablablablablablablablablablablablablablablablabla}}}}\\
\multicolumn{3}{@{}l@{}}{\footnotesize \sym{*} $$p<0.05$$, \sym{**} $$p<0.01$$, \sym{***} $$p<0.001$$}\\
\end{tabular*}
\end{threeparttable}
\end{table}
\end{document}


• Amazing, MANY MANY thanks! This is exactly what I was looking for! Is there also an option to put my "Blabla" into a small box? – Toni Jun 7 '19 at 13:02
• @Toni See updated code, where I assume you want a frame around the blabla. It looks awful. If you want a more fancy box, look at tcolorbox, but I suggest to not have such frames – Sveinung Jun 7 '19 at 13:43
• @Toni If the answer was acceptable for you, you may click the accept button under the numbers at the left side. – Sveinung Jun 7 '19 at 13:49
• You are right, it looks weird. Thanks still! One more: As my notes are (ofcourse) not blablabla, but sentences, latex now leaves out all spaces and combines the words to one word... So "GDP per capita, 5 year average" gets to "GDPpercapita,5yearaverage"... – Toni Jun 7 '19 at 14:23
• @toni Then you just remove the \seqsplit-command. \multicolumn{3}{@{}p{0.70\linewidth}@{}}{\footnotesize Write your sentence here, and it can be many words as you desire. The problem with no-breakable long word is not there in a sentence of words.}\\ – Sveinung Jun 7 '19 at 14:54