wL idZddlmZddlZddlZddlZddlZddlmZddl Z ddl m Z ddl m Z mZddlmZddlmZdd lmZmZmZGd d eZ dd efd ZddZddZdZdZdZddZy)zTools for handling LaTeX.)BytesION) encodebytes)Path)find_cmd FindCmdError get_config)SingletonConfigurable)ListBoolUnicodeceZdZdZdZeeddgdjdZe dd jdZ egd d jdZ ed jdZ y ) LaTeXToolz3An object to store configuration of the LaTeX tool.ctS)Nr)selfs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/lib/latextools.py_config_defaultzLaTeXTool._config_defaults | matplotlibdvipngaPreferred 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.)amsmathamsthmamssymbbmziA 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__rr r tagbackendsr use_breqnpackagespreamblerrrrs= L(+H   #T#   C  #T#   .F  #T#     #T#  rrsc t|tsJtjj}||d}||vry|dk(rt }n|dk(rt }|jdrt|dk(rZ djdjtj|d dd Dcgc]}tt|d c}}n4td j|td j||||||} |r | r t| } | Scc}w#t$r } td j|| d} ~ wwxYw)alRender 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. Nrrr#zRGB {} zInvalid color specification {}.zNo such backend {0}) isinstancestrrinstancer"latex_to_png_mpllatex_to_png_dvipng startswithlenformatjointextwrapwrapint ValueErrorr) r'encodebackendr9colorscaleallowed_backendsfxebin_datas r latex_to_pngrE<sM. a   ))+44"1%&&,  H     C 5zQ]$OOCHH6>mmE!"Iq6Q6S!c#a*o6S-TUE !!B!I!I%!PQQ.55g>??D%'H (x( O6S!]$%F%M%Me%TU[\\]s*41D"%D  D"D"" E +EE c0 ddlm}m}m}ddlm}ddlm}|jdd}|rdj|} |jd } d |z} t} |jd } | j|d | \} }}}}|j| d z |d z f}|j!d||z || ||j#||j%| | dd| j'S#t$rYywxYw#t(t*|f$rYywxYw)Nr)figure font_managermathtext) backend_agg)ParseFatalExceptionz$$$z${0}$ )sizexpathH)dpiprop)figsize)fontpropertiesr>pngT)rRr6 transparent)rrGrHrImatplotlib.backendsrJ pyparsingrK ImportErrorreplacer6FontPropertiesrMathTextParserparseFiguretextFigureCanvasAggsavefiggetvaluer; RuntimeError)r'r9r>r?rGrHrIrJrKrSrRbufferparserwidthheightdepth_figs rr2r2os$==31 $A OOA ***3Ek((0%+\\!$\%G"vuammURZ"$=m> EFNAd%H##C( FEt D  ) *  &9 :s#C3B4D3 C?>C?DDct tdtdd}tjdk(r7t j }|xj tjzc_ ttj}d}d}d}|j|jdd 5} | jt||dddt jdd d d |g|tj tj | t#d|z} t jddddt%| ddddd||d|g|tj tj | |j|jd5} | j'cdddt)j*|S#t$rYywxYw#1swYxYw#1swYnxYwn.#tj,$rYt)j*ywxYw t)j*|y#t)j*wxYw)Nlatexrntztmp.texztmp.dviztmp.pngwutf8)encodingz-halt-on-errorz -interaction batchmode)cwdstdoutstderr startupinfoz-Ttightz-Dz-z9z-bg Transparentz-oz-fgrb)rrosname subprocess STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWrtempfilemkdtempjoinpathopen writelines genelatex check_callDEVNULLroundr0readshutilrmtreeCalledProcessError) r'r9r>r?rvworkdirtmpfiledvifileoutfilerA resolutions rr3r3s K ww$ ,,. z>>>.x'')*   g & + +C& + A -Q LL1d+ , -  & W M%%%%#  3;' J  %%%%#) .  g & + +D 1 Q668    go  - -F     ( ( g   g gsmF3%AG*GB>GG G3 F?>F?G GGGH H.H HH H7c  tdtjd|gtjtj}|j \}}|j j ddS#t$rYywxYw)z5Invoke kpsewhich command with an argument `filename`. kpsewhich)rtrurpr[N)rr~PopenPIPE communicatestripdecoder)filenameprocrtrus rrrst  ( #??:??< ++-||~$$VY77    sA3A66 BBc#pKtj}|xr|jxr td}d|j}|r|dgz}|D]}dj |d|j r|j d|r d|dn|rd 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)rr1r#rr$r6r%)bodyr9ltrr$packs rrrs    B  cVt|djd}|r t||fzSy)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)rEr_data_uri_template_png)r'alt base64_datas r latex_to_htmlrs3q.55g>K%+s);;;r)FNFBlack?)rr)image) r iorr|rrr~base64rr8pathlibrIPython.utils.processrrtraitlets.configr traitlets.config.configurabler traitletsr r r rr0rEr2r3rrrrr&rrrsw   8'?))"%"LJM0 0f<;|  2N