Full LaTeX parser in Java [closed]
Asked Answered
S

2

7

I've written small Java application to create printable flashcards for my Maths revision.

At the moment, I'm using JLaTeXMath to generate the images for each side from LaTeX.

The only problem is, that JLaTeXMath seems to be limited to LaTeX formula. I want to use the same program to create flashcards for other subjects such as Biology, where the questions and answers will be text based (rather than equation based) and LaTeX formula's aren't suitable for this.

Are there any Java libraries that can parse LaTeX? Or is there a better way of doing this?

Strew answered 8/12, 2012 at 12:43 Comment(1)
LaTeX is a huge library, and I bet you can call it from the command line (also using Java).Rimple
B
7

LaTeX is a full programming language. Parsing it means executing the program.

While it seems to be simple in many of the common cases - \section etc. - it is by far not trivial. In fact, it should be turing complete. And some parts will even have a more or less different syntax. Take TIKZ for example - an excellent graph drawing library for LaTeX. It's syntax is somewhat like latex, but other parts are more that of modern programming languages. And a lot is like stylesheets.

However, you might be able to get away with supporting just part of the latex syntax. Have a look at what Texlipse does. It's in Java.

Breechloader answered 8/12, 2012 at 12:54 Comment(3)
I see, so maybe I had better write my app in LATEX... Is it possible to read/write files from LATEX. Or I could use Java to create .tex files and then run them to create the output. Which is best do you think?Strew
There is nothing wrong with using latex as an output-only format and then using pdflatex to produce PDF files. There must be thousands of tools that do this.Breechloader
I did wonder if somebody would correct me! Thanks for the edit!Strew
B
3

The heading said parsing and google lead me to here. Thus, I also include an example of JLaTeXMath and hints on other parsers.

SnuggleTeX

SnuggleTeX BSD License - has a good parser, too.

        /* 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();

Source: https://github.com/davemckain/snuggletex/blob/development_1_2_x/snuggletex-core/src/main/java/uk/ac/ed/ph/snuggletex/samples/MinimalExample.java.

It can even do \newcommand:

\newcommand{\test}[1]{Hello #1}\test{There} more text afterwards
Hello There more text afterwards

Source: https://github.com/davemckain/snuggletex/blob/development_1_2_x/snuggletex-core/src/test/resources/line-tests.txt

JLaTeXMath

I would use JLaTeXMath if GPL license is OK:

"JLaTeXMath is the best Java library to display LaTeX code."


import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;

public class Example5 {

    public static void main(String[] args) {

        String latex = "\\begin{array}{|c|l|||r|c|}";
        latex += "\\hline";
        latex += "\\text{Matrix}&\\multicolumn{2}{|c|}{\\text{Multicolumns}}&\\text{Font sizes commands}\\cr";
        latex += "\\hline";
        latex += "\\begin{pmatrix}\\alpha_{11}&\\cdots&\\alpha_{1n}\\cr\\hdotsfor{3}\\cr\\alpha_{n1}&\\cdots&\\alpha_{nn}\\end{pmatrix}&\\Large \\text{Large Right}&\\small \\text{small Left}&\\tiny \\text{tiny Tiny}\\cr";
        latex += "\\hline";
        latex += "\\multicolumn{4}{|c|}{\\Huge \\text{Huge Multicolumns}}\\cr";
        latex += "\\hline";
        latex += "\\end{array}";

        TeXFormula formula = new TeXFormula(latex);
        formula.createPNG(TeXConstants.STYLE_DISPLAY, 20, "target/Example5.png", Color.white, Color.black);
    }
}

Source for TeXFormula: https://github.com/opencollab/jlatexmath/blob/7995ce52b2699c9a3a8428a94c1f3762cdcb0284/jlatexmath/src/main/java/org/scilab/forge/jlatexmath/TeXFormula.java#L244

Other solutions

(partially based on https://tex.stackexchange.com/q/41609/9075)

Blakey answered 2/4, 2020 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.