rL i;dZddlZddlZddlZddlZddlmZddlZddlm Z ddl Z ddl Z ddl mZmZej eZdddZGdd Zy) a Support for embedded TeX expressions in Matplotlib. Requirements: * LaTeX. * \*Agg backends: dvipng>=1.6. * PS backend: PSfrag, dvips, and Ghostscript>=9.0. * PDF and SVG backends: if LuaTeX is present, it will be used to speed up some post-processing steps, but note that it is not used to parse the TeX string itself (only LaTeX is supported). To enable TeX rendering of all text in your Matplotlib figure, set :rc:`text.usetex` to True. TeX and dvipng/dvips processing results are cached in ~/.matplotlib/tex.cache for reuse between sessions. `TexManager.get_rgba` can also be used to directly obtain raster output as RGBA NumPy arrays. N)Path)TemporaryDirectory)cbookdvireadoptionc&|d|dnd}d||dzS)a$ Output LaTeX code that loads a package (possibly with an option) if it hasn't been loaded yet. LaTeX cannot load twice a package with different options, so this helper can be used to protect against users loading arbitrary packages/options in their custom preamble. []z]\makeatletter\@ifpackageloaded{%(package)s}{}{\usepackage%(option)s{%(package)s}}\makeatotherpackagerr s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/texmanager.py_usepackage_if_not_loadedr's1%0q]bF v.  //c eZdZdZej j ejdZ iZ dZ idddddd d d d d dd ddddddddddddddddddddZ ddddddddddddd Z ejd Zed!Zed"Zed0d$Zed%Zed&Zed'Zed(Zed#d)d*Zed+Zed,Zed1d-Zed2d.Zed0d/Zy#)3 TexManagera Convert strings to dvi files using TeX, caching the results to a directory. The cache directory is called ``tex.cache`` and is located in the directory returned by `.get_cachedir`. Repeated calls to this constructor always return the same instance. z tex.cache)serif sans-serifcursive monospacenew century schoolbookz\renewcommand{\rmdefault}{pnc}bookmanz\renewcommand{\rmdefault}{pbk}timesz\usepackage{mathptmx}palatinoz\usepackage{mathpazo} zapf chanceryz\usepackage{chancery}rcharterz\usepackage{charter}rr r helveticaz\usepackage{helvet} avant gardez\usepackage{avant}courierz\usepackage{courier}rz\usepackage{type1ec}computer modern romancomputer modern sans serifcomputer modern typewriter) rrrrrrrr r!r"r#r$cxt|jjddtj |S)NTparentsexist_ok)r _texcachemkdirobject__new__clss rr,zTexManager.__new__js- S]]!!$!>~~c""rchtjd}t|dk(r|djnd}t|dk(r||jvr|dfSt|dk(r||j vr|j |dfStjddj|jy ) z> JJw !  )+  &"& Zq+j0+> 8A$x!A#"77H 8 td3ww||Hh//rc,|j\}}|S)zU Return a string containing font configuration for the tex preamble. )rS)r. font_preamblecommands rget_font_preamblezTexManager.get_font_preambles "%!C!C!E wrc(tjdS)z>Return a string containing user additions to the tex preamble.ztext.latex.preamble)r3r4r-s rget_custom_preamblezTexManager.get_custom_preambles||122rc"|j\}}d|z}djddddd|dd d d d d |jddtddddtdddddddd|d|ddd |d!|dd"gS)#z;Return the complete TeX source for processing a TeX string.g?rAz\documentclass{article}z=% Pass-through \mathdefault, which is used in non-usetex modez>% to use the default text font but was historically suppressedz% in usetex mode.z \newcommand{\mathdefault}[1]{#1}z\usepackage[utf8]{inputenc}z.\DeclareUnicodeCharacter{2212}{\ensuremath{-}}z3% geometry is loaded before the custom preamble as z<% convert_psfrags relies on a custom preamble to change the z % geometry.z1\usepackage[papersize=72in, margin=1in]{geometry}z?% Use `underscore` package to take care of underscores in text.z?% The [strings] option allows to use underscores in file names. underscorestringsrz;% Custom packages (e.g. newtxtext) may already have loaded z"% textcomp with different options.textcompz\pagestyle{empty}z\begin{document}z>% The empty hbox ensures that a page is printed even for emptyz=% inputs, except when using psfrag which gets confused by it.z;% matplotlibbaselinemarker is used by dviread to detect thez% last line's baseline.z \fontsize{z}{z}%z!\ifdefined\psfrag\else\hbox{}\fi%{ z\end{document})rSr<rnr)r.r`rarjrR baselineskips rrXzTexManager._get_tex_sources"%!C!C!E wh yy & L M /  * = B K  @  # # % N N %l9 E J 1 %j 1  M L J &8*Dc : 0'!C5 $ ;   rc|j||dz}t|j|j||d|S)zv Generate a tex file to render the tex string at a specific font size. Return the file name. .texrUencoding)rhr write_textrX)r.r`ratexfiles rmake_texzTexManager.make_texsI""31F: W   !4!4S(!C*1 ! 3rcwdc tjtj| t j |||n |j tj}tj||S#t$r}td|dd|d}~wtj$rc}tdj|dtj||jd|jjdd dd}~wwxYw) N)r~stderrz*Failed to process string with tex because rz could not be foundz{prog} was not able to process the following string: {tex!r} Here is the full command invocation and its output: {format_command} {exc} unicode_escaperUbackslashreplace)progformat_commandr`exc)r:rEr_pformat_subprocess subprocess check_outputr)STDOUTFileNotFoundError RuntimeErrorCalledProcessErrorformatr[outputdecode)r.rkr`r~reportrs r_run_checked_subprocessz"TexManager._run_checked_subprocesss 5,,W56 ,,COS!((*F& 6 #! /BD 'ADD ct|j||jd}|jst |j 5}t|dj |j||d|jgd||t|dj|t|dj|jd d d d t|St|S#1swYt|SxYw) zm Generate a dvi file containing latex's layout of tex string. Return the file name. z.dvidirfile.texrUrx)latexz-interaction=nonstopmodez--halt-on-errorrr}zfile.dvirwN) rrh with_suffixexistsrparentrzrXrreplacerD)r.r`radvifiletmpdirs rmake_dvizTexManager.make_dviss''X67CCFK~~$7 N6VZ(33''X64J++!"%6,3VZ(009VZ(001D1DV1LM N7|s7| N7|s B C88D c Dt|j|||jd}|js|j ||}t |j 5}ddddt|ddd d |g }ttd d r4tjdjd k7r|jdd|j|||t|d j|dddt|St|S#1swYt|SxYw)zp Generate a png file containing latex's rendering of tex string. Return the file name. z.pngrdvipngz-bg Transparentz-Dz-Ttightz-ozfile.png_called_from_pytestFz1.16r1z --freetype0r}N)rrhrrrrrrDgetattrr3_get_executable_info raw_versioninsertrr)r.r`rarbpngfilerrrPs rmake_pngzTexManager.make_png.ss''Xs;<HHP~~ll31G#7 :6 tSXWdJA C!6>00:FF&PJJq-0++C&+AVZ(009 :7|s7| :7|s $B D  Dc|stjd}|stjd}|j|||f}|jj |}|u|j |||}tj jtjj|j|}|dddddfx|j|<}|S)zReturn the alpha channel.z font.sizez savefig.dpiN) r3r4rX _grey_arraydgetrimageimreadr^r_r<r))r.r`rarbkeyalpharrgbas rget_greyzTexManager.get_greyGs||K0H,,}-C!!#x0#5  $$S) =ll3#6G99##BGGLL$HID,0ArN :C  S !E rc|j|||}tjg|jd}tj j ||dddf<||d<|S)a* Return latex's rendering of the tex string as an RGBA array. Examples -------- >>> texmanager = TexManager() >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!" >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0)) .N).r)rnpemptyshaper3colorsto_rgb)r.r`rarbrgbrrs rget_rgbazTexManager.get_rgbaVsa S(C0xx)%++)q)* ))#.S"1"W W  rc<|jdk(ry|j||}|r|jdnd}tj|d|z5}|\}dddj |j |jz|jfS#1swY9xYw)z-Return width, height and descent of the text.r rrrg?r1HN)striprpoints_to_pixelsrDviwidthheightdescent)r.r`rarendererr dpi_fractiondvipages rget_text_width_height_descentz(TexManager.get_text_width_height_descentgs 99;" ,,sH-8@x004a [["|"3 4 ED zz4;;5t||CC  s BB)N)NN)NNr)__name__ __module__ __qualname____doc__r^r_r<r3 get_cachedirr)rr7r8r9 functools lru_cacher, classmethodr?rSrhrlrnrXr|rrrrrrrrrrr8sV -S--/=ILDN "C4 ) ,  1  +  *  b + , * ,!" !8#$ %&=%& %&='O,#*"!#!(&2&1 K## " "$!$!L00$33" " H  :>2:0    D Drr)rrrYloggingr^pathlibrrtempfilernumpyr matplotlibr3rr getLoggerrr:rrrrrrsT, '%w"26/"yDyDr