o
    Zh                     @   s   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 G d
d deZ	ddefddZdddZdddZdd Zdd ZdZd ddZ dS )!zTools for handling LaTeX.    )BytesIOopenN)encodebytes)Path)find_cmdFindCmdError
get_config)SingletonConfigurable)ListBoolUnicodec                   @   sr   e Zd ZdZdd Zee ddgddjdd	Ze	dd
djdd	Z
eg dddjdd	Zeddjdd	ZdS )	LaTeXToolz3An object to store configuration of the LaTeX tool.c                 C   s   t  S )Nr   )self r   M/var/www/html/lang_env/lib/python3.10/site-packages/IPython/lib/latextools.py_config_default   s   zLaTeXTool._config_default
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.)ZamsmathZamsthmZamssymbZ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preambler   r   r   r   r      s4    

r   FBlack      ?sc           
   
   C   s   t | tsJ t j}|du r|d }||vrdS |dkr!t}nL|dkrft}|dret|dkr^zd	d
d	d
 t|dd dD }W n# ty] } ztd	||d}~w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                 S   s   g | ]	}t t|d qS )   )strint).0xr   r   r   
<listcomp>a   s    z latex_to_png.<locals>.<listcomp>      zInvalid color specification {}.zNo such backend {0})
isinstancer'   r   instancer   latex_to_png_mpllatex_to_png_dvipng
startswithlenformatjointextwrapwrap
ValueErrorr   )
r"   encodebackendr7   colorscaleZallowed_backendsfeZbin_datar   r   r   latex_to_png<   s6   

r?   c              
   C   s  zddl m}m}m} ddlm} ddlm} W n
 ty"   Y d S w | 	dd} |r0d
| } zJ|jdd	}	d
| }
t }|d}|j| d|	d\}}}}}|j|d |d fd}|jd|| | |	|d || |j||
ddd | W S  tt|fy   Y d S w )Nr   )figurefont_managermathtext)backend_agg)ParseFatalExceptionz$$$z${0}$   )sizex   pathH   )dpiprop)Zfigsize)Zfontpropertiesr;   ZpngT)rK   r4   transparent)r   r@   rA   rB   Zmatplotlib.backendsrC   	pyparsingrD   ImportErrorreplacer4   ZFontPropertiesr   ZMathTextParserparseZFiguretextZFigureCanvasAggZsavefiggetvaluer8   RuntimeError)r"   r7   r;   r<   r@   rA   rB   rC   rD   rL   rK   bufferparserwidthheightdepth_Zfigr   r   r   r0   o   s0   



r0   c                 C   s  z
t d t d W n
 ty   Y d S w d }tjdkr(t }| jtjO  _zztt	
 }d}d}d}||jddd	}	|	t| | W d    n1 sSw   Y  tjdd
dd|g|tjtj|d td| }
tjddddt|
ddddd||d|g|tjtj|d ||d}	|	 W  d    W W t| S 1 sw   Y  W n tjy   Y W t| d S w W t| d S t| w )Nlatexr   ntztmp.texztmp.dviztmp.pngwutf8)encodingz-halt-on-errorz-interactionZ	batchmode)cwdstdoutstderrstartupinfo   z-TZtightz-Dz-z9z-bgZTransparentz-oz-fgrb)r   r   osname
subprocessZSTARTUPINFOZdwFlagsZSTARTF_USESHOWWINDOWr   tempfilemkdtempjoinpathr   
writelines	genelatex
check_callDEVNULLroundr'   readshutilrmtreeCalledProcessError)r"   r7   r;   r<   rc   ZworkdirZtmpfileZdvifileoutfiler=   
resolutionr   r   r   r1      st   
r1   c                 C   sT   zt d tjd| gtjtjd}| \}}| ddW S  ty)   Y dS w )z5Invoke kpsewhich command with an argument `filename`.	kpsewhich)ra   rb   r^   rP   N)r   ri   PopenPIPEcommunicatestripdecoder   )filenameprocra   rb   r   r   r   rx      s   rx   c                 c   s    t  }|o|jotd}dV  |j}|r|dg }|D ]}d|V  qdV  |jr0|jV  dV  |r?dV  | V  dV  n|rHd	| 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   r/   r   rx   r   r4   r   )bodyr7   ltr   r   packr   r   r   rn      s*   

rn   z-<img src="data:image/png;base64,%s" alt=%s />imagec                 C   s&   t | ddd}|rt||f 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)r9   asciiN)r?   r}   _data_uri_template_png)r"   ZaltZbase64_datar   r   r   latex_to_html   s   
r   )FNFr    r!   )r    r!   )r   )!r   ior   r   rg   rj   rs   ri   base64r   r6   pathlibr   ZIPython.utils.processr   r   Ztraitlets.configr	   Ztraitlets.config.configurabler
   Z	traitletsr   r   r   r   r'   r?   r0   r1   rx   rn   r   r   r   r   r   r   <module>   s0   &

3
>