Java实现Latex转MathML,MathML转Latex

Java实现Latex转MathML,MathML转Latex,直接上代码

public class LatexUtil {
    public static String LatexToMathML(String inStr) {
        if(inStr==null||"".equals(inStr.trim())){
            return "";
        }
        //String test="$\\sin(\\alpha)^{\\theta}=\\sum_{i=0}^{n}(x^i + \\cos(f))$";
        String mathml = ConvertFromLatexToMathML.convertToMathML(inStr);
        return mathml;
    }

    public static String MathMLToLatex(String inStr) {
        if(inStr==null||"".equals(inStr.trim())){
            return "";
        }
        String latex = ConvertFromMathMLToLatex.convertToLatex(inStr);
        return latex;
    }
    public static void main(String[] args){
        String latex="$\\sin(\\alpha)^{\\theta}=\\sum_{i=0}^{n}(x^i + \\cos(f))$";
        String mml=LatexUtil.LatexToMathML(latex);
        String latex2=LatexUtil.MathMLToLatex(mml);
        System.out.println(mml);
        System.out.println(latex2);
    }

}

运行效果:


<math mathvariant='italic' display='inline'>
    <mtext mathvariant='normal'>sin</mtext>
    <mo maxsize='1'>(</mo>
    <mtext>&alpha;</mtext>
    <msup>
        <mo maxsize='1'>)</mo>
        <mtext>&theta;</mtext>
    </msup>
    <mo>=</mo>
    <munderover>
        <mo>&sum;</mo>
        <mrow>
            <mi>i</mi>
            <mo>=</mo>
            <mn>0</mn>
        </mrow>
        <mi>n</mi>
    </munderover>
    <mo maxsize='1'>(</mo>
    <msup>
        <mi>x</mi>
        <mi>i</mi>
    </msup>
    <mo>+</mo>
    <mtext mathvariant='normal'>cos</mtext>
    <mo maxsize='1'>(</mo>
    <mi>f</mi>
    <mo maxsize='1'>)</mo>
    <mo maxsize='1'>)</mo>
</math>

$\sin(\alpha )^\theta = \sum^n_{i = 0} ( x^i + \cos(f))$

springboot完整项目下载地址http://www.zrscsoft.com/sitepic/12073.html 

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/105843664