MathToWeb .com Home|Support
What's New User's Guide Support Download Open Source MathToWeb Online Donate Links
Tips on Usage Browsers

Tips on Usage helpful information on using MathToWeb ...

This section will outline tips, tricks and other useful information on using MathToWeb.
If you would like to publish information on how you use MathToWeb, I would really appreciate hearing from you. Kindly email your article to this address and I will list it in this section.

Contents

•  Using MathToWeb in conjunction with PrinceXML

Publisher: Bryan Henderson (October 2011)
While implementing a new LMS (Learning Management System) called ‘BrainHoney’ by Agilix for BYU Independent Study, we had the need 
to convert LaTeX equations, preferred by the course designers, to MathML, supported by the XHTML to PDF converter tool, 
PrinceXML, that we use to render assignments and exams for paper printing, using the same HTML source used to render those
assessments online in the LMS.  Another desired feature was to be able to do all these conversions in memory, or in other words,
without having to create temporary files on the web server that does the conversions on demand since every assessment created is
unique.

MathToWeb plugged in easily to our site that uses ColdFusion, which is a scripting language built on top of Java.  As we receive the list of
questions to render on the assessment from the LMS, we pass question and choice text through MathToWeb to convert the LaTeX
equations to MathML, before sending the results on to PrinceXML for PDF generation.  Additionally, because MathToWeb works in a
browser, our course designers are able to check to see how their equations will be converted and deal with any problems to ensure
that the Online and paper experiences of our students will be the same.

After dropping the MathToWeb.jar in a location in the CF Server’s JVM classpath a page can create an object:

<cfset MathToWeb = CreateObject("java", "MathToWeb").init("conversion_utility") />

And convert as many equations as needed:

<cfset result = MathToWeb.convertLatexToMathMLUtility(myEq, "-unicode -line -ie UTF8 -rep") />

The command line arguments provided ensure that MathML entities and newlines are kept out of the result, both of which can cause PrinceXML problems.


http://elearn.byu.edu
http://brainhoney.com
http://princexml.com 
http://www.adobe.com/coldfusion


P.S. You may want to add PrinceXML to your list of “browsers” (or HTML rendering engines) capable of rendering MathML.
		

•  Calling the MathToWeb Java utility function, convertLatexToMathMLUtility(..), right from mathtoweb.jar

Publisher: Author (October 2011)
Other keywords: Calling a public method from a Java jar file. Object creation from a jar file.
import java.lang.reflect.*;
import java.awt.*;


// References:
// http://www.javaworld.com/javaworld/javatips/jw-javatip70.html (Java Tip 70: Create objects from jar files!)
// http://www.iam.ubc.ca/guides/javatut99/reflect/object/arg.html (Using Constructors that Have Arguments)   
// http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string

