from matplotlib.backends.backend_svg import RendererSVG
from matplotlib.font_manager import FontProperties
from io import StringIO
from IPython.display import display_html
We use matplotlib’s mathtext system and SVG backend to generate an SVG rendering of LaTeX code.
= FontProperties()
fp
= r"$D^{\ast\ast}\ell\nu$"
s
# get bounding box of rendered text
= RendererSVG(0, 0, StringIO())
r = r.get_text_width_height_descent(s, fp, ismath=True)
w, h, _
with StringIO() as f:
# render LaTeX as path
= RendererSVG(w, h, f)
r 0, h, s, fp, angle=0, ismath=True)
r.draw_text(r.new_gc(), 0)
f.seek(= f.read()
svg_code
=True) display_html(svg_code, raw
We can insert the SVG image into HTML layouts, for example, into an HTML table.
f"""
display_html(<html>
<body>
<table>
<tr>
<td> {svg_code} </td> <td> foo </td>
</tr>
<tr>
<td> bar </td> <td> baz </td>
</tr>
</table>
</body>
</html>
""", raw=True)
foo | |
bar | baz |