【问题标题】:MathJax Not working in androidMathJax 在 android 中不起作用
【发布时间】:2015-07-22 03:09:46
【问题描述】:

我正在尝试在 android 中使用 MathJax 库,但肯定没有成功。 我想显示一些可以在文本中间的复杂数学公式(如数学推导) 此链接肯定不再可用 http://cs.jsu.edu/wordpress/?p=498

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    WebView w;

    private String doubleEscapeTeX(String s) {
        String t = "";
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '\'')
                t += '\\';
            if (s.charAt(i) != '\n')
                t += s.charAt(i);
            if (s.charAt(i) == '\\')
                t += "\\";
        }
        return t;
    }

    private int exampleIndex = 0;

    private String getExample(int index) {
        return getResources().getStringArray(R.array.tex_examples)[index];
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        w = (WebView) findViewById(R.id.webview);
        w.getSettings().setJavaScriptEnabled(true);
        w.getSettings().setBuiltInZoomControls(true);
        w.loadDataWithBaseURL("http://bar",
                "<script type='text/x-mathjax-config'>"
                        + "MathJax.Hub.Config({ " + "showMathMenu: false, "
                        + "jax: ['input/TeX','output/HTML-CSS'], "
                        + "extensions: ['tex2jax.js'], "
                        + "TeX: { extensions: ['AMSmath.js','AMSsymbols.js',"
                        + "'noErrors.js','noUndefined.js'] } " + "});</script>"
                        + "<script type='text/javascript' "
                        + "src='file:///android_asset/MathJax/MathJax.js'"
                        + "></script><span id='math'></span>", "text/html",
                "utf-8", "");

        // Here is my added code.
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                String s = getExample(exampleIndex++);
                if (exampleIndex > getResources().getStringArray(
                        R.array.tex_examples).length - 1)
                    exampleIndex = 0;
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
                        + doubleEscapeTeX("\\int_{-\\infty}^{\\infty} e^{-x^2}\\, dx = \\sqrt{\\pi}"+ "\\\\]';"));
                w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");

            }
        }, 1000);

    }
}

我在 LogCat 中得到这个:

[INFO:CONSOLE(1)] "Uncaught ReferenceError: MathJax is not defined", source:  (1)

提前致谢

【问题讨论】:

标签: android mathjax


【解决方案1】:

浪费了一天的时间,终于写完了答案

这是根据@Neoh在此链接Display Good-looking Math Formula in Android中给出的解决方案@

请注意我最初犯的错误:不要复制资产文件夹中的 ASSESTS.ZIP 文件,而是复制资产文件夹中的 MathJax 文件夹。

在此之后,您可能会遇到无缘无故给出 ResourceNotFoundException 的问题,您可能会认为代码中缺少某些东西,但并非如此。它是 Android buildToolVersion 21.0.1 中的一个错误,我能够对其进行跟踪有一个非常有用的解决方案可以帮助我注意到这一点并解决这个问题 https://stackoverflow.com/a/26493179/4041688

感谢@Neoh 和@Riley C 提供的解决方案和帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2017-04-24
    • 2021-06-15
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多