// Test application.
//
// This application demonstrates how the public method convertLatexToMathMLUtility(..), within mathtoweb.jar,
// can be called right from the jar file.
// The supporting classes, MultiClassLoader.java, JarResources.java, and JarClassLoader.java, can be
// copied from the www.javaworld.com link above.
// 
//
// Download mathtoweb.jar at:  http://www.mathtoweb.com/cgi-bin/mathtoweb_download.pl
//
// To run this test application:
// $ java Test mathtoweb.jar MathToWeb
//
public class Test
{
	public static void main(String[] args) throws Exception
    {
    	if (args.length != 2)
    	{
    		// Usage: $ java Test mathtoweb.jar MathToWeb
    		System.err.println("Usage: java Test  ");
    		System.exit (1);
    	}

      	String sJarFileName = args[0];
      	String sClassName = args[1];

      	// Create the jar class loader and use the first argument
     	// passed in from the command line as the jar file to use.

    	JarClassLoader jarLoader = new JarClassLoader(sJarFileName);

    	// Load the class from the jar file and resolve it.
   	Class cMathToWeb = jarLoader.loadClass (sClassName, true);

	// The MathToWeb contructor (when used as a utility) has one argument: the string "conversion_utility".
	// MathToWeb(String s)

	// Note below, I've had to escape the \ . This is necessary only when the
	// input string is being read in from inside a Java program. If the
	// string were brought in from a file, escaping would not be necessary.
	String sLatexInput = "$\\int_a^b f(x) \\, dx$";

	// Any of the comman-line flags can put below, or "" if none are used.
	// At the command-line, type: $ java -jar mathtoweb.jar -help
	String sCommandLineArgs = "-unicode -line -ie UTF8 -rep";

	Class[] clsArrayArgs = new Class[] {String.class};  // getConstructor(..) below takes an array of Class objects.
	Object[] objArrayArgs = new Object[] {new String("conversion_utility")};  // createObject(..) belwo takes an array of Objects.

	Object obj = null;
	Method method = null;

	try
	{
		// Get a Constructor object that reflects the specified public
		// MathToWeb constructor we are interested in.  We want the constructor taking a
		// single String as an argument - this is the one called when using MathToWeb
		// as a conversion utility through calls to the public method convertLatexToMathMLUtility(..).
	  	Constructor constructor = cMathToWeb.getConstructor(clsArrayArgs);

	  	// Get a MathToWeb object - analogous to:
	  	// MathToWeb mtw = new MathToWeb("conversion_utility");
	  	obj = createObject(constructor, objArrayArgs);
	  	//System.out.println("obj: " + obj);

	  	// Get a Method object for the public MathToWeb utility function, convertLatexToMathMLUtility(..).
	  	// This method is invoked in the try block below.
	  	Class[] clsArrayParams = new Class[] {String.class, String.class};
	  	method = obj.getClass().getMethod("convertLatexToMathMLUtility", clsArrayParams);
		//System.out.println("method: " + method);
	}
	catch (NoSuchMethodException e)
	{	System.out.println(e);
	}
	catch(SecurityException e)
	{	System.out.println(e);
	}

	// See: http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string
	try
	{
		// This array of Objects represents the the actual arguments we will feed
		// to the MathToWeb utility function, convertLatexToMathMLUtility(..),
		// when we invoke it (see below).
		Object[] objArrayMethodArgs = new Object[] {sLatexInput, sCommandLineArgs};

		// Invokes the underlying method represented by this Method object, i.e. the method convertLatexToMathMLUtility(..).
		Object objMethodReturn = method.invoke(obj, objArrayMethodArgs);

		// Cast the return object to an array of String objects (there will be 3 of them).
		String[] strArrayReturn = (String[])objMethodReturn;

		// This compiles, let's check the return array:
		System.out.println("strArrayReturn[0]: " + strArrayReturn[0]); // Should be "Success" or "Failure".
		System.out.println("strArrayReturn[1]: " + strArrayReturn[1]); // Conversion log.
		System.out.println("strArrayReturn[2]: " + strArrayReturn[2]); // The result of the conversion - this is the MathML.
	}
	catch (IllegalArgumentException e)
	{	System.out.println(e);
	}
	catch (IllegalAccessException e)
	{	System.out.println(e);
	}
	catch (InvocationTargetException e)
	{	System.out.println(e);
	}


    }  // End: main(..).

    public static Object createObject(Constructor constructor, Object[] arguments)
    {
	//System.out.println ("Constructor: " + constructor.toString());
	Object object = null;

	try
	{
	  object = constructor.newInstance(arguments);
	  //System.out.println ("Object: " + object.toString());
	  return object;
	}
	catch (InstantiationException e)
	{	System.out.println(e);
	}
	catch(IllegalAccessException e)
	{	System.out.println(e);
	}
	catch (IllegalArgumentException e)
	{	System.out.println(e);
	}
	catch (InvocationTargetException e)
	{	System.out.println(e);
	}

	return object;
    }


}   // End: class Test.
	
	
		

•  Using MathToWeb within a DocBook toolchain

Publisher: Nashi Power (September 2010)
I use DocBook to produce large documents with lots of mathematics
and it works very well. It's a little slower to compile than LaTeX
(can take 10 minutes to compile a large document with lots of
equations) and it takes a bit of work up-front to get the toolchain
set-up, but now it's my preferred method. I'm in the habit of working
on and compiling individual sections which is very quick. (You can do
that with DocBook by splitting out sections into individual files and
using xincludes.) Here's how I do it.

 1. Customise the DocBook DTD to allow the inclusion of MathML within
<equation> and <inlineequation> blocks as described in [DocBook XSL:
The Complete Guide][1].

 2. Write standard DocBook, but include AMS LaTeX formatted equations
within <inlineequation>$ax+b$</inlineequation> and

<equation>$\frac{dx}{dt}=-kx$</equation>

blocks.

 3. As part of the toolchain, pass the document through a LaTeX to
MathML converter. I use [MathToWeb][2]. The command would be something
like:

    java -jar mathtoweb.jar -funicode -fline -ns mml -fforce -df
myfile.mml.xml myfile.xml

 4. Run it through the rest of the toolchain like normal. If using
Apache FOP to convert to PDF, the [JEuclid FOP plugin][3] .jar files
have to be placed in the Apache FOP lib directory to enable rendering
of MathML equations. If outputting to HTML, then a tool like
[MathJax][4] can ensure that the MathML gets displayed correctly
across all browsers, not just Firefox.

Hope that helps.

  [1]: http://www.sagehill.net/docbookxsl/MathML.html
  [2]: http://www.mathtoweb.com/cgi-bin/mathtoweb_home.pl
  [3]: http://jeuclid.sourceforge.net/jeuclid-fop/index.html
  [4]: http://www.mathjax.org/