
    9 f                         d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	 ddl
Z
ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ  G d de          Z	 	 ddZddZddZd Zd ZdZ ddZ!dS )zTools for handling LaTeX.    )BytesIOopenN)encodebytes)Path)find_cmdFindCmdError
get_config)SingletonConfigurable)ListBoolUnicode)cast_unicodec                   8   e Zd ZdZd Z e e            ddgd                              d          Z e	dd	                              d          Z
 eg d
d                              d          Z ed                              d          ZdS )	LaTeXToolz3An object to store configuration of the LaTeX tool.c                     t                      S )Nr	   )selfs    L/var/www/equiseq/venv/lib/python3.11/site-packages/IPython/lib/latextools.py_config_defaultzLaTeXTool._config_default   s    ||    
matplotlibdvipnga  Preferred backend to draw LaTeX math equations. Backends in the list are checked one by one and the first usable one is used.  Note that `matplotlib` backend is usable only for inline style equations.  To draw  display style equations, `dvipng` backend must be specified. )helpT)configzmUse breqn.sty to automatically break long equations. This configuration takes effect only for dvipng backend.)amsmathamsthmamssymbbmziA list of packages to use for dvipng backend. 'breqn' will be automatically appended when use_breqn=True.zKAdditional preamble to use when generating LaTeX source for dvipng backend.N)__name__
__module____qualname____doc__r   r   r   tagbackendsr   	use_breqnpackagespreamble r   r   r   r      s       ==   t		L(+H
 
 
 #T#

  C
 
 
 #T#

	  t...F
 
 
 #T#

	  w
 
 
 #T#

 HHr   r   FBlack      ?c           
         t          |           } t                                          j        }||d         }||vrdS |dk    rt          }n|dk    rt
          }|                    d          rt          |          dk    r	 d                    d	                    d	 t          j        |d
d         d          D                                 }nz# t          $ r(}t          d                    |                    |d}~ww xY wt          d                    |                    n"t          d                    |                     || |||          }	|r|	rt          |	          }	|	S )al  Render a LaTeX string to PNG.

    Parameters
    ----------
    s : str
        The raw string containing valid inline LaTeX.
    encode : bool, optional
        Should the PNG data base64 encoded to make it JSON'able.
    backend : {matplotlib, dvipng}
        Backend for producing PNG data.
    wrap : bool
        If true, Automatically wrap `s` as a LaTeX equation.
    color : string
        Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB
        format, e.g. '#AA20FA'.
    scale : float
        Scale factor for the resulting PNG.
    None is returned when the backend cannot be used.

    Nr   r   r   #   zRGB {} c                 H    g | ]}t          t          |d                      S )   )strint).0xs     r   
<listcomp>z latex_to_png.<locals>.<listcomp>a   s8     6S 6S 6S!c#a**oo 6S 6S 6Sr         zInvalid color specification {}.zNo such backend {0})r   r   instancer$   latex_to_png_mpllatex_to_png_dvipng
startswithlenformatjointextwrapwrap
ValueErrorr   )
sencodebackendr@   colorscaleallowed_backendsfebin_datas
             r   latex_to_pngrK   =   s   , 	QA ))++4"1%&&&t,	H		C   		R5zzQ]$OOCHH 6S 6S6>mE!""Iq6Q6Q6S 6S 6S -T -T U UEE! ] ] ]$%F%M%Me%T%TUU[\\] !!B!I!I%!P!PQQQ		R .55g>>???qD%''H )( )x((Os   AC 
D#C==Dc                    	 ddl m}m}m} ddlm} ddlm} n# t          $ r Y d S w xY w| 	                    dd          } |rd
                    |           } 	 |                    d	          }	d
|z  }
t                      }|                    d          }|                    | d|	          \  }}}}}|                    |dz  |dz  f          }|                    d||z  | |	|           |                    |           |                    ||
dd           |                                S # t(          t*          |f$ r Y d S w xY w)Nr   )figurefont_managermathtext)backend_agg)ParseFatalExceptionz$$$z${0}$   )sizex   pathH   )dpiprop)figsize)fontpropertiesrE   pngT)rX   r=   transparent)r   rM   rN   rO   matplotlib.backendsrP   	pyparsingrQ   ImportErrorreplacer=   FontPropertiesr   MathTextParserparseFiguretextFigureCanvasAggsavefiggetvaluerA   RuntimeError)rB   r@   rE   rF   rM   rN   rO   rP   rQ   rY   rX   bufferparserwidthheightdepth_figs                     r   r9   r9   o   s   ==========3333331111111   tt 	
		$A OOA***33Ek ((00%+\\!$\%G%G"vuammURZ"$=m>>EFNAd%HHH##C(((FEtDDD   &9:   tts    
''CD1 1EEc                    	 t          d           t          d           n# t          $ r Y d S w xY wd }t          j        dk    r-t	          j                    }|xj        t          j        z  c_        	 t          t          j
                              }d}d}d}|                    |                              dd	          5 }	|	                    t          | |                     d d d            n# 1 swxY w Y   t	          j        dd
