2015-07-22 37 views
1

我想在android中使用MathJax庫,但肯定沒有獲得成功。 我想顯示一些複雜的數學公式,可以在文本中間(像數學推導) 這個環節肯定是不再可用 http://cs.jsu.edu/wordpress/?p=498MathJax不工作在android

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) 

在此先感謝

enter image description here

+0

http://stackoverflow.com/questions/17029780/顯示好看的數學公式-Android – Neoh

+0

我嘗試過給出的解決方案,但它仍然不工作 – frost

+0

你在資產文件夾中有MathJax? – Neoh

回答

2

我通過答題獲得浪費了一天,所以需要一些時間來寫答案

這是每個在這個環節Display Good-looking Math Formula in Android

注意,我最初犯的錯用@Neoh給出的解決方案後:DO不要複製資產文件夾中的ASSESTS.ZIP文件,而是將MathJax文件夾複製到資產文件夾中。

在此之後,您可能會面臨無故發生ResourceNotFoundException的問題,並且您可能會認爲它的代碼中的某些內容已丟失,但不是那樣。它是Android buildToolVersion 21.0.1中的一個bug,它有一個非常非常有用的解決方案,它幫助我注意到這一點,並解決這個問題 https://stackoverflow.com/a/26493179/4041688

感謝@Neoh和@Riley下您的解決方案,並幫助