o
    û›hÀ  ã                   @   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   úLc:\Users\shubh\Videos\agent\nova\Lib\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}$é   )Úsizeéx   ÚpathéH   )Ú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  _z z†t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 s©w   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Ú
subprocessÚSTARTUPINFOÚdwFlagsÚ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   r{   Ë   s   þÿr{   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   r{   r   r4   r   )Úbodyr7   Últrƒ   r   Úpackr   r   r   rq   Ø   s*   €

rq   z-<img src="data:image/png;base64,%s" alt=%s />Úimagec                 C   s&   t | dd� d¡}|rt||f S dS )zÚRender 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   rm   rv   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   r{   rq   r‰   rŠ   r   r   r   r   Ú<module>   s0   &ÿ
ÿ
3
>