【问题标题】:Converting HTML text that contains Latex Comand for Mathematical formula to plain or attributed text outputting correct formula将包含用于数学公式的 Latex 命令的 HTML 文本转换为输出正确公式的纯文本或属性文本
【发布时间】:2016-08-09 16:49:01
【问题描述】:

我有一组存储在 SQLite 数据库中的问题。对于数学科目,这些以 html 格式存储为公式/方程式。我检索问题并将其存储为字符串:

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 $$";

为了在 UILabel 中显示这个字符串,我使用以下代码:

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

此代码未按预期工作,输出为:

预期的输出是:

在从数据库中获取数据或将 html 转换为纯文本时,我不明白哪里出了问题。

更新

从下面 Ashmi 的回答中,我发现文本是乳胶格式。 我们如何将乳胶命令转换为符号现在是一个问题。有3种可能:

  1. 将 Latex 转换为 HTML(按照 Ashmi 的建议),然后加载该 HTML 在标签中。
  2. 将 Latex 转换为 unicode 字符以直接加载到标签中。
  3. 如果有的话,使用库,可能类似于 Math.h

【问题讨论】:

    标签: ios objective-c sqlite nsattributedstring mathematical-expressions


    【解决方案1】:

    请检查下面的代码,您的 HTML 格式可能不正确所以我只是更改 HTML 格式并获得您想要的输出。有关在 HTML 中制作数学符号的更多信息,请查看以下链接。

    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);
    

    【讨论】:

    • 我正在动态地从数据库中获取 html 字符串。但是,您提供的链接帮助我找出文本是 LaTeX 格式。所以,需要找到一种从 iOS 中的 LaTex 命令中获取符号的方法。
    • 是的,但是您可以搜索 html 到 LaTex 格式并将其传递给字符串。
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2012-04-12
    • 2014-09-08
    • 1970-01-01
    相关资源
    最近更新 更多