Skip to content

built in syntax highlighter

E. F. Haghish edited this page Dec 17, 2018 · 4 revisions

Wish to develop handouts or presentation slides within Stata? MarkDoc has a built-in syntax highlighter for Stata code in HTML, LaTeX, and PDF formats, including presentation slides! You can also add the syntax highlighter to your blog! Read how...

Much effort was put to develop a syntax highlighter for Stata, which was not available before. This just shows how caring MarkDoc is, when it comes to teaching statistics! MarkDoc was intended to be used in daily routine, as early as introductory statistics courses to help students document their code and read it with more care.

How to add statax to your Document

statax is available for documents in the following formats:

  • HTML
  • LaTeX
  • PDF

To add the syntax highlighter just add the statax option. For example:

markdoc dofile.do, export(html) statax 

Alternatively, if you are using the dialog boxes, make sure that the statax syntax highlighting option is checked. The option is also available in the MarkDoc mini mode, which is independent of third-party software. to launch the dialog boxes type:

db markdoc
db mini

More about statax

Description

statax can acurately distinguish several different elements in Stata syntax such as:

  1. commands
  2. functions
  3. local and global macros
  4. strings
  5. numbers
  6. operators
  7. comments
  8. curly brackets

The JavaScript engine is hosted on my website and can be easily added to any webpage. See Statax Homepage for details. in brief, all you have to do to load the javaScript in your HTML document is adding the following code:

<script type="text/javascript" src='http://haghish.com/statax/Statax.js'></script>

And writing your Stata code in:

<pre class="sh_stata">
	. Stata code
	. Stata code
	...
</pre>

The LaTeX syntax highlighter is even easier to implement. All you need to do is loading the Statax.tex in the header of your latex document, before you \begin{document} as shown below:

\input{Statax}

Once the Statax file is loaded in the header, You can call the statax environment to highlight the Stata syntax. For example:

\begin{statax}
	
	// JavaScript Syntax Highlighter for Stata
	
	quietly erase "`This' $Example"
	noisily do `Something'
	forvalues num = 5/13 {
	count if vari`num' > 10 //this is a comment
	scalar 5 = 2 * 10 
	generate x = runiform()
	}
	
	/*
	E. F. Haghish
	Center for Medical Biometry and Medical Informatics
	Unersity of Freiburg, Germany
	*/

\end{statax}

Customization

statax can be easily customized. To do that, you need to include a theme file (Tex or CSS) to redefine the default syntax highlighter. The package includes three additional themes to provide examples of theme customization.

HTML documents

To customize the HTML document, source the CSS after sourcing the JavaScript file in the header. For example, you can add the Sunset.css theme from the web as follows:

Sunset theme

<script type="text/javascript" src='http://haghish.com/statax/Statax.js'></script>
<link rel="stylesheet" type="text/css" href="http://haghish.com/statax/CSS/Sunset.css">

Daring theme

<script type="text/javascript" src='http://haghish.com/statax/Statax.js'></script>
<link rel="stylesheet" type="text/css" href="http://haghish.com/statax/CSS/Daring.css">

Wrangler theme

<script type="text/javascript" src='http://haghish.com/statax/Statax.js'></script>
<link rel="stylesheet" type="text/css" href="http://haghish.com/statax/CSS/Wrangler.css">

Complete example files

A minimalistic LaTeX file is shown below with a code section that highlights Stata syntax:

\documentclass[12pt]{article}
\include{Statax}
\begin{document}
  \section{Statax Syntax Highlighter}
  \begin{statax}
    // Stata code or comment ...
  \end{statax}
\end{document}

and the code below shows a simple HTML file that can highlight Stata code:

<html>
  <head>
    <script src='http://haghish.com/statax/Statax.js'></script>
  </head>
  <body>
    <pre class="sh_stata" >
      // place Stata code or commnents here ...
    </pre>
  </body>
</html>