Beispiel #1
0
def tex_to_svg_file(tex_file_content):
    svg_file = os.path.join(
        get_tex_dir(), tex_hash(tex_file_content) + ".svg"
    )
    if not os.path.exists(svg_file):
        # If svg doesn't exist, create it
        tex_to_svg(tex_file_content, svg_file)
    return svg_file
Beispiel #2
0
def generate_tex_file(expression, template_tex_file_body):
    result = os.path.join(
        get_tex_dir(), tex_hash(expression, template_tex_file_body)) + ".tex"
    if not os.path.exists(result):
        print("Writing \"%s\" to %s" % ("".join(expression), result))
        new_body = template_tex_file_body.replace(TEX_TEXT_TO_REPLACE,
                                                  expression)
        with open(result, "w", encoding="utf-8") as outfile:
            outfile.write(new_body)
    return result
Beispiel #3
0
def tex_to_dvi(tex_file):
    result = tex_file.replace(".tex", ".dvi" if not TEX_USE_CTEX else ".xdv")
    if not os.path.exists(result):
        commands = [
            "latex", "-interaction=batchmode", "-halt-on-error",
            "-output-directory=\"{}\"".format(
                get_tex_dir()), "\"{}\"".format(tex_file), ">", os.devnull
        ] if not TEX_USE_CTEX else [
            "xelatex", "-no-pdf", "-interaction=batchmode", "-halt-on-error",
            "-output-directory=\"{}\"".format(
                get_tex_dir()), "\"{}\"".format(tex_file), ">", os.devnull
        ]
        exit_code = os.system(" ".join(commands))
        if exit_code != 0:
            log_file = tex_file.replace(".tex", ".log")
            raise Exception(
                ("Latex error converting to dvi. " if not TEX_USE_CTEX else
                 "Xelatex error converting to xdv. ") +
                "See log output above or the log file: %s" % log_file)
    return result
Beispiel #4
0
def tex_to_svg_file(*args):
    if len(args) == 1:
        tex_file_content = args[0]
        svg_file = os.path.join(get_tex_dir(),
                                tex_hash(tex_file_content) + ".svg")
        if not os.path.exists(svg_file):
            # If svg doesn't exist, create it
            tex_to_svg(tex_file_content, svg_file)
        return svg_file
    else:
        expression, template_tex_file_body = args
        tex_file = generate_tex_file(expression, template_tex_file_body)
        dvi_file = tex_to_dvi(tex_file)
        return dvi_to_svg(dvi_file)