
    lj,                       d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ d	d
lmZmZ dgZd8dZ	 	 d9d:dZ	 	 d9d:dZd;dZd<d!Zd<d"Zd=d#Zd>d?d)Zd@dAd.ZdBd0Zd1efd2efgZ dCd7Z!dS )DzuInterface for writing, compiling, and converting ``.tex`` files.

.. SEEALSO::

    :mod:`.mobject.svg.tex_mobject`

    )annotationsN)	GeneratorIterableSequence)Path)Match)Any)TexTemplate   )configloggertex_to_svg_file
expressionr	   returnstrc                    t          |           }t          j                    }|                    |                                           |                                d d         S )N   )r   hashlibsha256updateencode	hexdigest)r   id_strhashers      W/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/utils/tex_file_writing.pytex_hashr      sO    __F^F
MM&--//"""crc""    environment
str | Nonetex_templateTexTemplate | Noner   c                2   |t           d         }t          | ||          }|                    d          }|                                r|S t	          ||j        |j                  }t          ||j                  }t           d         st                       |S )a  Takes a tex expression and returns the svg version of the compiled tex

    Parameters
    ----------
    expression
        String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``
    environment
        The string containing the environment in which the expression should be typeset, e.g. ``align*``
    tex_template
        Template class used to typesetting. If not set, use default template set via `config["tex_template"]`

    Returns
    -------
    :class:`Path`
        Path to generated SVG file.
    Nr    .svgno_latex_cleanup)	r   generate_tex_filewith_suffixexistscompile_textex_compileroutput_formatconvert_to_svgdelete_nonsvg_files)r   r   r    tex_filesvg_filedvi_files         r   r   r   #   s    * n- [,GGH ##F++H !" H
 h(BCCH$% Or   c                   |t           d         }||                    | |          }n|                    |           }t          j        d          }|                    dd           |t          |          dz   z  }|                                s0t          j        d| | d           |	                    |d	
           |S )aL  Takes a tex expression (and an optional tex environment),
    and returns a fully formed tex file ready for compilation.

    Parameters
    ----------
    expression
        String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``
    environment
        The string containing the environment in which the expression should be typeset, e.g. ``align*``
    tex_template
        Template class used to typesetting. If not set, use default template set via `config["tex_template"]`

    Returns
    -------
    :class:`Path`
        Path to generated TeX file
    Nr    tex_dirT)parentsexist_ok.texz"Writing %(expression)s to %(path)s)r   pathutf-8encoding)
r   !get_texcode_for_expression_in_envget_texcode_for_expressionget_dirmkdirr   r'   r   info
write_text)r   r   r    outputr1   results         r   r%   r%   L   s    , n-??
KXX88DDnY''GMM$M...((612F==?? 40%&{;;	
 	
 	
 	&7333Mr   r)   r*   r-   r1   	list[str]c                h   | dv r<| dd|dd          dd|                                  |                                  g}nq| dk    rX|d	k    rd
g}n|dk    rg }nt          d          dg|ddd|                                  |                                  }nt          d|  d          |S )aR  Prepares the TeX compilation command, i.e. the TeX compiler name
    and all necessary CLI flags.

    Parameters
    ----------
    tex_compiler
        String containing the compiler to be used, e.g. ``pdflatex`` or ``lualatex``
    output_format
        String containing the output format generated by the compiler, e.g. ``.dvi`` or ``.pdf``
    tex_file
        File name of TeX file to be typeset.
    tex_dir
        Path to the directory where compiler output will be stored.

    Returns
    -------
    :class:`list[str]`
        Compilation command according to given parameters
    >   latexluatexlualatexpdflatexz-interaction=batchmodez-output-format=   Nz-halt-on-errorz-output-directory=xelatexz.xdvz-no-pdf.pdfz#xelatex output is either pdf or xdvzTex compiler z	 unknown.)as_posix
ValueError)r)   r*   r-   r1   commandoutflags         r   make_tex_compilation_commandrN   v   s!   , BBB$1mABB/115!1!1!3!355  ""$
 
	"	"F"" kGGf$$GGBCCC

 %
 	

 6!1!1!3!355
   ""$
 @@@@AAANr   matching
Match[str]Generator[str]c              #     K   t          t          | d         d                    }t          j        |          }d| d| d          dV  dV  d S )NrG   r   z(TexTemplate does not support character 'z' (U+z).zbSee the documentation for manim.mobject.svg.tex_mobject for details on using a custom TexTemplate.)chrintunicodedataname)rO   
code_pointrV   s      r   insight_inputenc_errorrX      sf      S!b))**JJ''D
OT
O
O
O
O
OOOO
nnnnnnr   c              #  B   K   d| d          dV  d| d          dV  d S )NzYou do not have package rG   z installed.zInstall z9 it using your LaTeX package manager, or check for typos. )rO   s    r   insight_package_not_found_errorr[      sB      
=Xa[
=
=
====
[Xa[
[
[
[[[[[[r   c                   |                      |          }t          j        d          }|                                st	          ||| |          }t          j        |t
          j                  }|j        dk    rE|                      d          }t          |||            t          | d|dd          d|           |S )	a  Compiles a tex_file into a .dvi or a .xdv or a .pdf

    Parameters
    ----------
    tex_file
        File name of TeX file to be typeset.
    tex_compiler
        String containing the compiler to be used, e.g. ``pdflatex`` or ``lualatex``
    output_format
        String containing the output format generated by the compiler, e.g. ``.dvi`` or ``.pdf``

    Returns
    -------
    :class:`Path`
        Path to generated output file in desired format (DVI, XDV or PDF).
    r1   stdoutr   z.logz error converting to rG   Nz(. See log output above or the log file: )r&   r   r;   r'   rN   
subprocessrunDEVNULL
returncodeprint_all_tex_errorsrK   )r-   r)   r*   r@   r1   rL   cplog_files           r   r(   r(      s    " !!-00FnY''G==?? .	
 
 ^GJ,>???=A++F33H <BBB - -!!""%- -"*- -  
 Mr   rG   r/   	extensionpagerT   c                v   |                      d          }|                                scdg|dk    rdgng d| ddd|                                 |                                  }t          j        |t          j        	           |                                st          d
| j         d          |S )a  Converts a .dvi, .xdv, or .pdf file into an svg using dvisvgm.

    Parameters
    ----------
    dvi_file
        File name of the input file to be converted.
    extension
        String containing the file extension and thus indicating the file type, e.g. ``.dvi`` or ``.pdf``
    page
        Page to be converted if input file is multi-page.

    Returns
    -------
    :class:`Path`
        Path to generated SVG file.
    r#   dvisvgmrI   z--pdfz--page=z
--no-fontsz--verbosity=0z	--output=r]   z.Your installation does not support converting a   files to SVG. Consider updating dvisvgm to at least version 2.4. If this does not solve the problem, please refer to our troubleshooting guide at: https://docs.manim.community/en/stable/faq/general.html#my-installation-does-not-support-converting-pdf-to-svg-help)r&   r'   rJ   r_   r`   ra   rK   suffix)r/   rf   rg   r@   rL   s        r   r+   r+      s   " !!&))F==?? 
;
$..wiiB
 d
 	

 
 ,))++
   ""$
 	wz'9:::: ==?? 
