How to display mathematical typesetting (MathJax, LaTeX, etc.) in Qt using PyQt5?

后端 未结 1 1421
心在旅途
心在旅途 2021-01-06 19:42

I am fairly new to Qt and PyQt5 and would like to display mathematical typesetting in a GUI window. Specifically, I would like it to be able to update dynamically. I can\'t

1条回答
  •  隐瞒了意图╮
    2021-01-06 20:02

    I figured out how to do this in a manner that is quite easy and simple. The example given below requires internet connectivity to access the MathJax JS module.

    1. First, import QApplication and QWebEngineView.

      import sys
      from PyQt5.QtWidgets import QApplication
      from PyQt5.QtWebEngineWidgets import QWebEngineView
      
    2. Then, write a multi-line string containing HTML code. The code should import the MathJax javascript module. Then, write your mathematical equation...

      pageSource = """
                   
                   
                   
                   

      $$u = \int_{-\infty}^{\infty}(awesome)\cdot du$$

      """
    3. Finally, instantiate a QApplication, instantiate a QWebEngineView, and set the WebEngineView to show your newly written HTML code:

      app = QApplication(sys.argv)
      webView = QWebEngineView()
      webView.setHtml(pageSource)
      
    4. Don't forget to call show on your WebEngineView.

      webView.show()
      sys.exit(app.exec_())
      

    If you want to create an app that does not require internet connectivity to run the MathJax JS file, simply copy the JS module and save it as a string in your code.

    0 讨论(0)
提交回复
热议问题