Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt latex

import sys
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PySide import QtGui, QtCore

def mathTex_to_QPixmap(mathTex, fs):

    #---- set up a mpl figure instance ----

    fig = mpl.figure.Figure()
    fig.patch.set_facecolor('none')
    fig.set_canvas(FigureCanvasAgg(fig))
    renderer = fig.canvas.get_renderer()

    #---- plot the mathTex expression ----

    ax = fig.add_axes([0, 0, 1, 1])
    ax.axis('off')
    ax.patch.set_facecolor('none')
    t = ax.text(0, 0, mathTex, ha='left', va='bottom', fontsize=fs)

    #---- fit figure size to text artist ----

    fwidth, fheight = fig.get_size_inches()
    fig_bbox = fig.get_window_extent(renderer)

    text_bbox = t.get_window_extent(renderer)

    tight_fwidth = text_bbox.width * fwidth / fig_bbox.width
    tight_fheight = text_bbox.height * fheight / fig_bbox.height

    fig.set_size_inches(tight_fwidth, tight_fheight)

    #---- convert mpl figure to QPixmap ----

    buf, size = fig.canvas.print_to_buffer()
    qimage = QtGui.QImage.rgbSwapped(QtGui.QImage(buf, size[0], size[1],
                                                  QtGui.QImage.Format_ARGB32))
    qpixmap = QtGui.QPixmap(qimage)

    return qpixmap
Comment

PREVIOUS NEXT
Code Example
Python :: How to find the three largest values of an array efficiently, in Python? 
Python :: python pdf to excel 
Python :: pandas scatter plot with different colors 
Python :: python create 2d array deep copy 
Python :: read data from yaml file in python 
Python :: python reverse string 
Python :: how to replace single string in all dictionary keys in python 
Python :: run python script from c# 
Python :: python get everything between two characters 
Python :: python gtts 
Python :: tuple in godot 
Python :: how to construct simple timedelta in python 
Python :: how to download file in python 
Python :: fill na with mode and mean python 
Python :: how to make python speak 
Python :: b1-motion tkinter 
Python :: convert categorical data type to int in pandas 
Python :: how to add 2 dates in python 
Python :: main arguments python 
Python :: how to change a thread name in python 
Python :: spacex 
Python :: scatter plot plotly 
Python :: add time delta pytohn 
Python :: How to replace both the diagonals of dataframe with 0 in pandas 
Python :: numpy print options 
Python :: how to log ip addresses in flask 
Python :: python backward difference 
Python :: Add a quit button Tkinter 
Python :: TypeError: dict is not a sequence 
Python :: say command python 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =