Stata to Latex – Part 2: Formatting Latex

UPDATE: This post has been updated to fix the decimal alignment in Latex (May 2017)

In this post, we want to generate a latex file using the stacked standard errors to keep results compact. Using the example in the previous post (Stata to Latex – Part 1), we add new regressions to the table to generate a more complicated output of the type shown below:

The table produces two regressions using price and foreign as dependent variables and regresses them on weight, and mpg. A second regression adds Length as an explanatory variable which is indicated under the table. Each regression has two specifications, where one normal standard errors are shown in round parentheses, while robust standard errors are shown in square brackets. This makes a total of eight regressions, which in a normal form, would make a very lengthy table. Given that the coefficients do not change, it is simpler to present by stacking the standard errors.

The stata code for generating the table is given in two parts. The first part generates the headers, the columns, and the first set of estimates. The second part adds the next set of rows. The code is given as follows:
sysuse auto, clear

replace weight = weight * 0.000453592  // we convert weight from pounds into tons.
lab var weight “Weight(tons)”

eststo clear   // clear any existing stored estimates

foreach x in price foreign {              // each loop generates a column for the tex file
    eststo: regress `x’ weight mpg
    estadd local Length “No”              // here we add information on the controls at the end of the table

    eststo: regress `x’ weight mpg length
    estadd local Length “Yes”
esttab using “regressions_stacking.tex”, replace f ///
cells(b(fmt(2)) se(fmt(2) star par)) keep(weight) ///
label booktabs noobs nonotes nomtitle collabels(none)  ///
mgroups(“Price” “Foreign”, pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

Several new commands are added here:
f: is for fragment which prevents stata from adding additional latex table commands. These will be used directly in the Latex file.
booktabs: Latex code for formatting tables using the booktabs packages. Gives a neater output.
mgroups: Creates the group header together with the line. Note the latex code inserted directly into Stata syntax.

eststo clear
foreach x in price foreign {

lab var weight ” “
eststo: regress `x’ weight mpg, vce(cluster make)
estadd local Length “No”

eststo: regress `x’ weight mpg length, vce(cluster make)
estadd local Length “Yes”

esttab using “regressions_stacking.tex”, append f ///
cells(se(fmt(2) star par([ ]))) keep(weight) ///
varlabels(, elist(weight \bottomrule)) ///
label booktabs nomtitle noline nonumber collabels(none)  ///
scalars( “Length”)

Additional commands used to generate this table:
varlabels: This command adds a line below the last estimate. Since we are using the noline option here (the first part adds the necessary line), we still need to insert a divider between the last line of the estimates and the observations.
scalars: This adds the indicator for the controls we defined. Several scalars can be added to this file.

The above commands will create a tex file. We now have to insert this in a latex file to generate the output. We use the following preamble in the latex file:


\usepackage{booktabs} % For better looking tables
\usepackage{dcolumn} % Align on the decimal point of numbers in tabular columns
\usepackage{threeparttable} % For better formatting of table notes
urlcolor=black]{hyperref} % for linking between references, figures, TOC, etc in the pdf document

Joerg Weber’s brilliant website: shows how esttab outputs can be customized. We make use of two of his user written commands: estwide, and Fignotes.

Estwide generates a table that is evenly spaced spanning the whole length of the page:

\let\estinput=\input % define a new input command so that we can still flatten the document




We add the document tex preamble, now the main body of text:
\title{Stacking variables: From Stata to Latex}


\author{Asjad Naqvi\thanks{WU, Wien. \href{}{}}}


Adding the text on the section:


Specification 1:
x_i = \beta_0 + \beta_1 Weight_i + \beta_2 Mpg_i

Specification 2:
x_i = \beta_0 + \beta_1 Weight_i + \beta_2 Mpg_i + \beta_3 Length_i

Results are given in Table ??.

The stata generated tex table can now be inserted as:

\caption{Customized table} \label{table}
\Fignote{Normal standard errors are in parentheses, robust standard errors in square brackets. Significance levels: * 10\%, ** 5\%, *** 1\%}


This will create the final file which should look something like this:


Previous post:
Stata to Latex – Part 1 –

8 thoughts on “Stata to Latex – Part 2: Formatting Latex

  1. Hello. Thanks for the example, is really useful. I have one question though, in which part you calculate and display the robust standard errors? Many thanks in advance! Regards,


    1. Hi Pablo,
      Robust standard errors are calculated using this line “regress `x’ weight mpg length, vce(cluster make)”.

  2. How do you align on decimal points? I see that you have a command for this but your PDF output is not really aligned. Thanks much!

    1. Hi Giri,

      You can use the dcolumn package to align the decimals in the PDF file:

      Make sure all the packages are updated! I am updating the PDF to reflect this as well.


  3. Hi,
    thanks so much for this tutorial, I am learning how to use TeX with Stata and this was precious.

    However, I used your code with different output from STATA that included 6 columns, and the table lines (all of them) do not span the whole table, ending short on the right side. Why could this be?


    1. Hi Andrea,

      You might to play a bit with estwide options. For example try changing the number in the line below from 7 to the number of columns your table has:


  4. Hello,

    your explanations were really clear, thank you very much. But this seems to only work when we output 1 explanatory variable (using append, for instance with 5 x_i, gives the 5 coefficient and s.e. of model with homoskedasticity and, after, the 5 s.e. of heteroskedasticity model). Could you give me a hint of how to reproduce the same output layout, but with more than one x_i?

    Thank you very much!

Leave a Reply

Your email address will not be published.