
    P/Ph                        d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	Z	ddl
mZmZ ddlZddlmZmZ ddlmZ  G d	 d
ej        ej                  Zd Zi g fdZeede_         G d de          ZddZd Zd Zd ZdS )a  
A role and directive to display mathtext in Sphinx
==================================================

The ``mathmpl`` Sphinx extension creates a mathtext image in Matplotlib and
shows it in html output. Thus, it is a true and faithful representation of what
you will see if you pass a given LaTeX string to Matplotlib (see
:ref:`mathtext`).

.. warning::
    In most cases, you will likely want to use one of `Sphinx's builtin Math
    extensions
    <https://www.sphinx-doc.org/en/master/usage/extensions/math.html>`__
    instead of this one. The builtin Sphinx math directive uses MathJax to
    render mathematical expressions, and addresses accessibility concerns that
    ``mathmpl`` doesn't address.

Mathtext may be included in two ways:

1. Inline, using the role::

     This text uses inline math: :mathmpl:`\alpha > \beta`.

   which produces:

     This text uses inline math: :mathmpl:`\alpha > \beta`.

2. Standalone, using the directive::

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

   which produces:

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

Options
-------

The ``mathmpl`` role and directive both support the following options:

fontset : str, default: 'cm'
    The font set to use when displaying math. See :rc:`mathtext.fontset`.

fontsize : float
    The font size, in points. Defaults to the value from the extension
    configuration option defined below.

Configuration options
---------------------

The mathtext extension has the following configuration options:

mathmpl_fontsize : float, default: 10.0
    Default font size, in points.

mathmpl_srcset : list of str, default: []
    Additional image sizes to generate when embedding in HTML, to support
    `responsive resolution images
    <https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images>`__.
    The list should contain additional x-descriptors (``'1.5x'``, ``'2x'``,
    etc.) to generate (1x is the default and always included.)

    N)Path)nodes)	Directive
