Outreg2
is a community-contributed command, that helps us easily output the results of regressions run on Stata into a clean table, which can then be viewed in text, Word documents, or in LaTeX.
Using the auto.dta
dataset, I run the following regression:
sysuse auto.dta, clear
ssc install outreg2
gen wtsq = weight^2
foreach s in price headroom trunk{
xi: reg `s' weight wtsq, vce(robust)
outreg2 weight wtsq using tab_base_`s'_j, keep(weight wtsq) bdec(3) nocons tex(nopretty) replace
xi: reg `s' weight wtsq foreign, vce(robust)
outreg2 weight wtsq foreign using tab_base_`s'_j, keep(weight wtsq foreign) bdec(3) nocons tex(nopretty) append
xi: reg `s' weight wtsq foreign length, vce(robust)
outreg2 weight wtsq foreign length using tab_base_`s'_j, keep(weight wtsq foreign length) bdec(3) nocons tex(nopretty) append
}
I get output three .tex
files named tab_base_price_j
, tab_base_trunk_j
, and so on. When I open the .tex files in LaTeX and run them, I obtain the regression tables in PDF in a perfect format, just as I want.
However, each of these files in LaTeX has the following format:
\documentclass[]{article}
\setlength{\pdfpagewidth}{8.5in} \setlength{\pdfpageheight}{11in}
\begin{document}
\begin{tabular}{lccc} \hline
& (1) & (2) & (3) \\
*** ALL THE TABLE VALUES - DELETED from this illustration ***
\end{tabular}
\end{document}
If I want to create a new document (as a journal article or paper format), and I want to input one of these .tex files using
\input{tab_base_price_j.tex}
in LaTeX,
I receive this error: ! LaTeX Error: Can be used only in preamble.
How do I output the regression tables from Stata in a way that the output .tex
files do not have \begin{document}
, and just start with:
\begin{tabular}{lccc} \hline
& (1) & (2) & (3) \\
*** ALL THE TABLE VALUES - DELETED from this illustration ***
\end{tabular}
You just need to use the tex(fragment)
option:
sysuse auto.dta, clear
generate wtsq = weight^2
foreach s in price headroom trunk {
regress `s' weight wtsq, vce(robust)
outreg2 weight wtsq using tab_base_`s'_j.tex, keep(weight wtsq) bdec(3) nocons tex(fragment)
regress `s' weight wtsq foreign, vce(robust)
outreg2 weight wtsq foreign using tab_base_`s'_j.tex, keep(weight wtsq foreign) bdec(3) nocons tex(fragment)
regress `s' weight wtsq foreign length, vce(robust)
outreg2 weight wtsq foreign length using tab_base_`s'_j.tex, keep(weight wtsq foreign length) bdec(3) nocons tex(fragment)
}
You can then input these as parts of a larger document as follows:
\documentclass[10pt]{article}
\begin{document}
... text before inclusion of table tab_base_price_j.tex ...
\input{tab_base_price_j.tex}
... text after inclusion of table tab_base_price_j.tex ...
\input{tab_base_headroom_j.tex}
... text after inclusion of table tab_base_headroom_j.tex ...
\input{tab_base_trunk_j.tex}
... text after inclusion of table tab_base_trunk_j.tex ...
\end{document}