6

I was wondering if there is an easy way to implement the rendering of math equations into an iOS app. It doesn't have to be interactive. All it should do is display the equation in traditional ways so when the user is dealing with complex equations it makes it easier to follow.

ex. 2^(2/(6^(1/2))) gives: Example output

Wolfram Alpha does this along with other apps that I have seen. It doesn't have to deal with variables either.

Neil
  • 2,004
  • 3
  • 23
  • 48
  • 1
    Have a look at this: http://stackoverflow.com/questions/2907045/drawing-formulas-with-quartz-2d – Tom Irving Apr 14 '13 at 17:59
  • @Tom, SirKaydian wants an _easy_ way, and drawing formulae with Quartz2D is, as Brad answers, "not a trivial undertaking".. – foundry Apr 14 '13 at 18:49
  • 1
    @RichardJ.RossIII, I wouldn't regard this as a duplicate, your linked question was asked in 2010 and self-answered in 2011, before the advent of MathML on iOS5 – foundry Apr 14 '13 at 18:51
  • 1
    Have a look at https://github.com/kostub/iosMath – Kostub Deshmukh May 13 '16 at 19:09

2 Answers2

4

A UIWebView can display mathML since iOS5. Your example:

<math title="2^(2/(sqrt(6))" xmlns="http://www.w3.org/1998/Math/MathML">
  <mstyle mathcolor="blue" fontfamily="sanserif" displaystyle="true">
    <msup>
      <mn>2</mn>
      <mrow>
        <mfrac>
          <mn>2</mn>
          <mrow>
            <msqrt>
              <mrow>
                <mn>6</mn>
              </mrow>
            </msqrt>
          </mrow>
        </mfrac>
      </mrow>
    </msup>
  </mstyle>
</math>

This will render in a UIWebView on iOS.

There are javascript libraries that can convert for you - eg mathjax can accept ASCIIMath input such as this example 2^(2/(sqrt(6))

There is a good discussion of these issues here Tradeoff between LaTex, MathML, and XHTMLMathML in an iOS app?

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • But there's a step missing here - you will now need a library to convert a 'simple' equation from `2^(2/(6^(1/2)))` to it's MathML equivalent. – Richard J. Ross III Apr 14 '13 at 18:19
  • @RichardJ.RossIII I only changed 6^(1/2) to sqrt(6)- the former will render "correctly" putting "0.5" into 6's superscript. That's a human authoring decision as much as anything (and maybe mathjax would render that with a sqrt symbol anyway, i didn't try)... – foundry Apr 14 '13 at 18:22
1

For simple expressions, the easiest way is probably to use HTML and display the expression in a UIWebView. This will work especially well if the expression part of other content that can also be displayed using HTML.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    The difficulty with this is that JavaScript solutions can be slow on older devices, and UIWebViews generally aren't known for their good memory footprints. These limitations have gone down with newer devices, but I still wouldn't recommend going crazy with the UIWebViews. – Richard J. Ross III Apr 14 '13 at 18:06
  • @RichardJ.RossIII Agree. On the other hand, using a web view is a lot easier than diving into Core Text and writing your own math rendering library. Javascript doesn't really need to be a factor unless you choose to use a Javascript library to help with the rendering. – Caleb Apr 14 '13 at 18:17