0

I am trying to display MathML in a WebView using MathJax.

When displaying my own MathML this works well:

        w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='"
                + "New <math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
                + "<mi>d</mi><mo>&#x2260;</mo><mn>13</mn>"
                + "</math>'",
                null);
        w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");

However, when I try to display MathML which I get from a widget, the following does not work:

        mlOutput = mlOutput.replaceAll("(\\r|\\n)", ""); //some random trial and error
        mlOutput = mlOutput.replaceAll("'", "\\'");
        mlOutput = mlOutput.replaceAll("\'", "\\\'");
        mlOutput = mlOutput.replaceAll("'", "\\\'");

        w.evaluateJavascript("javascript:document.getElementById('math').innerHTML='"
                        + mlOutput+ "'",
                null);
        w.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");

mlOutput is a String which is printed to Logcat like this (before my string replacing):

<math xmlns='http://www.w3.org/1998/Math/MathML'>
<mfrac>
<mrow>
<mn> 2 </mn>
</mrow>
<mrow>
<mn> 3 </mn>
</mrow>
</mfrac>
</math>

Also I get the following Info from chromium:

I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source:  (1)

Any help on how to appropriately escape the mlOutput MathML-String so that I can use it within Javascript would be highly appreciated!

So far the best I could find was something about the Apache StringEscapeUtils.escapeJavaScript function. But as far as I know this can not be used within Android? I just imported the 500KB .jar library but this was not the solution.

Edit: the solution was some method from this MathJax sample implementation app https://github.com/leathrum/android-apps/tree/master/MathJaxApp

mlOutput = doubleEscape(mlOutput);

private String doubleEscape(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;
}
2
  • What does mlOutput look like after replacement? I think you need to replace the xmlns='http... with xmlns="http... Commented Jan 2, 2016 at 17:38
  • Thank you, the quotes were indeed the problem :) Commented Jan 3, 2016 at 9:06

1 Answer 1

1

the solution was some method from this MathJax sample implementation app https://github.com/leathrum/android-apps/tree/master/MathJaxApp

mlOutput = doubleEscape(mlOutput);

private String doubleEscape(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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.