Displaying Stata code in a blog post

Set the front matter variable has-code: true to load highlight.js (for syntax highlighting).

Within paragraphs, use backticks (`command`) or code tags (<code>command</code>) to mention commands such as regress or ivreg2.

Embed code blocks or Stata output with triple backticks:


which looks like this:

. sysuse auto
no; data in memory would be lost

Display compact code blocks with the “```output” class. This uses a smaller font and is useful for displaying large blocks of Stata output:

. sysuse auto
no; data in memory would be lost

Classes can also highlight code from Stata and many other languages:

program define gr_log
version 6.0

local or = `2'
local xunits = `3'
local b1 = ln(`or')

* make summary of logistic data from equation
set obs `xunits'
generate pgty = 1 - 1/(1 + exp(score))
 * Comment 1
reg y x

Python code looks like this:

def somefunc(param1='', param2=0):
    r'''A docstring'''
    if param1 > param2: # interesting
        print 'Gre\'ater'
    return (param2 - param1 + 1) or None

class SomeClass:

>>> message = '''interpreter
... prompt'''

Finally, for elegant but elaborate results you can combine the pre, code, kbd and samp tags (don’t do it by hand!):

. cls
. reghdfe price weight, a(turn trunk)
(dropped 9 singleton observations)
(converged in 12 iterations)

HDFE Linear regression                            Number of obs   =         65
Absorbing 2 HDFE groups                           F(   1,     39) =      25.39
                                                  Prob > F        =     0.0000
                                                  R-squared       =     0.6347
                                                  Adj R-squared   =     0.4005
                                                  Within R-sq.    =     0.3943
                                                  Root MSE        =  2242.7172

       price │      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      weight │   5.055336   1.003362     5.04   0.000     3.025846    7.084826
    Absorbed │         F(24, 39) =      1.794   0.049             (Joint test)

Absorbed degrees of freedom:
 Absorbed FE │  Num. Coefs.  =   Categories  -   Redundant     │
        turn │           13              13              0     │ 
       trunk │           12              13              1     │ 

A starting point could be:

<pre class="output"><code class="language-output">. cls
<kbd>. count if foreign</kbd>
<samp>  22</samp>

TLDR: Single backticks within paragraphs. Triple backticks for output (output class) and code (stata class + front matter). Resort to low-level HTML elements for custom solutions.

Appendix: Technical Details

HTML handles code elements in both inline and block modes, and the elements can be nested.

Inline code

The existing code-like tags are:

for general code excerpts: foreach item in list
for keyboard or command line input: Ctrl+Enter, sudo apt-get update
for variable names: price weight length
for program output: SyntaxError: invalid syntax


As above, but surround the above tags with the pre tag:


foreach item in list


sudo apt-get update


price weight length


variable id does not uniquely identify observations in the master data

Tag Inception

You can also nest tags, so a message like “syntax error, option gen(var) is required” could be input as

<samp>syntax error, option <code>gen(<var>var</var>)</code> is required</samp>