3

I am trying to use MathJax Library in android but certainly not getting success. I want to display some complex mathematical formulas which can be in the middle of text(Like Mathematical derivations) this link is certainly not available anymore 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);

    }
}

I am getting this in LogCat :

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

Thanks in advance

enter image description here

frost
  • 520
  • 1
  • 3
  • 13
  • http://stackoverflow.com/questions/17029780/display-good-looking-math-formula-in-android – Neoh Jul 22 '15 at 03:48
  • I tried the solution given but its still not working – frost Jul 22 '15 at 03:53
  • Do you have MathJax in assets folder? – Neoh Jul 22 '15 at 04:02
  • see the edit.. I have the project structure please tell me if something is wrong – frost Jul 22 '15 at 04:10
  • 1
    Refer to this git repository :https://github.com/leathrum/android-apps/tree/master/MathJaxApp/full. Try to download the assets.zip, there should be a MathJax folder inside. Put it into your assets folder. – Neoh Jul 22 '15 at 04:19
  • This assets.zip is from that only.... so i need to extract and just place MathJax folder in my assests or the entire assests.zip ? – frost Jul 22 '15 at 04:20
  • I noticed you have assets.zip in your folder. That is probably not what you should do. There should be a "MathJax" folder inside your assets directory. – Neoh Jul 22 '15 at 04:21
  • @Neoh I have one more query... I dont have formulas in such a format way my text would be in question answer format and I dont know what is coming from server where I have the formulas placed. So how can I handle this scenario? – frost Jul 22 '15 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84026/discussion-between-chinmay-ghag-and-neoh). – frost Jul 23 '15 at 02:34

1 Answers1

3

I got through the answer after wasting a day so taking some time to write the answer

This is as per the solution given by @Neoh in this link Display Good-looking Math Formula in Android

Note the mistake which I initially made : DO NOT COPY THE ASSESTS.ZIP file in your assets folder instead copy the MathJax folder in the asset folder.

After this you might face the issue which gives ResourceNotFoundException for no reason and you might think that its something in the code that's missing but its not like that.Its a bug in Android buildToolVersion 21.0.1 which I was able to trace it there was one very very useful solution which help me notice this and solve this problem https://stackoverflow.com/a/26493179/4041688

Thanks @Neoh and @Riley C for your solutions and help

Community
  • 1
  • 1
frost
  • 520
  • 1
  • 3
  • 13