Using the stmd command in Stata

Stata provides commands to turn Markdown documents into HTML, Word, or PDF documents. Additionally, there are commands to execute Stata commands embedded within text documents, returning the commands and adding their output to the document - for example, generating a Markdown document.

The stmd adds to this facility by allowing users to write dynamic documents in standard dynamic Markdown style. Code to be executed is enclosed in a code fence with an information tag that includes the language of the dynamic code, and may also include options.

Stata code to be executed is included in a document within a fenced code block like the following:

```{stata, ... }

--- stata commands here ---

```

The stmd command takes a text document, converts the dynamic code to Markdown, and then converts the Markdown to it's final HTML, Word, or PDF format.

Install stmd

In Stata, use

ssc describe stmd

and follow the links to install stmd. This includes stmd2dyn, dyn2do, and a script to install menu items. If you don’t care about the menu items just try

ssc install stmd

Alternatively, use

net from https://www.ssc.wisc.edu/~hemken/Stataworkshops

You can also obtain the source code, including these web pages for extra documentation, from https://github.com/Hemken/stmd .

Process Documents

See help stmd for details.

Basic use to process a document is

stmd example.stmd, saving(example.html) replace

or just

stmd example.stmd

(HTML is the default output.)

The Goal: Easy-to-write source text

Conventional dynamic Markdown looks like the text in the first column, and is turned into a web document (HTML) that appears like the second column.

There are two main parts to this:

Markdown Result
An Example File
===============

Some preliminary text.

A code block, evaluted:
```{stata}
sysuse auto
summarize weight
```

In-line text: the mean weight 
is `{stata} %9.1f r(mean)` pounds.

A list:

- N = `{stata} r(N)`
- mean = `{stata} %9.1f r(mean)`
- sd = `{stata} % 9.2f r(sd)`

A graph:
```{stata}
histogram weight
```

More text.

An Example File

Some preliminary text.

A code block, evaluted:

. sysuse auto
(1978 Automobile Data)

. summarize weight

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      weight |         74    3019.459    777.1936       1760       4840

In-line text: the mean weight is 3019.5 pounds.

A list:

  • N = 74
  • mean = 3019.5
  • sd = 777.19

A graph:

. histogram weight
(bin=8, start=1760, width=385)

More text.