Display Good-looking Math Formula in Android

前端 未结 6 730
無奈伤痛
無奈伤痛 2020-12-02 11:35

It is sad to see that science and math is never given enough attention in Android platform. Maybe this is an easy problem, but I\'m a total dude in javascript so I\

6条回答
  •  萌比男神i
    2020-12-02 12:04

    Late to the party but I think I have a better solution than Joe's. The original question asked:

    how can I show plain text in paragraph and a formatted equation within the same webview?

    In the original example we had:

    w.loadUrl("javascript:document.getElementById('math').innerHTML='\\\\["
                      +doubleEscapeTeX("sample string")
                      +"\\\\]';");
    

    Do you notice the \\\\[ and \\\\]? Those four backslashes are interpreted by LaTeX as only one, and \[ and \] in LaTeX is a shorthand for \begin{equation}... \end{equation} which produces a block of displayed mathematics. If we want a paragraph of text with some equations within the paragraph we need to get rid of the \\\\[ and use the LaTeX command \( and \). The code for an example with paragraph mathematics and displayed mathematics would be:

    w.loadUrl("javascript:document.getElementById('math').innerHTML='"
       +doubleEscapeTeX(
               "This is a second degree equation \\( \\small ax^2+bx+c=0\\) and its "
              +"roots are \\[x=\\frac{-b\\pm \\sqrt{b^2-4ac}}{2a}. \\]"
        )+"';");
    

    The WebView becomes: enter image description here Notes:

    • We only need two backslashes instead of four, this is because we are inside doubleEscapeTeX() and this function duplicates the backslashes.

    • Also the text outside the math environments is rendered with HTML (inside the tag).

    • The LaTeX font is bigger that the html font, so adding a \\small in each math environment will make them roughly the same size. Here I added the \\small command only to the first equation to show the difference.

提交回复
热议问题