3

I am using MathTextParser to depict some equations in Qt. Therefore i get the required tex string out of a dict, parse it using the to_rgba method of the parser i created (handing over a general fontsize and dpi) and finally hand this to my FigureCanvas of the QWidget by figimage.
Have a look:

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

My problem now is, that the equations to be parsed are of varying length whereas the FigureCanvas has a fixed size (as it needs to have) so that it happens, that some equations result in an image, which exceeds the figure in its size so that some part of the equation is cut off/not visible, like in this example:
Cut off equation
What i wouldn't like to do is to somehow predefine a fontsize for every tex string depeding on its length, for the reason that even user defined equations (of unpredictable length) need to be parsed and displayed.
So is there by any chance a way to make the parser fit the e.g. fontsize, so that the output is a rasterized tex of a fixed size independently of the equation size/length?

Besides: In comparison to e.g. numpy/scipy the docs of matplotlib seem inscrutable to me for some reason, thus if somebody could comment on how to_rgba and figimage exactly work i'd be grateful, too.. e.g. concerning to_rgba: what means the following quote elucidating depth?:

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

Thanks in advance

4

1 回答 1

0

显示 TeX 代码的另一种可能的解决方案:调用latex获取 dvi 文件,然后调用dvisvgm --no-fonts将 dvi 文件转换为 SVG,然后使用 QSvgRenderer 加载和渲染 SVG 文件: 在 python 中将乳胶代码转换为 mathml 或 svg 代码

于 2013-06-03T13:18:09.220 回答