I want to build a android application based on jet pack compose which can display math equations or latex.
I have checked internet and found many libraries but all are for xml and not for compose and also they are outdated and do not work for xml even. I am trying to find a library which work for compose or at the worst case for xml and need bare minimum code snippets for gradle, main activity Kotlin, compose (or xml) which works.
Using KaTeX and WebViews, you need to create a file
main/assets/latex_render.html, with the following content (most of it taken from KaTeX docs):here, you wait for the
addBodyfunction to execute and then the LaTeX expression will be displayed.The styling applied to the
bodytag is mostly for page arrangement, with the exception ofbackground-color: transparent, which I've found to be needed to stop the flickering of the WebView when it's first created.Now, for the Android Compose part, you will need to add the following dependency to your
build.gradle:Then, you can create a wrapper component over the WebView, something simple like this should suffice:
(inspired by the response to this KaTeXView issue, which unfortunately didn't work for me as-is, but it was close enough)
You should be able now to just render your latex like this:
And it should render something like this (minus the gradient background):