dd|g|t          j        t          j        |           t#          d|z            }
t	          j        ddddt%          |
          ddddd||d|g|t          j        t          j        |           |                    |                              d          5 }	|	                                cd d d            t)          j        |           S # 1 swxY w Y   n*# t          j        $ r Y t)          j        |           d S w xY w	 t)          j        |           d S # t)          j        |           w xY w)Nlatexr   ntztmp.texztmp.dviztmp.pngwutf8)encodingz-halt-on-errorz-interaction	batchmode)cwdstdoutstderrstartupinfo   z-Ttightz-Dz-z9z-bgTransparentz-oz-fgrb)r   r   osname
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWr   tempfilemkdtempjoinpathr   
writelines	genelatex
check_callDEVNULLroundr1   readshutilrmtreeCalledProcessError)rB   r@   rE   rF   r|   workdirtmpfiledvifileoutfilerH   
resolutions              r   r:   r:      s      tt K	w$ ,..z>>.x'))**g&&++C&+AA 	-QLL1d++,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	&WM%%#	
 	
 	
 	
 3;''
J  %%#)	
 	
 	
 	
. g&&++D11 	Q6688	 	 	 	 	 	 	
 	g	 	 	 	 	 	 	 	 	(   g	
 	ggs   ! 
//2AG2 $C2&G2 2C66G2 9C6:B7G2 1G&G2 &G**G2 -G*.G2 1H3 2HH3 HH3 3I	c                    	 t          d           t          j        d| gt          j        t          j                  }|                                \  }}|                                                    dd          S # t          $ r Y dS w xY w)z5Invoke kpsewhich command with an argument `filename`.	kpsewhich)rz   r{   rv   ra   N)r   r   PopenPIPEcommunicatestripdecoder   )filenameprocrz   r{   s       r   r   r      s    (#?:?< < <  ++--||~~$$VY777   s   A:A= =
B
Bc              #   \  K   t                                           }|o|j        ot          d          }dV  |j        }|r|dgz   }|D ]}d                    |          V  dV  |j        r	|j        V  dV  |rdV  | V  dV  n|rd	                    |           V  n| V  d
V  dS )z+Generate LaTeX document for dvipng backend.z	breqn.styz\documentclass{article}breqnz\usepackage{{{0}}}z\pagestyle{empty}z\begin{document}z\begin{dmath*}z\end{dmath*}z$${0}$$z\end{document}N)r   r8   r%   r   r&   r=   r'   )bodyr@   ltr   r&   packs         r   r   r      s     					B<R\<i&<&<E
$$$${H (wi' 1 1#**4000000
	{ k
 


	 %%%%%%



r   z-<img src="data:image/png;base64,%s" alt=%s />imagec                 j    t          | d                              d          }|rt          ||fz  S dS )zRender LaTeX to HTML with embedded PNG data using data URIs.

    Parameters
    ----------
    s : str
        The raw string containing valid inline LateX.
    alt : str
        The alt text to use for the HTML.
    T)rC   asciiN)rK   r   _data_uri_template_png)rB   altbase64_datas      r   latex_to_htmlr      sF     q...55g>>K <%+s);;;< <r   )FNFr)   r*   )r)   r*   )r   )"r"   ior   r   r   r   r   r   base64r   r?   pathlibr   IPython.utils.processr   r   traitlets.configr
   traitlets.config.configurabler   	traitletsr   r   r   IPython.utils.py3compatr   r   rK   r9   r:   r   r   r   r   r(   r   r   <module>r      s    
         				                    8 8 8 8 8 8 8 8 ' ' ' ' ' ' ? ? ? ? ? ? ) ) ) ) ) ) ) ) ) ) 0 0 0 0 0 0" " " " "% " " "J CJ/ / / /d   <; ; ; ;|
 
 
  2 N < < < < < <r   