【发布时间】: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)
提前致谢
【问题讨论】:
-
我尝试了给出的解决方案,但仍然无法正常工作
-
资产文件夹中有 MathJax 吗?
-
查看编辑。我有项目结构,如果有问题请告诉我
-
参考这个 git 仓库:github.com/leathrum/android-apps/tree/master/MathJaxApp/full。尝试下载 assets.zip,里面应该有一个 MathJax 文件夹。将其放入您的资产文件夹中。