# Incorporating Stata into reproducible documents
## [Hua Peng@StataCorp][hpeng]
### 2017 Stata Conference in Baltimore
# Why reproducible documents?
Eliminate manual steps such as hand-editing documents
- mix formatted text and Stata output
- include inline Stata results
- embed Stata graphs
- produce tables containing the output from selected Stata commands
# Commands in Stata 15
- dyndoc - convert dynamic Markdown documents to web pages
- putdocx - create Word documents
- putpdf - create PDF files
# A dyndoc example
Produce [a blog article](./example.html) from a [dynamic Markdown document](./example.md)
# Quick review of dynamic tags
## dd_do for a block of Stata code
````
<>
<>
sysuse auto
regress weight displacement
<>
<>
````
##
````
<>
sysuse auto
regress weight displacement
<>
````
##
Attributes change a tag's behavior
````
<>
<>
matrix define eb = e(b)
<>
<>
````
<>
matrix define eb = e(b)
<>
## dd_display for inline Stata results
<>
- For every unit increase in displacement, a <> unit
increase in weight is predicted.
<>
> - For every unit increase in displacement, a <> unit
increase in weight is predicted.
## dd_graph
````
<>
<>
scatter weight displacement, mcolor(%30)
<>
<>
````
<>
scatter weight displacement, mcolor(%30)
<>
````
<>
<>
<>
````
##
#### <>
# putdocx
Produce a [.docx document](./exdocx.docx) from a [do-file](./exdocx.do)
- full range of control for text formatting
- flexible table output
# Output tables in Markdown
Produce [Markdown tables](./table.html) from [commands](./table.md), including
- all estimation commands using **\_coef_table**
- **table**
- **estimates table**
# dyntext
Process dynamic tags in any text files, for example, LaTeX and JavaScript
# LaTeX file
Produce a [.pdf file](./extex.pdf) from a [LaTeX file](./extex.md) with
dynamic tags. It requires **pdflatex** and Stata command **sjlog**
~~~~
dyntext extex.md, sav(extex.tex) replace
!pdflatex extex.tex
~~~~
# JavaScript
- One [example](./gchart.html) is using Google Chart library and the [dynamic document](./gchart.txt)
- Another [example](./gchart1.txt) is dynamically generating web pages based
on arguments
The following code generates a scatter plot between **mpg** and **weight**
~~~~
sysuse auto, clear
dyndoc gchart1.txt mpg weight, replace
~~~~
# Use pandoc instead of Stata's markdown
Produce reveal.js [slildes deck](./birds17.md) use **pandoc**.
It requires **pandoc** and user written Stata commands
[**dynpandoc**](./dynpandoc.ado) and [**pandocmarkdown**](./pandocmarkdown.ado).
~~~~
dynpandoc birds17.md, sav(birds17.html) replace to(revealjs) ///
pargs(-s --template=revealjs.html --standalone ///
--section-divs --variable theme="stata")
~~~~
# putpdf
Produce a [.pdf document](./expdf.pdf) from a [do-file](./expdf.do)
# TODO
- cached code block
- better LaTeX support
- Markdown support in the do-file editor
# THE END
[hpeng]: hpeng@stata.com