Ian Watson's tabout
can produce beautiful frequency tables and tables
of summary statistics, with output in LaTeX or HTML, and as noted in
Chapter 9 of the User's Guide for version 3, it can also produce
dynamic documents.
Here we show how you can combine the power of tabout
with the
simplicity of markstat
to produce dynamic documents, using an
abbreviated version of the example on page 72 and table 34 of the
User's Guide.
Note that we use tabout
to generate table34.html
and then simply
.include table34.html
in the document. We omit the options topf
,
topstr
, botf
and botstr
, using simple Markdown with inline code
instead. Here is the script
tabout1.stmd
```s/
quietly sysuse auto, clear
quietly sum weight
local meanwt = r(mean)
quietly sum length
local meanlen = r(mean)
local obs = r(N)
quietly tabout rep78 foreign using table34.html, replace ///
style(htm) font(italic) c(mean weight) f(0c) sum ///
twidth(9) h1(Car type (mean weight in lbs.)) h3(nil) ///
title(Table 34: Short report example) fn(auto.dta)
```
The title of my short report
----------------------------
This is an excerpt of the report to focus on the dynamic parts.
And now we have an important result: __`s %3.2f `meanwt'`__ is
the average weight of all vehicles. And a second important
result: __`s %3.2f `meanlen'`__ is the average length.
And now we have the first table.
<center>
.include table34.html
</center>
<style>table {display:table; width:auto;}</style>
And this is how the script is rendered by the command markstat using tabout
.
tabout1.html
Note. tabout
generates the table title and footer as separate
paragraphs rather than a caption
and tfoot
. As a result they are not
centered when the table is centered. To keep everything together I
wrapped the table in center tags. I also added a CSS rule at the end
to make sure the table width was automatic.
To obtain LaTeX output you just need to change the output to
using table34.tex
, the style to style(tex)
, and then use
\input{table34.tex}
instead of .include
.
In order to convert the generated LaTeX to PDF, however, we need to add
four packages used by tabout
which are not included in the Pandoc
template. Fortunately this is easy to do with "header includes". This
example uses only the first three packages, but I include all four for
completeness. At the start of the script simply insert this YAML block:
--- header-includes: - \usepackage{multicol} - \usepackage{tabularx} - \usepackage{booktabs} - \usepackage{lscape} ---
The revised script is available here and the resulting pdf file is here.
The same approach can be used with other Stata commands that generate HTML or LaTeX output.