Eclipse Wiki Weblog

All | General | Java | Eclipse | Groovy | Grails | GWT | Google | MathEclipse | Bliki
20080228 Thursday February 28, 2008

Get LaTeX formula from Mathtran.org as PNG image file

Here's a quick and dirty snippet in Java, how to create a local *.png image file from a LaTeX formula. The image name in the given directory is generated with URLEncoder.encode().

Remember to include these libraries on the Java classpath:

Some questions remain:

Reference:

Last updated 3/01/08 for font size:

package info.bliki.wiki.addon.test.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * Download LaTeX formula from www.mathtran.org as PNG image.
 * 
 */
public class MathtranUtil {
	private final String fMathtranUrl;

	private final String fDirectoryName;

	public static void main(String[] args) {
		String texFormula = "\\sin^2\\theta + \\cos^2\\theta = 1";
		if (args.length > 0) {
			texFormula = args[0];
		}
		MathtranUtil util = new MathtranUtil("http://www.mathtran.org/cgi-bin/mathtran?", "c:/temp/");
		util.downloadImageFile(texFormula, 5);
	}

	/**
	 * Create this utility instance from a given mathtran url and a
	 * <code>diretoryName</code>, where the image file should be saved with the
	 * filename in <code>URLEncoder.encode()</code> format.
	 * 
	 * @param mathtranUrl
	 * @param directoryName
	 */
	public MathtranUtil(String mathtranUrl, String directoryName) {
		super();
		fMathtranUrl = mathtranUrl;
		fDirectoryName = directoryName;
	}

	/**
	 * Download the <code>texFormula</code> from www.mathtran.org as a PNG
	 * image.
	 * 
	 * @param texFormula
	 *          the pure LaTeX formula
	 * @param fontSize
	 *          a number in the range from 1 to 10
	 */
	public void downloadImageFile(String texFormula, int fontSize) {
		FileOutputStream fos = null;
		try {
			HttpClient client = new HttpClient();
			client.setConnectionTimeout(30000);
			String formualEncoded = URLEncoder.encode(texFormula, "UTF-8");
			String fileName = fDirectoryName + formualEncoded + ".png";
			GetMethod method = new GetMethod(fMathtranUrl + "D=" + fontSize + "&tex=" + formualEncoded);
			method.setRequestHeader("accept", "image/png");
			// method.setFollowRedirects(true);

			// Execute the GET method
			int statusCode = client.executeMethod(method);
			if (statusCode != -1) {
				System.out.println("Reading image url: " + fMathtranUrl + " filename: " + fileName);
				byte[] b = method.getResponseBody();
				method.releaseConnection();
				fos = new FileOutputStream(fileName);
				fos.write(b);
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
Posted by axelclk ( Feb 28 2008, 09:00:47 PM CET ) Permalink Comments [2]

Calendar

Links

Search

del.icio.us Tag Cloud

RSS Feeds

Navigation

Referers