\documentclass{article}
\usepackage{graphicx}
\usepackage{stata}
\begin{document}

\section{Example of a StatWeave document}

\subsection{Some Standard Features}

\subsubsection{Some simple Stata input and output}

Here is a regression example using the obligatory \texttt{auto} dataset, using good physics

\begin{Statacode}
clear
sysuse auto
// making the physics right
gen gp100m = 100/mpg
regress gp100m weight displacement gear_ratio foreign
\end{Statacode}

\subsubsection{Including a result in the body of a sentence.}

This is useful for including results in sentences in a paper.

StatWeave can use inline expansions. As an example, we can see that the 
coefficient for the foreign variable is \Stataexpr{%5.3f _b[foreign]}. 
To put something inline, think of it as being fed to a \texttt{display} command.

\subsubsection{Including a graph.}

Here is an example scatterplot command:
\begin{Statacode}{label=scatter}
twoway (scatter gp100m weight if !foreign) ///
  (scatter gp100m weight if foreign), ///
  legend(order(1 "US" 2 "Non-US"))
\end{Statacode}
Here is the graph.
\begin{Statacode}{echo=FALSE,hide=TRUE}
graph export scatter.eps, replace
\end{Statacode}
\begin{center}
\includegraphics{scatter}
\end{center}
Need to work around two corners a little, but not too awful.

\subsubsection{Hiding commands and showing only results}

It can be useful to hide commands and show just their output. Here, for 
example, is the output from a summarize command:

\begin{Statacode}{echo=FALSE}
summarize gp100m weight displacement gear_ratio
\end{Statacode}

Showing just commands works similarly.

Showing just commands or output is done line by line.

\subsubsection{Splitting input and output.}

StatWeave is special because it can directly split input and output from 
Stata commands. This is worthwhile in teaching documents, for the most part:

This command:
\begin{Statacode}{label=splitEx,saveout}
summarize gp100m
\end{Statacode}
will give summary statistics about the gp100m variable
\recallout{splitEx}

The downside of StatWeave splitting output is that it puts all commands 
before all output for every codeblock. This is very unStata-like.

\begin{Statacode}
summarize weight
regress gp100m weight
\end{Statacode}

This behavior came because StatWeave was originally SASWeave, but was expanded
to handle Stata, SAS, and R.

\subsection{Some other features to check}

\subsubsection{Showing Mata code and output}

Here is some Mata code. 

\begin{Statacode}
mata
X = (76, 53, 48 \ 53, 88, 46 \ 48, 46, 63)
Xi = invsym(X)
\end{Statacode}

It is useful to see the output from checking that Xi is really the inverse of X.

\begin{Statacode}
Xi*X
end
\end{Statacode}

\end{document}
