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 [3]

Trackback URL: http://www.groovy-news.org/e/trackback/axelclk/Weblog/get_latex_formula_image_from
Linkbacks:
Comments:

Thank you, Alex, for writing this simple MathTran client.

To change the size of the image, use the D=<n> in the query string, where <n> ranges from 1 to 10.

The MathTran service has funding to August 2008, and is I expect that funding for 2009 will also be found.

Of course, the more useful and used it is, the easier it is to find funds.

Posted by Jonathan Fine (87.113.69.243) on February 28, 2008 at 10:52 PM CET
Website: http://jonathanfine.wordpress.com #

Thanks for the font size hint. I've changed the above code snippet.

Posted by Axel Kramer (87.181.228.123) on March 02, 2008 at 12:10 PM CET
Website: http://www.groovy-news.org/e/page/axelclk #

Good post!As i was passing by here and i read your post.
It's quite interesting.I will look around for more such post.Thanks for sharing.

Posted by nike outlet (117.27.81.141) on January 18, 2010 at 07:01 AM CET
Website: http://www.nba-sneakers.com #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: Allowed

Calendar

Links

Search

del.icio.us Tag Cloud

RSS Feeds

Navigation

Referers