gpt4 book ai didi

python - 数学文本解析器 : Fit output to figsize

转载 作者:行者123 更新时间:2023-11-28 19:25:57 27 4
gpt4 key购买 nike

我正在使用 MathTextParser 来描述 Qt 中的一些方程式。因此,我从 dict 中获取所需的 tex 字符串,使用我创建的解析器的 to_rgba 方法解析它(移交一般字体大小和 dpi),最后将其交给我的 FigureCanvas QWidget 来自 figimage
看看:

class MathWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
...
self.canvas = MathCanvas() # The FigureCanvas to display the teximage
...
self.parser = mathtext.MathTextParser("Bitmap")

def render(self,metric):
equation = self.equations[metric]
rgb, d = self.parser.to_rgba(equation, color='black', fontsize=8, dpi=200)
self.canvas.fig.figimage(rgb.astype(float)/255., 0, 0)
self.canvas.draw()

我现在的问题是,要解析的方程式的长度各不相同,而 FigureCanvas 具有固定大小(因为它需要),因此某些方程式会导致图像,它的大小超过了图形,因此等式的某些部分被截断/不可见,如本例所示:
Cut off equation
我不想做的是以某种方式为每个 tex 字符串预定义一个字体大小取决于它的长度,因为即使是用户定义的方程式(不可预测的长度)也需要被解析和显示。
那么有没有办法让解析器适合例如fontsize,以便输出是独立于方程大小/长度的固定大小的光栅化 tex?

此外:与例如相比numpy/scipy matplotlib 的文档出于某种原因对我来说似乎难以理解,因此如果有人可以评论 to_rgbafigimage 究竟是如何工作的,我也将不胜感激。例如关于 to_rgba:以下说明深度的引述是什么意思?:

  • depth is the offset of the baseline from the bottom of the image in pixels

提前致谢

最佳答案

另一种显示 TeX 代码的可能解决方案:调用 latex 获取 dvi 文件,然后调用 dvisvgm --no-fonts 将 dvi 文件转换为 SVG,以及然后使用 QSvgRenderer 加载并渲染 SVG 文件: converting a latex code to mathml or svg code in python

关于python - 数学文本解析器 : Fit output to figsize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com