Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: Is there Sweave tool for stata

From   Eric Booth <>
To   <>
Subject   Re: st: Re: Is there Sweave tool for stata
Date   Fri, 8 Jul 2011 21:03:14 -0500

On Jul 8, 2011, at 7:23 PM, Twaha Daudi wrote:

> There was an error - see .log for details
> ERROR -- Text file
> "C:\Users\User\Desktop\Test\Stata-test-StataEngine.log" not found

My guess is that path doesnt exist on your computer (unless your username is 'user'), so you've likely specified your paths wrong when you set up the program.  Beyond re-installing , the only place I know to check/edit those paths is in your statweave.cfg file (you can edit this in a text editor).  

I never really got into StatWeave, and I think the combination of -texdoc- (from SSC) and Stata (+ your TeX software of choice) is a more straightforward solution. StatWeave seems like an unnecessary middle step to me (but others may know some advantages that I missed).

You can typeset the document created from the Stata do-file by using the command line as dictated by your TeX program.  So, I use MacTeX for Mac MikTeX for Windows and include this section at the end of all my do-files that 'weave' together text, code, and Stata tables/graphs/output via -texdoc-:

**************detect OS and compile:
if "`c(os)'" == "Windows"{
cap rm "report.pdf"
*run pdflatex twice for hyperref
	!pdflatex "report.tex"
	!pdflatex "report.tex"
if "`c(os)'" == "MacOSX" {
cap rm "report.pdf"
	!/usr/texbin/pdflatex  "report.tex"  "report" 
	!/usr/texbin/pdflatex  "report.tex"  "report" 

You can include the makeindex or biblatex commands in there too.

Below is a minimal, example do-file that analyzes some data and writes & typesets a document with the analysis results all from a single do-file.  The resulting report.tex file is about 40 lines of code.  There are some commands in the 'setup' section below that install some packages you need and copy some .tex files for -tabout- from my dropbox.

Watch out for wrapping issues:

if "`c(os)'" == "MacOSX" global ext pdf
if "`c(os)'" == "Windows" global ext png
foreach v in  texdoc texsave sjlatex tabout outreg2 {
		cap which `v'
		if _rc ssc install `v', replace
**copies files from my DropBox:
foreach f in bot.tex top.tex stata.sty {
copy`f'  `f', replace public	

*===============DATA ANALYSIS	
sysuse nlsw88, clear 

tabout race south using table1.tex, replace sum ///
	c(median age r7525 age ) bt font(bold) ///
	f(1c)  npos(tufte)  ptotal(both)  ///
	style(tex) topf(top.tex) botf(bot.tex) ///
	topstr( \caption{test caption})
tabout race south smsa married industry union ///
   collgrad  using table2.tex, replace /// 
	c(col) f( 1p) layout(cb) h3(nil) npos(lab) /// 
	 style(tex) bt font(bold)  h1(HERE IS A SUBTITLE) /// 
	topf(top.tex) botf(bot.tex) topstr( \caption{title goes here})  ///
	botstr(10cm) rotate(60)
graph bar (mean) race south,  ///
	over(collgrad)  bargap(-60) ///
	 blabel(bar, position(inside) format(%9.1f) size(medium) color(white))  ///
	title(Race and Location by College Graduation Status) ///
	legend(off) name(figure1, replace)
graph save "figure1.gph" , replace
gr export  "figure1.$ext", as($ext) replace
win man close graph _all
regress age tenure wage south smsa married union
outreg2 using "estimates1.tex", replace  tex(frag) ///
	 addstat(LR $\chi^2$, e(chi2), Prob , e(p)) 

*===============WRITE LATEX DOC
texdoc init report, replace
tex \documentclass[12pt]{article}

*************************Macros & Packages
tex \usepackage[english]{babel}
tex \usepackage[right=1in,left=1in]{geometry}
tex \usepackage{hyperref, graphicx, blindtext, stata, longtable} 
tex \usepackage{tabularx, booktabs, rotating, float}
**rotate command macro**
tex \newcommand{\rot}[2]{\rule{1em}{0pt}% 
tex \makebox[0cm][c]{\rotatebox{#1}{\ #2}}}

tex \title{Example Article using \LaTeX{} written from Stata via -\texttt{texdoc}-}
tex \author{ Eric A. Booth \\ Texas A\&M University \\ 
tex  \href{}{}  \and 
tex   John Doe \\ Blah University \\ \href{}{} }
tex \date{\today}

tex \begin{document}
tex  \maketitle
tex \abstract{\blindtext}
tex  \tableofcontents
tex \section{Introduction}
tex \blindtext
tex \input{table2}	
tex \section{First Section}
tex \blindtext
tex  \begin{figure}[H]
tex   \centering
tex   \includegraphics[width=6in, height=4in]{figure1}
tex   \caption{\label{myfig1}Here is a caption on a regular figure}
tex   \end{figure}
tex \subsection{subsection here}
tex \blinddescription
tex \pagebreak[4]
tex \input{table1}	
tex \section{Findings}
tex  \Blindtext
tex  \begin{center}
tex  \input{estimates1}
tex  \end{center}
tex \section{Stata Code Example from \texttt{texdoc}}
texdoc stlog codeexample
codebook, c
regress grade union married south
texdoc stlog close
tex \blinddocument
tex \end{document}
texdoc close

if "`c(os)'" == "Windows"{
	cap rm "report.pdf"
*run pdflatex twice for hyperref
	!pdflatex "report.tex"
	!pdflatex "report.tex"
di as smcl `"Click to open directory: {browse `""'}"'
di as smcl `"CLICK TO OPEN: {browse `"report.pdf"'}"'
if "`c(os)'" == "MacOSX" {
cap rm "report.pdf"
	!/usr/texbin/pdflatex  "report.tex"  "report" 
	!/usr/texbin/pdflatex  "report.tex"  "report" 
	!open "report.pdf"
di as smcl `"CLICK TO OPEN: {browse `"report.pdf"'}"'

For this example, I made the part that 'writes' to the .tex file (via texdoc) a separate section at the end, but in practice, I usually interweave it with the code analyzing the data at the top of the do-file.

The example above creates this pdf document:

- Eric
Eric A. Booth
Public Policy Research Institute
Texas A&M University

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index