<> # Writing Dynamic Stata Code Blocks When you write a document in the standard dynamic Markdown format, it is processed in two stages. First the Stata code blocks and in-line references are processed, and the results are substituted into your document. Then the Markdown elements of your document are processed, producing a final HTML document. ## Specify the Stata language The standard approach to including dynamic content in a Markdown document is to place some code-to-be-processed in a *fenced code block* (demarcated with three or more backticks or tildes). This fenced code block has an additional *information tag*, the first word of which is reserved for the name of the programming language to be invoked. This usually looks like ```` ~~~stata -- code -- ~~~ ```` Similarly, dynamic *in-line code* (demarcated with single backticks) has an additional info tag for the programming language. It typically looks like ```` some text `stata --code--` more text ```` In Stata Markdown, the in-line code is limited to *display* commands (not arbitrary Stata commands). The `stmd` command only processes code in Stata (this could change in the future, to include Mata or other languages). The language for each code block may be specified in a number of alternative ways. - stata - {stata} - s - {s} That is, you may use the keyword \"stata\" or the keyword \"s\", with or without curly braces. The first option, the simple keyword \"stata\", gives you HTML with a `class="language-stata"` declaration, useful if your documents make use of style sheets. The \"{stata}\" with braces, is compatible with `knitr` documents in R. The last two options, \"{s}\" and \"s\" are compatible with `markstat` documents. ## Examples ### Code Blocks
Markdown Resulting Document
Using \"{stata}\" ~~~~ ```{stata} sysuse auto, clear tabstat weight, stat(mean sd) ``` ~~~~ ```{stata} sysuse auto, clear tabstat weight, stat(mean sd) ```
Using \"{s}\" ~~~~ ```{s} tabstat price, stat(mean sd) ``` ~~~~ ```{s} tabstat price, stat(mean sd) ```
Using \"stata\" (no braces) ~~~~ ```stata tabstat mpg, stat(mean sd) ``` ~~~~ ```stata tabstat mpg, stat(mean sd) ```
Using \"s\" (no braces) ~~~~ ```s tabstat disp, stat(mean sd) ``` ~~~~ ```s tabstat disp, stat(mean sd) ```
### In-line Code In-line dynamic code begins with a backtick followed by a language keyword, followed by a space, followed by a Stata `display` directive (a `display` command without the keyword "display").
Markdown Resulting Document
Using \"{stata}\" in-line ~~~~ ```{stata, quietly} summarize price ``` Mean price `{stata} %9.1f r(mean)` ~~~~ ```{stata, quietly} summarize price ``` Mean price: `{stata} %9.1f r(mean)`
Using \"{s}\" in-line ~~~~ Standard deviation of price: `{s} %9.1f r(sd)` ~~~~ Standard deviation of price: `{s} %9.1f r(sd)`
Using \"stata\" (no braces) ~~~~ Minimum price: `stata %9.1f r(min)` ~~~~ Minimum price: `stata %9.1f r(min)`
Using \"s\" (no braces) ~~~~ Maximum price: `stata %9.1f r(max)` ~~~~ Maximum price: `stata %9.1f r(max)`
<>