<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Drawing LaTeX image in pyside6 using TextObject]]></title><description><![CDATA[<p dir="auto">I want to draw rendered LaTeX in a pyside6 application using TextObjectInterface where an object is stored as TextObject and LaTeX is painted on it. I wish to avoid TextImage as it works but there is problem of accumulation of bmp in resource cache when deleting old LaTeX equations and adding new ones. The code below does not generate only text before and after LaTeX image and nothing appears in place of the image. I have a TeX distribution installed. Everything works with TextImage, no error message appears. It did not work in 3.13 nor in 3.12.13.</p>
<pre><code>
I want the program to display the formula rendered on the screen, e.g.

</code></pre>
<p dir="auto">a^2 + b^2 = c^2</p>
<pre><code>
But nothing appears. I am using QTextObject to draw the LaTeX image directly on an object containing a math expression.

I need python equal or greater than .12 for gemini api feature So i cannot use pyqt6 as it does not get install.

Recommend me the best version of python for QTextObject and gemini API 

</code></pre>
<p dir="auto">import sys<br />
from io import BytesIO<br />
from PySide6.QtWidgets import QApplication, QTextEdit<br />
from PySide6.QtGui import (<br />
QTextObjectInterface,<br />
QTextFormat,<br />
QTextCharFormat,<br />
QImage,<br />
QPainter,<br />
QColor<br />
)<br />
from PySide6.QtCore import QSizeF, QObject, Qt</p>
<p dir="auto">from matplotlib.figure import Figure<br />
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas</p>
<p dir="auto">LATEX_FORMAT = QTextFormat.UserObject + 1</p>
<p dir="auto">class LatexRenderer(QObject, QTextObjectInterface):<br />
def <strong>init</strong>(self):<br />
super().<strong>init</strong>()<br />
self._cache = {}</p>
<pre><code>def intrinsicSize(self, doc, posInDocument, format):
    latex = format.property(1)
    img = self.render_latex(latex)
    return QSizeF(img.width(), img.height())

def drawObject(self, painter, rect, doc, posInDocument, format):
    latex = format.property(1)
    img = self.render_latex(latex)
    
    # DEBUG: Draw a light gray background so we can see the "box"
    painter.fillRect(rect, QColor("#f0f0f0"))
    
    painter.drawImage(rect.topLeft(), img)

def render_latex(self, latex):
    if latex in self._cache:       
        return self._cache[latex]

    # Force a specific DPI and small figure
    fig = Figure(figsize=(1, 1), dpi=100)
    canvas = FigureCanvas(fig)
    fig.patch.set_alpha(0) 

    # Using 'mathtext' specifically
    fig.text(0.5, 0.5, f"${latex}$", 
             fontsize=20, 
             ha='center', 
             va='center', 
             color="black")

    buf = BytesIO()
    # 'tight' is essential, but let's add a small pad to be safe
    fig.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True)
    
    img = QImage()
    img.loadFromData(buf.getvalue(), "PNG")
    
    self._cache[latex] = img
    return img
</code></pre>
<p dir="auto">class MainWindow(QTextEdit):<br />
def <strong>init</strong>(self):<br />
super().<strong>init</strong>()</p>
<pre><code>    # 1. Register handler BEFORE adding text
    self.renderer = LatexRenderer()
    self.document().documentLayout().registerHandler(LATEX_FORMAT, self.renderer)

    # 2. Insert text and math
    cursor = self.textCursor()
    cursor.insertText("The Pythagorean theorem is: ")
    
    # Use a RAW string for LaTeX
    self.insert_latex(cursor, r"a^2 + b^2 = c^2")
    
    cursor.insertText("\n\nAnd the Quadratic formula: ")
    self.insert_latex(cursor, r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}")

def insert_latex(self, cursor, latex):
    fmt = QTextCharFormat()
    fmt.setObjectType(LATEX_FORMAT)
    fmt.setProperty(1, latex)
    cursor.insertText("\uFFFC", fmt)
</code></pre>
<p dir="auto">if <strong>name</strong> == "<strong>main</strong>":<br />
app = QApplication(sys.argv)<br />
# Removing the deprecated HighDPI line as it's now default<br />
w = MainWindow()<br />
w.resize(600, 400)<br />
w.show()<br />
sys.exit(app.exec())</p>
]]></description><link>https://forum.qt.io/topic/164525/drawing-latex-image-in-pyside6-using-textobject</link><generator>RSS for Node</generator><lastBuildDate>Thu, 21 May 2026 13:56:55 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164525.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 05 Apr 2026 10:19:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Drawing LaTeX image in pyside6 using TextObject on Sun, 05 Apr 2026 11:49:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pasmon90">@<bdi>pasmon90</bdi></a> said in <a href="/post/837544">Drawing LaTeX image in pyside6 using TextObject</a>:</p>
<blockquote>
<p dir="auto">I need python equal or greater than .12 for gemini api feature So i cannot use pyqt6 as it does not get install.</p>
</blockquote>
<p dir="auto">I know nothing about your situation.  But if you are saying that PyQt6 would work, or is at least worth trying to compare against PySide behaviour, have you tried running PyQt6 with whatever 3.12/3.13 version of Python, regardless of whether PyQt6 is supplied with an older version of Python?</p>
]]></description><link>https://forum.qt.io/post/837546</link><guid isPermaLink="true">https://forum.qt.io/post/837546</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 05 Apr 2026 11:49:53 GMT</pubDate></item><item><title><![CDATA[Reply to Drawing LaTeX image in pyside6 using TextObject on Sun, 05 Apr 2026 10:33:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pasmon90">@<bdi>pasmon90</bdi></a><br />
For my part at least: especially when using Python, please use the forum's "Code" tags ( <code>&lt;/&gt;</code> button or blocks delimited at start and end by <code>```</code> ) to properly format your post and your code, including leading whitespace.</p>
]]></description><link>https://forum.qt.io/post/837545</link><guid isPermaLink="true">https://forum.qt.io/post/837545</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 05 Apr 2026 10:33:37 GMT</pubDate></item></channel></rss>