B
    }d                 @   sv   d Z ddlZddlmZ dZdgZdd Zdd	 Ze Zd
dddddgZ	e
dde	 dZdddZdd ZdS )z,Handle TeX formatting for matplotlib output
    N)whichz(Duncan Macleod <duncan.macleod@ligo.org>z*\def\rtHz{\ensuremath{\sqrt{\mathrm{Hz}}}}c           	   C   sZ   ddl m} m} |ddi* |  }| }|d |j  W dQ R X | | dS )zCDraw (but don't show) a test image using matplotlib and LaTeX.
    r   )pyplot
rc_contextztext.usetexTz\LaTeXN)	Z
matplotlibr   r   ZfigureZgcaZ
set_xlabelZcanvasZdrawclose)r   r   ZfigZax r   Z/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/plot/tex.py_test_usetex#   s    
r   c              C   sB   xdD ]} t | dkrdS qW y
t  W n tk
r<   dS X dS )a'  Returns whether tex is installed on this system

    Checks for ``latex``, ``pdflatex``, and ``dvipng`` on the path, and
    then attemps to draw an image using LaTeX syntax.

    Returns
    -------
    hastex : `bool`
        `True` if the test image is drawn correctly, otherwise `False`
    )latexZpdflatexZdvipngNFT)r   r   	Exception)exer   r   r   has_tex/   s    

r   %\_~&#z(?<!\\)[ z](?!.*{)%.2gc             C   s   | dkrdS ||  }d|kr |S | d\}}t| rFtt|}|d}|drjd|dd  }t|d	krd
| dS | d| dS )al  Convert a floating point number to a latex representation.

    In particular, scientific notation is handled gracefully: e -> 10^

    Parameters
    ----------
    x : `float`
        the number to represent

    format : `str`, optional
        the output string format

    Returns
    -------
    tex : `str`
        a TeX representation of the input

    Examples
    --------
    >>> from gwpy.plot.tex import float_to_latex
    >>> float_to_latex(1)
    '1'
    >>> float_to_latex(2000)
    '2\times 10^{3}'
    >>> float_to_latex(100)
    '10^{2}'
    >>> float_to_latex(-500)
    r'-5\!\!\times\!\!10^{2}'
    g        0ez0+z-0-   Ng      ?z10^{}z\!\!\times\!\!10^{)splitfloat
is_integerintlstrip
startswith)xformatZbase_strZmantissaexponentr   r   r   float_to_latexR   s    

r#   c             C   s   | dkrdS g }d}xPt | D ]B}| \}}| d }|| ||  |d|  |}q W |sn| S || |d  d|S )a  Convert text into a latex-passable representation.

    This method just escapes the following reserved LaTeX characters:
    % \ _ ~ &, whilst trying to avoid doubly-escaping already escaped
    characters

    Parameters
    ----------
    text : `str`
        input text to convert

    Returns
    -------
    tex : `str`
        a modified version of the input text with all unescaped reserved
        latex characters escaped

    Examples
    --------
    >>> from gwpy.plot.tex import label_to_latex
    >>> label_to_latex('normal text')
    'normal text'
    >>> label_to_latex('$1 + 2 = 3$')
    '$1 + 2 = 3$'
    >>> label_to_latex('H1:ABC-DEF_GHI')
    'H1:ABC-DEF\\_GHI'
    >>> label_to_latex('H1:ABC-DEF\_GHI')
    'H1:ABC-DEF\\_GHI'
    Nr   r   r   )re_latex_controlfinditerspangroupappendjoin)textoutr    mabcharr   r   r   label_to_latex   s    r0   )r   )__doc__reshutilr   
__author__ZMACROSr   r   ZHAS_TEXZLATEX_CONTROL_CHARScompiler)   r$   r#   r0   r   r   r   r   <module>   s   
/