;X_ ; ; ;
 
 	
 Mr   rZ   additional_endingsIterable[str]Nonec                    t          j        d          }ddh| }|                                D ]}|j        |vr|                                  dS )zDeletes every file that does not have a suffix in ``(".svg", ".tex", *additional_endings)``

    Parameters
    ----------
    additional_endings
        Additional endings to whitelist
    r1   r#   r4   N)r   r;   iterdirrj   unlink)rk   r1   file_suffix_whitelistfs       r   r,   r,     sc     nY''G#VA.@A__  8000HHJJJ r   re   c                   |                                  st          | d          |                     d          5 }|                                }d d d            n# 1 swxY w Y   d t	          |          D             }|rX|                    d          5 }|                                }d d d            n# 1 swxY w Y   |D ]}t          |||           d S d S )NzF failed but did not produce a log file. Check your LaTeX installation.r6   r7   c                B    g | ]\  }}|                     d           |S )!
startswith).0indexlines      r   
<listcomp>z(print_all_tex_errors.<locals>.<listcomp>  s>       %4??SVCWCW  r   )r'   RuntimeErroropen	readlines	enumerateprint_tex_error)re   r)   r-   rr   tex_compilation_logerror_indicestexerror_indexs           r   rc   rc     s   ?? 
 - - -
 
 	
 
	(	( ,Akkmm, , , , , , , , , , , , , , , !*+>!?!?  M  C]]G],, 	 ++--C	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 ( 	C 	CK/cBBBB	C C	C 	Cs#   AA"%A"B;;B?B?z>inputenc Error: Unicode character (?:.*) \(U\+([0-9a-fA-F]+)\)z*LaTeX Error: File `(.*?[clsty])' not foundr   Sequence[str]error_start_index
tex_sourcec                R   t          j        d| |         dd                      t          d | |d          D             d                             d          d                             d          d                   dz
  }|t	          |          k    rd S dg}|d	k     r||d |d	z            z  }d
|d         z   |d<   nV|t	          |          d	z
  k    r|||dz
  d          z  }d
|d         z   |d<   n!|||d	z
  |d	z            z  }d
|d         z   |d<   d                    |          }t          j        |           t          D ]a}|\  }}t          j        |d                    | |                   dd                    }	|	" ||	          D ]}
t          j	        |
           bd S )NzLaTeX compilation error: r   c                <    g | ]}|                     d           |S )zl.rv   )rx   log_lines     r   r{   z#print_tex_error.<locals>.<listcomp>A  s<       &&t,,  r   r    .rG   zContext of error: 
   z->  )
r   errorrT   splitlenjoinLATEX_ERROR_INSIGHTSresearchr=   )r   r   r   line_of_tex_errorcontextcontext_joinedinsightsprobget_insightrO   insights              r   r   r   4  s(   
 LP$78I$J122$NPP   	  34E4F4F G   	
 U3ZZ U3ZZ	
 	
 		  C
OO++t%&G1:5 1A 5566gbk)	S__q0	0	0:/!35566WQZ'

:/!36G!6KKLLgbk)WWW%%N
L   ( % %$k9GG'(9:;;ABB?
 
 &;x00 % %G$$$$% %r   )r   r	   r   r   )NN)r   r   r   r   r    r!   r   r   )
r)   r   r*   r   r-   r   r1   r   r   rA   )rO   rP   r   rQ   )r-   r   r)   r   r*   r   r   r   )rG   )r/   r   rf   r   rg   rT   r   r   )rZ   )rk   rl   r   rm   )re   r   r)   r   r-   r   r   rm   )r   r   r   rT   r   r   r   rm   )"__doc__
__future__r   r   r   r_   rU   collections.abcr   r   r   pathlibr   r   typingr	   manim.utils.texr
   r   r   r   __all__r   r   r%   rN   rX   r[   r(   r+   r,   rc   r   r   rZ   r   r   <module>r      s    # " " " " "  				         9 9 9 9 9 9 9 9 9 9                   ' ' ' ' ' '        
# # # # #'+& & & & &V #'+' ' ' ' 'T0 0 0 0fo o o o\ \ \ \
# # # #L( ( ( ( (V     C C C C( 	J
 	6'	 1% 1% 1% 1% 1% 1%r   