【问题标题】:MathJax not rendering correctlyMathJax 未正确呈现
【发布时间】:2018-06-16 00:13:48
【问题描述】:

我目前在我的应用程序中使用这个库:https://github.com/kexanie/MathView 它用于将文本转换为数学,它使用 TeX/MathJax。

这是按预期工作的:

MathView mv = new MathView(context, null);
        mv.setEngine(MathView.Engine.MATHJAX);
        mv.config("MathJax.Hub.Config({\n" +
                "jax: [\"input/TeX\",\"output/HTML-CSS\"],\n" +
                "displayAlign: \"left\"" +
                "});"
        );
        mv.setText(context.getString(id));

其中“id”指向 strings.xml 文件中的资源。

现在,我将其更改为包含在一个文件中,并从那里获取它。

MathView mv = new MathView(context, null);
        mv.setEngine(MathView.Engine.MATHJAX);
        mv.config("MathJax.Hub.Config({\n" +
                "jax: [\"input/TeX\",\"output/HTML-CSS\"],\n" +
                "displayAlign: \"left\"" +
                "});"
        );
        String a= text;
        mv.setText(a);

从文件中接收文本的位置,但与 strings.xml 中的早期文本相同,我记录下来以进行验证。

但是现在输出坏了。它无法正确识别文本 "\frac{a}{b}" 以前是 "a/b",现在是 "fracab"

这是图书馆的问题吗?

【问题讨论】:

    标签: android mathjax tex


    【解决方案1】:

    我在 angular4 项目中使用过 MathJax。它也可能对您的项目有所帮助。

    为 mathjax 库使用 cdnjs 链接:

     <script type="text/javascript" async 
    src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?
    config=TeX-MML-AM_CHTML"></script>
    

    创建 mathjax 指令:

    import {Directive, ElementRef, Input, OnChanges} from'@angular/core';
    declare var MathJax:any;
    @Directive({
    selector : '[mathText]',
    })
    export class MathTextDirective implements OnChanges {
    constructor(public elementRef: ElementRef) {
        this.hostEl = elementRef.nativeElement; //capture the HTML 
    element host
    }
    
    //Used to bind data: eg: <div [mathText]="raw string">
    @Input('mathText') inputString:string;
    // host element
    private hostEl:HTMLElement;
    
    //have MathJax parse the host element and render the math
    render(){MathJax.Hub.Queue(['Typeset', MathJax.Hub, this.hostEl])}
    
    // called when the inputString changes.
    ngOnChanges(){
        //make the input string into the innerText of the host element
        this.hostEl.innerText = this.inputString;
        this.render();
     }
    }
    

    在 app.module.ts 中注册这个指令 并在 html 中使用此指令,例如:

     <div [mathText]="\frac{a}{b}"></div>
    

    【讨论】:

    • 效果很好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多