3

I am building an app using appcelerator which needs to display a mathematical expression e.g 2^3 but with the 3 being a small number above the 2. Is there a way to express mathematical expressions in Javascript?

Notes

  • The text is a static bit of text.
  • Language in use is Javascript.
  • Runs on the iPhone/Android simulator in the appcelerator framework.
  • Appcelerator escapes HTML

Thanks in advance for any assistance.

woot586
  • 3,906
  • 10
  • 32
  • 40
  • I suppose the end result is it being displayed, so maybe there is a special html power code or something – woot586 Jul 21 '11 at 13:57
  • What about brackets? How full-fledged should it be? – pimvdb Jul 21 '11 at 13:58
  • 1
    Have you tried using unicode characters? (^3 is 00B3) – Pat Jul 21 '11 at 14:00
  • Its relatively simple, I just have a couple of labels like cm^2. Just tried adding some html tags around the text and it seems appcelerator escapes html tags. – woot586 Jul 21 '11 at 14:00
  • Is it client side that shoud be represented in browser? if it is so you may use simple markup x2 – Fedor Skrynnikov Jul 21 '11 at 14:02
  • it's a pity that [MathML](http://en.wikipedia.org/wiki/MathML) doesn't have wider cross-browser support, or I'd have recommended it. But as it is, it isn't supported by any of the mobile browsers, which rules it out for you. – Spudley Jul 21 '11 at 14:03

5 Answers5

2

"2"+"3".sup() should give you the correct formatting.

You can also use .sub() for subscript.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
rennekon
  • 157
  • 2
  • 7
2

if you are concerned only about the display, you can make use of the html tags <sub></sub> and <sup></sup> which makes the text between them subscript and superscript.
A big plus is that the native javascript string has the functionality of returning a string wrapped into theese tags (not that it would've been very hard to implement):

var a = "2",
    b = "3",
    formattedEquation = a+b.sup(),
    r = Math.pow(a,b),
    htmlEquation = formattedEquation + ' = ' + r;
// let's say you use jquery for the simplicity of the example
$(myContainer).html(htmlEquation);
gion_13
  • 41,171
  • 10
  • 96
  • 108
1

For more elaborate mathematical typesetting, you might be able to use the MathJax JavaScript display engine. It looks to be fully compatible with WebKit and seems to typeset things fine on my iOS devices.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
0

There are the <sup> and the <sub> tags in HTML, try this:

<html>x<sup>4</sup>+y<sup>1245687</sup></html>

If this isn't enough, take a look at MathML

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
  • this works in HTML but I think the OP has a problem with Appcelerator escaping HTML tags. – Perception Jul 21 '11 at 14:03
  • My understanding of appcelerator was, you build a webapp and have additional javascript apis to the native functions of the phone. Don't you write HTML pages in appcelerator? – Tim Büthe Jul 21 '11 at 14:13
  • thats my understanding too. I was referring to the OP's comment on his original question. I think he needs to include more source code. – Perception Jul 21 '11 at 14:17
  • For things like JQueryMobile, but with Appcelerator you write it in javascript and it then compiles it and generates native iOS and Android code. – woot586 Jul 21 '11 at 14:22
0

Some good ideas which would work on platforms like JQueryMobile but unfortunately Appcelerator escapes all html (Unless your coding just for the Android).

The way to display the mathematical expressions is by defining the unicode character in the string as suggested by Pat in a comment.

E.g.

2\u00B3
woot586
  • 3,906
  • 10
  • 32
  • 40