如何使用 PyQt5 在 Qt 中显示数学排版(MathJax、LaTeX 等)? [英] How to display mathematical typesetting (MathJax, LaTeX, etc.) in Qt using PyQt5?

查看:35
本文介绍了如何使用 PyQt5 在 Qt 中显示数学排版(MathJax、LaTeX 等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Qt 和 PyQt5 还很陌生,想在 GUI 窗口中显示数学排版.具体来说,我希望它能够动态更新.我似乎找不到任何有关如何使用 PyQt5 执行此操作的有用信息.

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 seem to find any helpful information on how to do this with PyQt5.

我已经彻底研究了如何做到这一点

I've thoroughly researched how to do this

这里找到了一个看似相关的答案,但没有给出解决方案,它实际上并没有解决在 GUI 中显示排版数学的问题.

One seemingly relevant answer is found here, but no solution is given and it doesn't actually address the issue of showing the typeset math in a GUI.

这里找到另一个看似相关的答案,但使用 PySide(和python 2.7) 并给出了一个荒谬且不必要的复杂和过时的答案.

Another seemingly relevant answer is found here, but uses PySide (and python 2.7) and gives an absurdly and unecessarily complicated and outdated answer.

推荐答案

我想出了如何以一种非常简单的方式做到这一点.下面给出的示例需要 Internet 连接才能访问 MathJax JS 模块.

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. 首先,导入 QApplicationQWebEngineView.

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView

  • 然后,编写一个包含 HTML 代码的多行字符串.该代码应导入 MathJax javascript 模块.然后,写出你的数学方程式……

  • Then, write a multi-line string containing HTML code. The code should import the MathJax javascript module. Then, write your mathematical equation...

    pageSource = """
                 <html><head>
                 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">                     
                 </script></head>
                 <body>
                 <p><mathjax style="font-size:2.3em">$$u = int_{-infty}^{infty}(awesome)cdot du$$</mathjax></p>
                 </body></html>
                 """
    

  • 最后,实例化一个QApplication,实例化一个QWebEngineView,并设置WebEngineView显示你新写的HTML代码:

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

  • 别忘了在你的 WebEngineView 上调用 show.

  • Don't forget to call show on your WebEngineView.

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

  • 如果你想创建一个不需要互联网连接来运行 MathJax JS 文件的应用程序,只需复制 JS 模块并将其保存为代码中的字符串.

    这篇关于如何使用 PyQt5 在 Qt 中显示数学排版(MathJax、LaTeX 等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