Web Page Example

Look at the WebPageExample.java . (The source for this is available in the full SnuggleTeX distribution if you want to compile this yourself.) This example generalises the previous examples slightly to show you how to create a simple web page output using SnuggleTeX. It starts off in the same way as other examples by creating a SnuggleSession and parsing some simple input:

/* Create vanilla SnuggleEngine and new SnuggleSession */
SnuggleEngine engine = new SnuggleEngine();
SnuggleSession session = engine.createSession();

/* Parse some very basic Math Mode input */
SnuggleInput input = new SnuggleInput("$$ a^2 = b^2 + c^2 $$");

Next, we create a WebPageOutputOptions that is used to configure your web page output to your needs:

/* Create "options" Object to SnuggleTeX what kind of web page we want. We're going
 * to generate one that will work fine with MOZILLA and tweak a few options, just for
 * fun!
WebPageOutputOptions options = WebPageOutputOptionsTemplates.createWebPageOptions(WebPageType.MOZILLA);
options.setTitle("My Web Page");

There are a number of options available: see the API documentation or source for full details. Finally, we ask SnuggleTeX to generate the web page, dumping the resulting output to the console:

/* Now ask SnuggleTeX to write the resulting output to the console.
 * (You would normally send the output somewhere more interesting, though!) */
session.writeWebPage(options, System.out);

You can run this on the command line with:

java -classpath snuggletex-core-n.n.n.jar uk.ac.ed.ph.snuggletex.samples.WebPageExample

When run, you should get an output like:

<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <meta content="application/xhtml+xml; charset=UTF-8" http-equiv="Content-Type"/>
    <meta content="SnuggleTeX" name="Generator"/>
    <title>My Web Page</title>
    <h1>My Web Page</h1>
    <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
        <annotation encoding="SnuggleTeX">$$ a^2 = b^2 + c^2 $$</annotation>