iAkshay
iAkshay

Reputation: 1263

Converting HTML text that contains Latex Comand for Mathematical formula to plain or attributed text outputting correct formula

I have set of questions stored in SQLite database. For mathematics subject those are stored as formula/equations in html format. I retrieve question and stored it as string:

NSString *htmlstring = @"If 3<em>x</em><sup><strong>2</strong></sup>&nbsp;+ 11<em>x</em>&nbsp;+ 6 $$\\ge$$&nbsp;0, then <em>x</em>&nbsp;$$\\in $$";

To display this string in UILabel, I use following piece of code :

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        questionLbl.attributedText = attrStr;

This code not worked as expected and output is:

enter image description here

The expected output is:

enter image description here

I didn't understand where its going wrong, while fetching data from database or converting html to plain text.

UPDATE

From Ashmi's answer below I figured out the text is in Latex format. How can we convert latex command to symbol is now a question. There are 3 possibilities:

  1. Convert Latex to HTML(as suggested by Ashmi), then load that HTML in label.
  2. Convert Latex to unicode character to load directly in label.
  3. Using library if any, may be like Math.h

Upvotes: 1

Views: 548

Answers (1)

ashmi123
ashmi123

Reputation: 710

Please check below code your HTML format may be not correct So I just change HTML format and get output you want. For more information to make Math symbol in HTML please check below link.

https://www.univie.ac.at/moe/formeln.html

  NSString *htmlstring = @"If 3<em>x</em><sup><strong>2</strong></sup>&nbsp;+ 11<em>x</em>&nbsp;+ 6 &#x2265;0, then <em>x</em>&#x20AC";

  NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlstring dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

  NSLog(@"%@",attrStr.string);

enter image description here

Upvotes: 3

Related Questions