Format: application/x-python-code
⬇ Download File (6.7 KB)
   1  o
   2  
   3  ��h��@s�dZddlmZmZddlZddlZddlZddlZddlm	Z	ddl
   4  Z
   5  ddlmZddl
   6  mZmZddlmZddlmZdd	lmZmZmZGd
   7  d�de�Z	ddefdd�Zddd�Zddd�Zdd�Zdd�ZdZd dd�Z dS)!zTools for handling LaTeX.�)�BytesIO�openN)�encodebytes)�Path)�find_cmd�FindCmdError��
   8  get_config)�SingletonConfigurable)�List�Bool�Unicodec@sreZdZdZdd�Zee�ddgdd�jdd	�Ze	dd
   9  d�jdd	�Z
  10  egd�dd�jdd	�Zed
  11  d�jdd	�ZdS)�	LaTeXToolz3An object to store configuration of the LaTeX tool.cCst�S)Nr)�self�r�Lc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/lib/latextools.py�_config_defaultszLaTeXTool._config_default�
  12  matplotlib�dvipngaPreferred 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.)ZamsmathZamsthmZamssymbZbmziA 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)
  13  �__name__�
  14  __module__�__qualname__�__doc__rrr
  15  �tag�backendsr�	use_breqn�packages�preamblerrrrrs4
  16  �������
  17  �rF�Black��?�sc
  18  
  19  Cs�t|t�sJ�t��j}|dur|d}||vrdS|dkr!t}nL|dkrft}|�d�ret|�dkr^zd�	d�
  20  d	d
  21  �t�|dd�d�D���}Wn#t
  22  y]}zt
  23  d
  24  �	|��|�d}~wwt
  25  d
  26  �	|���nt
  27  d�	|���|||||�}	|r||	r|t|	�}	|	S)alRender a LaTeX string to PNG.
  28  
  29      Parameters
  30      ----------
  31      s : str
  32          The raw string containing valid inline LaTeX.
  33      encode : bool, optional
  34          Should the PNG data base64 encoded to make it JSON'able.
  35      backend : {matplotlib, dvipng}
  36          Backend for producing PNG data.
  37      wrap : bool
  38          If true, Automatically wrap `s` as a LaTeX equation.
  39      color : string
  40          Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB
  41          format, e.g. '#AA20FA'.
  42      scale : float
  43          Scale factor for the resulting PNG.
  44      None is returned when the backend cannot be used.
  45  
  46      Nrrr�#�zRGB {}� cSsg|]	}tt|d���qS)�)�str�int)�.0�xrrr�
  47  <listcomp>asz latex_to_png.<locals>.<listcomp>��zInvalid color specification {}.zNo such backend {0})�
  48  isinstancer'r�instancer�latex_to_png_mpl�latex_to_png_dvipng�
  49  startswith�len�format�join�textwrap�wrap�
  50  ValueErrorr)
  51  r"�encode�backendr7�color�scaleZallowed_backends�f�eZbin_datarrr�latex_to_png<s6
  52  
  53  ����r?c
  54  Cszddlm}m}m}ddlm}ddlm}Wn
  55  ty"YdSw|�	dd�}|r0d�
  56  |�}zJ|jdd	�}	d
  57  |}
  58  t�}|�
  59  d�}|j|d|	d
  60  �\}
  61  }}}}|j|
  62  d|dfd�}|jd||||	|d�|�|�|j||
  63  ddd�|��WStt|fy�YdSw)Nr)�figure�font_manager�mathtext)�backend_agg)�ParseFatalExceptionz$$�$z${0}$�)�size�x�path�H)�dpi�prop)Zfigsize)Zfontpropertiesr;ZpngT)rKr4�transparent)rr@rArBZmatplotlib.backendsrC�	pyparsingrD�ImportError�replacer4ZFontPropertiesrZMathTextParser�parseZFigure�textZFigureCanvasAggZsavefig�getvaluer8�RuntimeError)r"r7r;r<r@rArBrCrDrLrK�buffer�parser�width�height�depth�_Zfigrrrr0os0�
  64  
  65  
  66  
  67  �r0cCs�z
  68  td�td�Wn
  69  tyYdSwd}tjdkr(t��}|jtjO_z�z�tt	�
  70  ��}d}d}d}|�|�jddd	��}	|	�
  71  t||��Wd�n1sSwYtjdd
  72  dd|g|tjtj|d
  73  �td|�}
  74  tjddddt|
  75  �ddddd||d|g|tjtj|d
  76  �|�|��d��}	|	��Wd�WWt�|�S1s�wYWntjy�YWt�|�dSwWt�|�dSt�|�w)N�latexr�ntztmp.texztmp.dviztmp.png�w�utf8)�encodingz-halt-on-errorz-interactionZ	batchmode)�cwd�stdout�stderr�startupinfo�z-TZtightz-Dz-z�9z-bgZTransparentz-oz-fg�rb)rr�os�name�
  77  subprocess�STARTUPINFO�dwFlags�STARTF_USESHOWWINDOWr�tempfile�mkdtemp�joinpathr�
  78  writelines�	genelatex�
  79  check_call�DEVNULL�roundr'�read�shutil�rmtree�CalledProcessError)r"r7r;r<rcZworkdirZtmpfileZdvifile�outfiler=�
  80  resolutionrrrr1�st�
  81  ��������r1cCsTztd�tjd|gtjtjd�}|��\}}|���dd�WSty)YdSw)z5Invoke kpsewhich command with an argument `filename`.�	kpsewhich)rarbr^rPN)rri�Popen�PIPE�communicate�strip�decoder)�filename�procrarbrrrr{�s��r{ccs��t��}|o
  82  |jo
  83  td�}dV|j}|r|dg}|D]}d�|�VqdV|jr0|jVdV|r?dV|VdVn|rHd	�|�Vn|Vd
  84  VdS)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)rr/rr{rr4r)�bodyr7�ltr�r�packrrrrq�s*�
  85  
  86  rqz-<img src="data:image/png;base64,%s" alt=%s />�imagecCs&t|dd��d�}|rt||fSdS)z�Render LaTeX to HTML with embedded PNG data using data URIs.
  87  
  88      Parameters
  89      ----------
  90      s : str
  91          The raw string containing valid inline LateX.
  92      alt : str
  93          The alt text to use for the HTML.
  94      T)r9�asciiN)r?r��_data_uri_template_png)r"ZaltZbase64_datarrr�
  95  latex_to_html�s
  96  �r�)FNFr r!)r r!)r�)!r�iorrrgrmrvri�base64rr6�pathlibrZIPython.utils.processrrZtraitlets.configr	Ztraitlets.config.configurabler
  97  Z	traitletsrrr
  98  rr'r?r0r1r{rqr�r�rrrr�<module>s0&�
  99  �
 100  3
 101  >
 102