)Uc@sHdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z yddl m Z Wn!e k rddl m Z nXddlZddlZddlZddlmZddlmZddljZddlZeZejjdr"dZnd Zd Zd d d YZ dS(s This module supports embedded TeX expressions in matplotlib via dvipng and dvips for the raster and postscript backends. The tex and dvipng/dvips information is cached in ~/.matplotlib/tex.cache for reuse between sessions Requirements: * latex * \*Agg backends: dvipng * PS backend: latex w/ psfrag, dvips, and Ghostscript 8.51 (older versions do not work properly) Backends: * \*Agg * PS * PDF For raster output, you can get RGBA numpy arrays from TeX expressions as follows:: texmanager = TexManager() s = '\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!' Z = self.texmanager.get_rgba(s, size=12, dpi=80, rgb=(1,0,0)) To enable tex rendering of all text in your matplotlib figure, set text.usetex in your matplotlibrc file (http://matplotlib.sf.net/matplotlibrc) or include these two lines in your script:: from matplotlib import rc rc('text', usetex=True) iN(tPopentPIPEtSTDOUT(tmd5(trcParams(tread_pngtwint&t;c Cstddtdtdtdtdtjdk}|j|j}}xl|D]d}|jdrM|j d }t j j d |d t jj|}|t jjd kSqMWt j j d d tS(Nsdvipng -versiontshelltstdintstdouttstderrt close_fdstwin32sdvipng isFound dvipng version %sthelpfuls1.6sNo dvipng was found(RtTrueRRtsystplatformR R t startswithtsplittmpltverbosetreportt distutilstversiont LooseVersiontFalse(tpR R tlineR((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytdvipng_hack_alpha9s t TexManagercBsfeZdZejZed?kr3ejZnej j edZ ej Z ej j e dZej je rejde efIJeje enej jesejend?ZiZiZiZdZd@ZdAZdBZdCZd ZdDZidEd6dFd6dGd6dHd6dId6dJd 6dKd6dLd 6dMd 6dNd!6dOd$6dPd'6dQd6dRd(6dSd)6dTd*6Z d?Z!dUe"gdVeD]Z#d-e#^qZ$d.Z%d?d/Z&d0Z'd1Z(d2Z)d3Z*d4Z+e,j-d5Z.d6Z/d7Z0d8Z1d9Z2d:Z3d;Z4d?d?d<Z5d?d?dWd=Z6d?d>Z7RS(XsZ Convert strings to dvi files using TeX, caching the results to a working dir s .tex.caches tex.cachesmWARNING: found a TeX cache dir in the deprecated location "%s". Moving it to the new default location "%s".itcmrttcmsstcmtttpzcs\usepackage{chancery}tserifs sans-seriftcursivet monospacetpncs\renewcommand{\rmdefault}{pnc}snew century schoolbooktpbks\renewcommand{\rmdefault}{pbk}tbookmantptms\usepackage{mathptmx}ttimestppls\usepackage{mathpazo}tpalatinos zapf chancerytpchs\usepackage{charter}tchartertphvs\usepackage{helvet}t helveticatpags\usepackage{avant}s avant gardetpcrs\usepackage{courier}tcourierscomputer modern romanscomputer modern sans serifscomputer modern typewriterstext.latex.preambletfamilysfont.cCsHtjj|js(tj|jntdj}||jkrS||_n t j j d|dd|_|jg}xg|jD]}||j ddf^qD]\}}xtd|D]}|j|j kr@|j |j}t|||j |jtr<d|||j |jfGHnPqtrd GHqqWt j j d |dt|||j ||jt||d qWd j||_|jd |jd |jd g}|jdkr|j|jd nxd|kr|jdqWdj|}djd|dg|_dS(Ns font.familysOThe %s font family is not compatible with LaTeX. serif will be used by default.RR%t-t_sfont.sfamily: %s, font: %s, info: %ss%$s font is not compatible with usetexsQNo LaTeX-compatible font found for the %s font family in rcParams. Using default.iR!iR&s\usepackage{type1cm}s s\usepackage{textcomp}(tostpathtisdirttexcachetmkdirRtlowert font_familiest font_familyRRRtreplacet font_infotsetattrtDEBUGtappendtgetattrtjoint _fontconfigR%t sans_serifR'R&tremovet_font_preamble(tselftfft fontconfigR@tfont_family_attrtfontt found_fonttcmd((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyt__init__sB   8  $ cCsqdj||jd||jt|p.dg}t|jd}tjj|jt |j S(sU returns a filename based on a hash of the string, fontsize, and dpi R!s%fsutf-8( RGtget_font_configtget_custom_preambletstrtunicodetencodeR9R:R<Rt hexdigest(RLttextfontsizetdpitstbytes((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyt get_basefiles!cCs|jdkr=tg|jD]}|df^q|_ng|jD]#}t||j|krG|^qG}|rtrdG|GHnxN|D]F}trd||j|t|fGHntjt||j| %-10ssDEBUG RE-INIT old fontconfig:sDEBUG fontconfig:N( t _rc_cachetNonetdictt_rc_cache_keysRRDtcopytdeepcopyRHRS(RLtktpartchanged((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyRTs$.   cCs|jS(sU returns a string containing font configuration for the tex preamble (RK(RL((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytget_font_preamblescCsdjtdS(s>returns a string containing user additions to the tex preambles stext.latex.preamble(RGR(RL((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyRUscGsRtjdkr2dtjj|jdg}ng}|j|dj|S(s On windows, changing directories can be complicated by the presence of multiple drives. get_shell_cmd deals with this issue. Rs%sis && (RRR9R:t splitdriveR<textendRG(RLtargstcommand((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyt_get_shell_cmds # c Cs|j||}d|}t|d}|j}idd6dd6j|jd}||}tdrwd }nd }d |j||||d |f} tdr|j| jd n=y|j| Wn)t k r} t j j ddnX|j |S(su Generate a tex file to render the tex string at a specific font size returns the file name s%s.textws{\sffamily %s}s sans-serifs{\ttfamily %s}R's{\rmfamily %s}stext.latex.unicodes-\usepackage{ucs} \usepackage[utf8x]{inputenc}R!s\documentclass{article} %s %s %s \usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry} \pagestyle{empty} \begin{document} \fontsize{%f}{%f}%s \end{document} g?tutf8sbYou are using unicode and latex, but have not enabled the matplotlib 'text.latex.unicode' rcParam.R(R_tfileRUtgetR@RRKtwriteRXtUnicodeEncodeErrorRRRtclose( RLRZR[tbasefilettexfiletfhtcustom_preambletfontcmdtunicode_preambleR]terr((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytmake_texs.           s3MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)ptc Cs|j||}d|}t|d}|j}idd6dd6j|jd}||}tdrwd }nd }d |j||||d |f} tdr|j| jd n=y|j| Wn)t k r} t j j ddnX|j |S(s Generate a tex file to render the tex string at a specific font size. It uses the preview.sty to determin the dimension (width, height, descent) of the output. returns the file name s%s.texRos{\sffamily %s}s sans-serifs{\ttfamily %s}R's{\rmfamily %s}stext.latex.unicodes-\usepackage{ucs} \usepackage[utf8x]{inputenc}R!s\documentclass{article} %s %s %s \usepackage[active,showbox,tightpage]{preview} \usepackage[papersize={72in,72in}, body={70in,70in}, margin={1in,1in}]{geometry} %% we override the default showbox as it is treated as an error and makes %% the exit status not zero \def\showbox#1{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}} \begin{document} \begin{preview} {\fontsize{%f}{%f}%s} \end{preview} \end{document} g?RpsbYou are using unicode and latex, but have not enabled the matplotlib 'text.latex.unicode' rcParam.R(R_RqRURrR@RRKRsRXRtRRRRu( RLRZR[RvRwRxRyRzR{R]R|((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytmake_tex_previews.          c Cstdr|j||S|j||}d|}tsOtjj| r|j||}|d}|jd|j dtjj |d|f}t j j |dtj|}y&t|} | j} | jWntk rd} nXytj|t} Wntk r0t} nX|s>| r[td t|| nt j j | dxgtj|d D]O} | jd rq| jd rqytj| Wqtk rqXqWn|S( sm generates a dvi file containing latex's layout of tex string returns the file name stext.latex.previews%s.dvis.outputscd "%s"s(latex -interaction=nonstopmode %s > "%s"itdebugs No latex error report available.seLaTeX was not able to process the following string: %s Here is the full report generated by LaTeX: t*tdviRZ(Rtmake_dvi_previewR_RDR9R:texistsR}RnR<RRRRtsystemRqtreadRutIOErrortstatRtOSErrorRt RuntimeErrortreprtglobtendswithRJ( RLRZR[RvtdvifileRwtoutfileRmt exit_statusRxRRtfname((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytmake_dviPsJ              cCs|j||}d|}d|}tsRtjj| sRtjj| r|j||}|d}|jd|jdtjj|d|f}t j j |dtj |} y&t |} | j} | jWntk rd} nX| r&td t|| nt j j | dtjj| } t|d d jd j| jxytj|d D]a} | jdrq| jdrq| jdrqytj| Wqtk rqXqWn|S(s generates a dvi file containing latex's layout of tex string. It calls make_tex_preview() method and store the size information (width, height, descent) in a separte file. returns the file name s%s.dvis %s.baselines.outputscd "%s"s(latex -interaction=nonstopmode %s > "%s"iRs No latex error report available.seLaTeX was not able to process the following string: %s Here is the full report generated by LaTeX: s .baselineRot RRRZtbaseline(R_RDR9R:RR~RnR<RRRRRRqRRuRRRRt_re_vboxtsearchtopenRsRGtgroupsRRRJR(RLRZR[RvRt baselinefileRwRRmRRxRtmR((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyRsH        ) c Cs[|j|||}d|}ts8tjj| rW|j||}|d}|jd|jd|tjj|dtjj|d|f}t j j |dtj |} y&t |} | j} | jWntk rd} nX| rtd|| nt j j | dytj|WqWtk rSqWXn|S( so generates a png file containing latex's rendering of tex string returns the filename s%s.pngs.outputscd "%s"sQdvipng -bg Transparent -D %s -T tight -o "%s" "%s" > "%s"iRs!No dvipng error report available.sedvipng was not able to process the following file: %s Here is the full report generated by dvipng: (R_RDR9R:RRRnR<RRRRRRqRRuRRRJR( RLRZR[R\RvtpngfileRRRmRRxR((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytmake_pngs2        c Cs|j||}d|}ts5tjj| r|j||}|d}|jd|jdtjj|dtjj|d|f}t j j |dtj |}t |} |rtd|| jnt j j | jd| jtj|n|S(sw generates a postscript file containing latex's rendering of tex string returns the file name s%s.epsfs.outputscd "%s"sdvips -q -E -o "%s" "%s" > "%s"iRscdvipng was not able to process the flowing file: %s Here is the full report generated by dvipng: (R_RDR9R:RRRnR<RRRRRRqRRRuRJ( RLRZR[RvtpsfileRRRmRRx((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pytmake_pss&     cCsy|j||}t|}xD|D]<}|jdr%g|jdD]}t|^qKSq%Wtd|dS(sw returns a list containing the postscript bounding box for latex's rendering of the tex string s%%BoundingBox:isCould not parse %sN(RRqRRtintR(RLRZR[RtpsRtval((sH/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/texmanager.pyt get_ps_bboxs   +c Cs ||j||f}|jj|}|dkr|j|||}ttjj|j |}t ddk rt d}n't j dkrt t _ nt j }|rd|dddddf}n|dddddf}||j|"s&H