
    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!y)zTools for handling LaTeX.    )BytesIOopenN)encodebytes)Path)find_cmdFindCmdError
get_config)SingletonConfigurable)ListBoolUnicode)cast_unicodec                       e Zd ZdZd Z e e       ddgd      j                  d      Z e	dd	      j                  d      Z
 eg d
d      j                  d      Z ed      j                  d      Zy)	LaTeXToolz3An object to store configuration of the LaTeX tool.c                     t               S )Nr	   )selfs    Q/var/www/cvtools/html/venv/lib/python3.12/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    = 	L(+H
 #T#
  C
 #T#
	  .F
 #T#
	  
 #T#
 r   r   c                    t        |       } t        j                         j                  }||d   }||vry|dk(  rt        }n|dk(  rt
        }|j                  d      rt        |      dk(  rZ	 dj                  dj                  t        j                  |d	d d
      D cg c]  }t        t        |d             c}            }n4t        dj                  |            t        dj                  |             || |||      }
|r|
rt        |
      }
|
S c c}w # t        $ r }	t        dj                  |            |	d}	~	ww xY w)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 {}          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strint
ValueErrorr   )sencodebackendr8   colorscaleallowed_backendsfxebin_datas              r   latex_to_pngrF   =   sF   , 	QA ))+44"1%&&,	H	C 5zQ]$OOCHH6>mmE!"Iq6Q6S!c#a*o 6S -T UE
 !!B!I!I%!PQQ.55g>??D%'H(x(O6S! ]$%F%M%Me%TU[\\]s*   -1D D:D D 	E$D??Ec                 0   	 ddl m}m}m} ddlm} ddlm} | j                  dd      } |rdj                  |       } 	 |j                  d	      }	d
|z  }
t               }|j                  d      }|j                  | d|	      \  }}}}}|j                  |dz  |dz  f      }|j!                  d||z  | |	|       |j#                  |       |j%                  ||
dd       |j'                         S # t        $ r Y y w xY w# t(        t*        |f$ r Y y w xY w)Nr   )figurefont_managermathtext)backend_agg)ParseFatalExceptionz$$$z${0}$   )sizex   pathH   )dpiprop)figsize)fontpropertiesr?   pngT)rS   r5   transparent)r   rH   rI   rJ   matplotlib.backendsrK   	pyparsingrL   ImportErrorreplacer5   FontPropertiesr   MathTextParserparseFiguretextFigureCanvasAggsavefiggetvaluer;   RuntimeError)r<   r8   r?   r@   rH   rI   rJ   rK   rL   rT   rS   bufferparserwidthheightdepth_figs                     r   r1   r1   o   s$   ==31
 	
		$AOOA***3Ek ((0%+\\!$\%G"vuammURZ"$=m>EFNAd%H##C(FEtD  )  * &9: s#   C3 B4D 3	C?>C?DDc                 t   	 t        d       t        d       d }t        j                  dk(  r7t	        j
                         }|xj                  t        j                  z  c_        	 t        t        j                               }d}d}d}|j                  |      j                  dd	      5 }	|	j                  t        | |             d d d        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                   |       |j                  |      j                  d      5 }	|	j'                         cd d d        t)        j*                  |       S # t        $ r Y y w xY w# 1 sw Y   xY w# 1 sw Y   nxY wn.# t        j,                  $ r Y t)        j*                         y w xY w	 t)        j*                  |       y # 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roundr9   readshutilrmtreeCalledProcessError)r<   r8   r?   r@   rw   workdirtmpfiledvifileoutfilerB   
resolutions              r   r2   r2      s    K	ww$ ,,.z>>>.x'')*g&++C&+A 	-QLL1d+,	- 	&WM%%%%#	
 3;'
J  %%%%#)	
. g&++D1 	Q668	 	
 	go  	- 	-F	 	 	(( g	
 	ggsm   F3 %AG *GB>G G	G 3	F?>F?GG GG H  H.H  HH   H7c                 
   	 t        d       t        j                  d| gt        j                  t        j                        }|j	                         \  }}|j                         j                  dd      S # t        $ r Y yw xY w)z5Invoke kpsewhich command with an argument `filename`.	kpsewhich)ru   rv   rq   r\   N)r   r   PopenPIPEcommunicatestripdecoder   )filenameprocru   rv   s       r   r   r      st    (#??:??<  ++-||~$$VY77 s   A3A6 6	BBc              #   p  K   t         j                         }|xr |j                  xr t        d      }d |j                  }|r|dgz   }|D ]  }dj                  |        d |j                  r|j                   d |rd |  d n|rd	j                  |        n|  d
 yw)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   r0   r%   r   r&   r5   r'   )bodyr8   ltr   r&   packs         r   r   r      s     				B<R\\<i&<E
$${{Hwi' 1#**4001
	{{kk

	%%

s   B4B6z-<img src="data:image/png;base64,%s" alt=%s />c                 V    t        | d      j                  d      }|rt        ||fz  S y)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)r=   asciiN)rF   r   _data_uri_template_png)r<   altbase64_datas      r   latex_to_htmlr      s3     q.55g>K%+s);;; r   )FNFBlack      ?)r   r   )image)"r"   ior   r   r}   r   r   r   base64r   r7   pathlibr   IPython.utils.processr   r   traitlets.configr
   traitlets.config.configurabler   	traitletsr   r   r   IPython.utils.py3compatr   r   rF   r1   r2   r   r   r   r   r(   r   r   <module>r      su    
  	       8 ' ? ) ) 0"% "J CJ/d<;|
2 N <r   