Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to render math formulas in my Java GUI application. I am looking for a library made for computer use, not web use, which can render TeX. I have already tried jLatexMath, but it's very slow when I use it in my application.

If there are any other/better, could you point me in the right direction?

(I originally asked this question on StackOverflow)

share|improve this question
I can think of three libraries that might somehow help or inspire you: JEuclid, SnuggleTeX and JLaTeXMath. – Paulo Cereda Jan 19 '12 at 16:42
I am using jLatexMath in one of my own projects, and the first time I got it to display equations I also thought it was very slow, until I realized this was only true for the first invocation of jLatexMath. For every start of your application, the first rendering will be slow but subsequent renderings are fast. – propaganda Jan 20 '12 at 3:33

4 Answers

up vote 8 down vote accepted

Info Friendly note: most of the conversion process involves input parsing, evaluation, font loading, rendering and output, so I wouldn't expect a nearly real-time conversion.

That said, let's go to business. :)

Info Warning: boring non-TeX technical Java stuff ahead. :)

I had a quick look at both SnuggleTeX and JEuclid in order to come up with this answer. Sorry, I didn't have time to come up with a better example.

The first one, SnuggleTeX, is described as "a free and open-source Java library for converting fragments of LaTeX to XML (usually XHTML + MathML)." The second one, JEuclid, is described as "a complete MathML rendering solution". What I actually did was to redirect one's output to the other's input.

First, with SnuggleTeX, you can obtain the needed code from the minimal example in its own homepage:

/* 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("$$ x+2=3 $$");
session.parseInput(input);

/* Convert the results to an XML String, which in this case will
 * be a single MathML <math>...</math> element. */
String xmlString = session.buildXMLString();

Now you have the MathML representation of your LaTeX input. Let's check JEuclid, from its API, there's the Converter class with the following method:

BufferedImage render(Node node, LayoutContext context) 

Then you can use net.sourceforge.jeuclid.MathMLParserSupport to parse your XML string to org.w3c.dom.Document. Calling the render method with the correct parameters will give you a BufferedImage representing your input.

My attempt:

My code

It took around 1.4 secs to render this image.

I didn't like the ouput, but to be honest, I just wrote this app in 2 minutes as a [cough... cough... bad...] proof of concept. :) I'm almost sure the rendering quality can be improved, but I'm quite busy ATM. Anyway, I think you can try something similar and then decide if this approach is worth a shot. :)

Update: It seems JEuclid has a JMathComponent in order to display MathML content in a Component.

share|improve this answer
That's great! Thanks for your time :) – Hidde Jan 20 '12 at 16:03

Using Java's Runtime class you can execute external processes, so you can easily generate some LaTeX input file that contains the LaTeX expressions, execute pdflatex on the input file, possibly use Runrime to convert the pdf output to a different format (with gs) and open the resulting picture.

If you know Java this should be easy to implement. I don't have time to do this myself, but I'm confident this shouldn't take more than an hour or so.

share|improve this answer

I know very little about Java programming, but I am pretty sure that there are free applications that use JLaTeXMath to render LaTeX that you can try and look at. I have pretty good experience with Geogebra. The rendering in Geogebra is pretty fast (generally does not seem to slow the application down in any way), and the output is rather nice, I think. It is a free software, so you can download the source code and see how they do it.

share|improve this answer

Have you seen JavaTeX? If that doesn't solve your problem, can you describe the features you're looking for and other constraints?

share|improve this answer
This is a program, but I am looking for a Java library. I want to give a String, and receive anything I can draw on a JPanel or any other JComponent, preferably a BufferedImage. (This is why I asked this question on SO, they probably know a lot more about this kind of stuff.) – Hidde Jan 19 '12 at 16:40
1  
The source is available on sourceforge. You should be able to extract the functionality to a library. – recluze Jan 19 '12 at 16:42
JavaTeX is NTS combined with a dvi viewer. That's nice, but also a 10 year old dead end. – Martin Schröder Jan 19 '12 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.