MathView Android Library

repository·master·Indexed 21 days ago

https://github.com/jianzhongli/mathview

An Android library that extends WebView to render TeX or MathML code into mathematical formulas. It supports both MathJax and KaTeX rendering engines, allowing developers to define formulas in XML layouts or set them programmatically via Java.

Tokens
994
Snippets
4
Records
6
Agent score
27%

What's inside MathView

  1. Compare MathJax and KaTeX engines

    master

    MathView supports two rendering engines:

    • KaTeX: Faster in mobile environments, but supports fewer TeX commands.
    • MathJax: Supports more features and is more visually polished, but is slower than KaTeX.

    When using MathView.getText(), the returned value is the raw TeX/MathML string.

  2. Install MathView from a local .aar file

    master

    If you prefer using a local .aar file:

    1. Download the latest version from Bintray.
    2. In Android Studio, go to File -> New -> New Module -> Import .JAR/.AAR Package and select your file.
    3. Add the module dependency via File -> Project Structure -> Dependencies, click the plus icon, and select 3. Module Dependency.
  3. Install MathView via Maven

    master

    Add the following dependency to the dependencies section of your module's build.gradle file to install MathView from the jcenter remote repository.

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:23.0.0'
        compile 'io.github.kexanie.library:MathView:0.0.6'
    }
  4. Set MathView text programmatically in Java

    master

    To update the formula via code, find the view by its ID and call setText(). Note that when using Java strings, you must escape special characters like backslashes.

    MathView formula_two;
    String tex = "This come from string. You can insert inline formula:" +
            " \\(ax^2 + bx + c = 0\\) " +
            "or displayed formula: $$\sum_{i=0}^n i^2 = \\frac{(n^2+n)(2n+1)}{6}$$";
    
    @Override
    protected void onResume() {
        super.onResume();
        formula_two = (MathView) findViewById(R.id.formula_two);
        formula_two.setText(tex);
    }
  5. Define MathView in XML layout

    master

    You can include MathView directly in your XML layout files. Use the auto:engine attribute to choose between MathJax and KaTeX.

    Important Usage Notes:

    • Use \( ... \) for inline formulas instead of $ ... $.
    • If you want the view to support wrap_content height, wrap the MathView inside a NestedScrollView.
    <io.github.kexanie.library.MathView
        android:id="@+id/formula_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        auto:text="When \\(a \\ne 0\\), there are two solutions to \\(ax^2 + bx + c = 0\\)
        and they are $$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.$$"
        auto:engine="MathJax">
    </io.github.kexanie.library.MathView>
  6. Configure MathJax settings

    master

    For advanced MathJax customization (such as enabling automatic linebreaking), use the MathView.config() method. This must be called before calling setText().

    MathView.config("MathJax.Hub.Config({\n" +
                "  CommonHTML: { linebreaks: { automatic: true } },\n" +
                "  \"HTML-CSS\": { linebreaks: { automatic: true } },\n" +
                "         SVG: { linebreaks: { automatic: true } }\n" +
                "});;");