directives)ConfigErrorExtensionError)_apimathtext)validate_float_or_Nonec                       e Zd ZdS )
latex_mathN)__name__
__module____qualname__     \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/matplotlib/sphinxext/mathmpl.pyr   r   W   s        Dr   r   c                 J    t          j        | t          j        j                  S N)r   choicer
   MathTextParser_font_type_mapping)args    r   fontset_choicer   [   s    S("9"LMMMr   c                    |                     d          }||dz   d         }t          |          }	||	d<   |                    dd          |	d<   |                    dt          j        j        j                  |	d<   |	gg fS )N`   latexfontsetcmfontsize)findr   getsetupappconfigmathmpl_fontsize)
rolerawtexttextlinenoinlineroptionscontentir   nodes
             r   	math_roler2   _   s    SAAaCFOEgDDMkk)T22DO{{:#(9#3#DF FD62:r   r    r"   c                   2    e Zd ZdZdZdZdZdZee	dZ
d ZdS )MathDirectivezR
    The ``.. mathmpl::`` directive, as documented in the module's docstring.
    Tr   Fr3   c                    d                     | j                  }t          | j                  }||d<   | j                            dd          |d<   | j                            dt          j        j        j	                  |d<   |gS )N r   r    r!   r"   )
joinr/   r   
block_textr.   r$   r%   r&   r'   r(   )selfr   r1   s      r   runzMathDirective.runx   sz    %%$/**W,**9d;;Y<++J,1I,<,MO OZvr   N)r   r   r   __doc__has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   r   option_specr;   r   r   r   r5   r5   m   sX          K %,57 7K    r   r5   r!   
   d   c                     t          j        ||d          5  	 t          j        d|  d||d          }n)# t          $ r t          j        d|             d}Y nw xY wd d d            n# 1 swxY w Y   |S )N)zmathtext.fontsetz	font.size$png)dpiformatz!Could not render math expression r   )mpl
rc_contextr
   math_to_image	Exceptionr	   warn_external)r   filenamer    r"   rG   depths         r   	latex2pngrP      s    	W8LL	M	M  	*EhC? ? ?EE 	 	 	J5JJKKKEEE		               Ls1   A,7A,#AA,AA,,A03A0c                    t          | j        t          j                  }| d         }| d         }| d         }d                    t          j        | | |                                                                           dd                    }t          t          j        j        j        dd          }|                    dd	           || d
z  }t          ||||          }	g }
t          j        j        j        D ]~}| d|                    dd           d
}t          |||z  ||dt'          |d d                   z             |
                    t          j        j        j         d| d|            |
r6dt          j        j        j         d| dd                    |
          z   dz   }
|rd}nd}|r|	dk    r	d|	dz   z  }nd}dt          j        j        j         d| d|
 | | d	S ) Nr   r    r"   zmath-{}i_imagesmathmplT)parentsexist_okz.png)r"   -._rC   r   )r"   rG   z	/mathmpl/ zsrcset="z.png, z, z" r7   zclass="center" r   z)style="position: relative; bottom: -%dpx"r   z
<img src="z.png" z/>)
isinstanceparentr   TextElementrH   hashlibmd5encode	hexdigestr   r%   r&   builderoutdirmkdirrP   r'   mathmpl_srcsetreplacefloatappendimgpathr8   )r1   sourceinliner   r    r"   namedestdirdestrO   srcsetsizerN   clsstyles                  r   
latex2htmlrr      sg   U%677FME9oGJHu1g1x1188::;;EEGGMO OD 59$+Y	BBGMM$M...]]]"DeT7X>>>EF	 / F F99T\\#s33999%8+WxE$ss),,,	. 	. 	. 	.y (DD8DDdDD	F 	F 	F 	F ,MUY.6MMMMM))F##$&*+    %1**;uqyI'*2 ' 'T ' '''"' ' ' (r   c                     t          | j        j                  D ]]\  }}|d         dk    r:	 t          |d d                    *# t          $ r t          d|d          w xY wt          d|d          d S )Nr   xz,Invalid value for mathmpl_srcset parameter: ze. Must be a list of strings with the multiplicative factor followed by an "x".  e.g. ["2.0x", "1.5x"])	enumerater'   rd   rf   
ValueErrorr   )r&   r'   r0   ro   s       r   _config_initedrw      s    SZ677 E E48s??Id3B3i     I I I!H4 H H HI I II Dt D D DE E EE Es   AA"c                 ^   | t           _        |                     ddd           |                     dg d           	 |                     dt                     n'# t
          $ r |                     dd            Y nw xY wd }d	 }d
 }d }|                     t          ||f||f           |                     dt                     | 
                    dt                     t          j        dk     r6|                     dt                     | 
                    dt                     ddd}|S )Nr(   g      $@Trd   zconfig-initedzenv-updatedc                 "    t          | d           S r   )rw   )r&   envs     r   <lambda>zsetup.<locals>.<lambda>   s    N34M4M r   c                 z    | j         j        d         }| j                            t	          ||                     d S )Nri   )document
attributesbodyrg   rr   )r:   r1   ri   s      r   visit_latex_math_htmlz$setup.<locals>.visit_latex_math_html   s7    )(3	D&1122222r   c                     d S r   r   r:   r1   s     r   depart_latex_math_htmlz%setup.<locals>.depart_latex_math_html       r   c                     t          |j        t          j                  }|r%| j                            d|d         z             d S | j                            d|d         dg           d S )Nz$%s$r   z\begin{equation}z\end{equation})rZ   r[   r   r\   r   rg   extend)r:   r1   rj   s      r   visit_latex_math_latexz%setup.<locals>.visit_latex_math_latex   sy    DK):;; 	2IVd7m344444I1"7m/1 2 2 2 2 2r   c                     d S r   r   r   s     r   depart_latex_math_latexz&setup.<locals>.depart_latex_math_latex   r   r   )htmlr   rS   )r      math)parallel_read_safeparallel_write_safe)r%   r&   add_config_valueconnectrw   r   add_noder   add_roler2   add_directiver5   sphinxversion_info)r&   r   r   r   r   metadatas         r   r%   r%      s{   EI+T4888)2t444OO^4444 O O OM#M#MNNNNNO3 3 3  2 2 2   LL,.DE.0GH  J J J LLI&&&i///V##VY'''&-000&*4HHHOs   A !A<;A<)r!   rB   rC   )r<   r]   pathlibr   docutilsr   docutils.parsers.rstr   r   r   sphinx.errorsr   r   
matplotlibrI   r	   r
   matplotlib.rcsetupr   GeneralElementr   r   r2   r.   r5   rP   rr   rw   r%   r   r   r   <module>r      s  F FP              6 6 6 6 6 6 6 6  5 5 5 5 5 5 5 5     % % % % % % % % 5 5 5 5 5 5	 	 	 	 	 	 	 	N N N
 "	 	 	 	 !/!79 9	     I   ,   #( #( #(LE E E$( ( ( ( (r   