Repository: ipython/ipython

Issue:
latex_to_mpl_png relies on deprectated functionality in matplotlib 3.4
Short repro:
```
In [1]: from IPython.lib import latextools                                                                                                                                           

In [2]: png = latextools.latex_to_png_mpl('f(x) = y', True)                                
/lib/python3.8/site-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_png function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/lib/python3.8/site-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_rgba function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/lib/python3.8/site-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_mask function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/lib/python3.8/site-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The MathtextBackendBitmap class was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
```

Deprecation info: https://matplotlib.org/stable/api/api_changes.html#mathtextparser-bitmap-is-deprecated

`math_to_image` has been around since the beginning, so it hopefully has no version compatibility issues: https://github.com/matplotlib/matplotlib/blame/e904fa3b1e5455cf741017268dae7c94b6caa5d6/lib/matplotlib/mathtext.py#L587


Discussion:
@jakevdp which IPython version are you using? I cannot reproduce this on 7.22.0 nor master.
Using IPython 7.22.0 and matplotlib 3.4.1, with Python 3.7.10 on Ubuntu:
```python
from IPython.lib import latextools
import sys
import IPython
import matplotlib
print(sys.version_info)
print("IPython version:", IPython.__version__)
print("matplotlib version:", matplotlib.__version__)
png = latextools.latex_to_png_mpl('f(x) = y', True)
```
```
sys.version_info(major=3, minor=7, micro=10, releaselevel='final', serial=0)
IPython version: 7.22.0
matplotlib version: 3.4.1
/usr/local/lib/python3.7/dist-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_png function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/usr/local/lib/python3.7/dist-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_rgba function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/usr/local/lib/python3.7/dist-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The to_mask function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
/usr/local/lib/python3.7/dist-packages/IPython/lib/latextools.py:126: MatplotlibDeprecationWarning: 
The MathtextBackendBitmap class was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use mathtext.math_to_image instead.
  mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
```
Got it! Thank you!

Resolving patch:
diff --git a/IPython/lib/latextools.py b/IPython/lib/latextools.py
--- a/IPython/lib/latextools.py
+++ b/IPython/lib/latextools.py
@@ -111,7 +111,8 @@ def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black',
 
 def latex_to_png_mpl(s, wrap, color='Black', scale=1.0):
     try:
-        from matplotlib import mathtext
+        from matplotlib import figure, font_manager, mathtext
+        from matplotlib.backends import backend_agg
         from pyparsing import ParseFatalException
     except ImportError:
         return None
@@ -122,11 +123,18 @@ def latex_to_png_mpl(s, wrap, color='Black', scale=1.0):
         s = u'${0}$'.format(s)
 
     try:
-        mt = mathtext.MathTextParser('bitmap')
-        f = BytesIO()
-        dpi = 120*scale
-        mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
-        return f.getvalue()
+        prop = font_manager.FontProperties(size=12)
+        dpi = 120 * scale
+        buffer = BytesIO()
+
+        # Adapted from mathtext.math_to_image
+        parser = mathtext.MathTextParser("path")
+        width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)
+        fig = figure.Figure(figsize=(width / 72, height / 72))
+        fig.text(0, depth / height, s, fontproperties=prop, color=color)
+        backend_agg.FigureCanvasAgg(fig)
+        fig.savefig(buffer, dpi=dpi, format="png", transparent=True)
+        return buffer.getvalue()
     except (ValueError, RuntimeError, ParseFatalException):
         return None
 


Test patch: