Skip to content
Advertisement

Display LaTex in Android

I’m trying to display a LaTex item in android in an ImageView. I’m using jlatexmath library. So far, I’ve got TexIcon object from formula. Now I’d like to display that content in ImageView (or anywhere, just need to display them in activity). One way would be to convert it to bitmap or png then display in ImageView, but I’m not able to convert TexIcon to any other format, here’s my java code so far:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String math = "\frac {V_m} {K_M+S}";
    TeXFormula fomule = new TeXFormula(math);
    TeXIcon ti = fomule.createTeXIcon(TeXConstants.STYLE_DISPLAY, 40);
}

Advertisement

Answer

EDIT : I tried using jlatexMath in Android but with the amount of dependency it requires (such as the awt and swing package from java) to port to Android, I had to leave it halfway.

This is the sample code available from the JLatex repo but BufferedImage and Insets are not available in Android.

    TeXFormula formula = new TeXFormula(latex);
    // Note: Old interface for creating icons:
    //TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
    // Note: New interface using builder pattern (inner class):
    TeXIcon icon = formula.new TeXIconBuilder().setStyle(TeXConstants.STYLE_DISPLAY).setSize(20).build();
    icon.setInsets(new Insets(5, 5, 5, 5));

    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0,0,icon.getIconWidth(),icon.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    icon.paintIcon(jl, g2, 0, 0);
    File file = new File("Example2.png");
    try {
        ImageIO.write(image, "png", file.getAbsoluteFile());
    } catch (IOException ex) {}

I would recommend another library – https://github.com/kexanie/MathView which is a wrapper around webview and you can use either MathJax or KaTex library.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement