A community-maintained Python framework for creating mathematical animations.
Merge pull request #483 from cobordism/refactor-tex-templates
Leo Torres899d416adc5d
diff --cc manim/utils/tex_file_writing.pyindex d36b17dd8,cf3399ac9..2b1b4d398--- a/manim/utils/tex_file_writing.py+++ b/manim/utils/tex_file_writing.py@@@ -21,27 -21,58 +21,62 @@@ def tex_hash(expression)return hasher.hexdigest()[:16]- def tex_to_svg_file(expression, source_type, tex_template=None):+ def tex_to_svg_file(expression, environment=None, tex_template=None):+ """Takes a tex expression and returns the svg version of the compiled tex++ Parameters+ ----------+ expression : :class:`str`+ String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``+ environment : Optional[:class:`str`], optional+ The string containing the environment in which the expression should be typeset, e.g. ``align*``+ tex_template : Optional[:class:`~.TexTemplate`], optional+ Template class used to typesetting. If not set, use default template set via `config["tex_template"]`++ Returns+ -------+ :class:`str`+ Path to generated SVG file.+ """if tex_template is None:tex_template = config["tex_template"]- tex_file = generate_tex_file(expression, tex_template, source_type)- dvi_file = tex_to_dvi(tex_file, tex_template.use_ctex, tex_template.tex_compiler)- return dvi_to_svg(- dvi_file, use_ctex=tex_template.use_ctex, tex_compiler=tex_template.tex_compiler+ tex_file = generate_tex_file(expression, environment, tex_template)+ dvi_file = compile_tex(+ tex_file, tex_template.tex_compiler, tex_template.output_format)--- def generate_tex_file(expression, tex_template, source_type):- if source_type == "text":- output = tex_template.get_text_for_text_mode(expression)- elif source_type == "tex":- output = tex_template.get_text_for_tex_mode(expression)+ return convert_to_svg(dvi_file, tex_template.output_format)+++ def generate_tex_file(expression, environment=None, tex_template=None):+ """Takes a tex expression (and an optional tex environment),+ and returns a fully formed tex file ready for compilation.++ Parameters+ ----------+ expression : :class:`str`+ String containing the TeX expression to be rendered, e.g. ``\\sqrt{2}`` or ``foo``+ environment : Optional[:class:`str`], optional+ The string containing the environment in which the expression should be typeset, e.g. ``align*``+ tex_template : Optional[:class:`~.TexTemplate`], optional+ Template class used to typesetting. If not set, use default template set via `config["tex_template"]`++ Returns+ -------+ :class:`str`+ Path to generated TeX file+ """+ if tex_template is None:+ tex_template = config["tex_template"]+ if environment is not None:+ output = tex_template.get_texcode_for_expression_in_env(expression, environment)+ else:+ output = tex_template.get_texcode_for_expression(expression)- result = os.path.join(file_writer_config["tex_dir"], tex_hash(output)) + ".tex"+ tex_dir = file_writer_config["tex_dir"]+ if not os.path.exists(tex_dir):+ os.makedirs(tex_dir)++ result = os.path.join(tex_dir, tex_hash(output)) + ".tex"if not os.path.exists(result):logger.info('Writing "%s" to %s' % ("".join(expression), result))with open(result, "w", encoding="utf-8") as outfile: