Не могу использовать MathJax в Swift - PullRequest
0 голосов
/ 06 ноября 2019

Я пытаюсь отобразить отформатированные математические уравнения в webView, используя MathJax, проблема в том, что «компилятор находит escape-последовательность и другие специальные символы в коде HTML как ошибки». Также я никогда не использовал MathJax прежде. Я просто следуюкод ниже, который я нахожу в StackOverFlow.

Это «QUESTIONMESSAGE», который я пытался добавить в запрос: «Когда $ a \ ne 0 $, есть два решения (ax ^ 2 + bx + c= 0) и они $$ x = {-b \ pm \ sqrt {b ^ 2-4ac} \ over 2a}. $$ "

Это код,

func formatWebViewText()
{

    //Specify the text that will go in the HTML file:

    let htmlString = "<head><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [ ['$','$'] ],},\"HTML-CSS\": { linebreaks: { automatic: true, width: \"container\" } } } )</script><script src='" +
        String(Bundle.mainBundle().pathForResource("MathJax", ofType: "js", inDirectory: "MathJax-masterSlim")!) +  //reference to local MathJax files
        "?config=TeX-AMS-MML_HTMLorMML'></script></head>" +
        "<body>" + questionMessage + "</body></html>"
    // "QUESTIONMESSAGE" contains the text to be typeset/processed by MathJax

    //Give HTML file a name:
    let htmlFileName:String = "htmlTempFile.html"

    //Create a reference to the temporary directory that will store the HTML file:
    let tempDirectory = NSTemporaryDirectory() as NSString

    //Append reference temporary directory to include HTML file name:
    let htmlFilePath = tempDirectory.appendingPathComponent(htmlFileName as String)

    //Convert appended temporary directory NSString to an NSURL object:
    let htmlUrl = NSURL(fileURLWithPath: htmlFilePath as String)

    //Below, HTML string is written to a file:

    do{
        try htmlString.write(to: htmlFilePath as String , atomically: true, encoding: NSUTF8StringEncoding) 
    } catch { }

    //HTML file URL is fetched and displayed in UIWebView:
    webs.load(NSURLRequest(url: htmlUrl as URL) as URLRequest)
}
...