
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/usetex_baseline_test.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_text_labels_and_annotations_usetex_baseline_test.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_text_labels_and_annotations_usetex_baseline_test.py:


====================
Usetex Baseline Test
====================

A test for :rc:`text.latex.preview`, a deprecated feature which relied
on the preview.sty LaTeX package to properly align TeX baselines.  This
feature has been deprecated as Matplotlib's dvi parser now computes baselines
just as well as preview.sty; this example will be removed together with
:rc:`text.latex.preview` after the deprecation elapses.

.. GENERATED FROM PYTHON SOURCE LINES 12-77



.. image:: /gallery/text_labels_and_annotations/images/sphx_glr_usetex_baseline_test_001.png
    :alt: usetex=False , usetex=True preview=False, usetex=True preview=True
    :class: sphx-glr-single-img





.. code-block:: default


    import matplotlib.pyplot as plt
    import matplotlib.axes as maxes


    plt.rcParams.update({"mathtext.fontset": "cm", "mathtext.rm": "serif"})


    @maxes.subplot_class_factory
    class LatexPreviewSubplot(maxes.Axes):
        """
        A hackish way to simultaneously draw texts with text.latex.preview=True and
        text.latex.preview=False in the same figure.  It does not work with the ps
        backend.
        """

        def __init__(self, *args, preview=False, **kwargs):
            self.preview = preview
            super().__init__(*args, **kwargs)

        def draw(self, renderer):
            with plt.rc_context({"text.latex.preview": self.preview}):
                super().draw(renderer)


    def test_window_extent(ax, usetex, preview):

        ax.xaxis.set_visible(False)
        ax.yaxis.set_visible(False)

        test_strings = ["lg", r"$\frac{1}{2}\pi$",
                        r"$p^{3^A}$", r"$p_{3_2}$"]

        ax.axvline(0, color="r")

        for i, s in enumerate(test_strings):
            ax.axhline(i, color="r")
            ax.text(0., 3 - i, s,
                    usetex=usetex,
                    verticalalignment="baseline",
                    size=50,
                    bbox=dict(pad=0, ec="k", fc="none"))

        ax.set_xlim(-0.1, 1.1)
        ax.set_ylim(-.8, 3.9)

        title = f"usetex={usetex}\n"
        if usetex:
            title += f"preview={preview}"
        ax.set_title(title)


    fig = plt.figure(figsize=(2 * 3, 6.5))

    for i, usetex, preview in [[0, False, False],
                               [1, True, False],
                               [2, True, True]]:
        ax = LatexPreviewSubplot(fig, 1, 3, i + 1, preview=preview)
        fig.add_subplot(ax)
        fig.subplots_adjust(top=0.85)

        test_window_extent(ax, usetex=usetex, preview=preview)


    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  2.494 seconds)


.. _sphx_glr_download_gallery_text_labels_and_annotations_usetex_baseline_test.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: usetex_baseline_test.py <usetex_baseline_test.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: usetex_baseline_test.ipynb <usetex_baseline_test.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
