Ask Your Question

Revision history [back]

Try this:

def compile_latex(s):
    """
    Compile string `s` using LaTeX, and display a png picture of the output.
    """
    from sage.misc.misc import tmp_dir, graphics_filename
    from sage.misc.latex import _run_latex_, LATEX_HEADER
    import os, shutil

    base_dir = os.path.abspath("")
    png_file = graphics_filename(ext='png')
    png_link = "cell://" + png_file
    tmp = tmp_dir()
    tmp = tmp_dir('sage_viewer')
    tex_file = os.path.join(tmp, "sage.tex")
    tmp_png_file = os.path.join(tmp, "sage.png")

    latex_code = LATEX_HEADER + '\n\\begin{document}\n' + s + '\\end{document}'          
    open(tex_file, 'w').write(latex_code)
    e = _run_latex_(tex_file, png=True)
    if e.find("Error") == -1:
        shutil.copy(tmp_png_file, os.path.join(base_dir, png_file))
    print '<html><img src="%s"></html>'%png_link
    return

Then run it with compile_string('some math: $x=y$. Some more: $x^2 = y^2$.').