Beispiel #1
0
def latex_to_png_base64(latex):
    with tempfile.TemporaryDirectory() as tmpdir:
        tmpfile = os.path.join(tmpdir, "tmp.tex")
        dvifile = os.path.join(tmpdir, "tmp.dvi")
        pngfile = os.path.join(tmpdir, "tmp.png")

        contents = list(genelatex(latex, False))
        with open(tmpfile, "w") as f:
            f.writelines(contents)

        with open(os.devnull, 'w') as devnull:
            try:
                subprocess.check_call(["latex", "-halt-on-error", tmpfile],
                                      cwd=tmpdir,
                                      stdout=devnull,
                                      stderr=devnull)
            except Exception as e:  # pragma: no cover
                print("************")
                print(len(contents))
                print('\n'.join(contents))
                raise (e)

            subprocess.check_call([
                "dvipng", "-T", "tight", "-x", "6000", "-z", "9", "-bg",
                "transparent", "-o", pngfile, dvifile
            ],
                                  cwd=tmpdir,
                                  stdout=devnull,
                                  stderr=devnull)

        with open(pngfile, 'rb') as f:
            return base64.b64encode(f.read())
Beispiel #2
0
def latex(content, tmpdir=None):
    content = content.strip() or '.'
    tmpfile = op.join(tmpdir, 'tmp.tex')
    outfile = Path(tmpfile).with_suffix('.dvi')
    lines = list(genelatex(content, False))
    with open(tmpfile, 'w') as f:
        f.writelines(lines)
    cmd('latex -halt-on-error %s', (tmpfile, ), tmpdir=tmpdir)
    return outfile
Beispiel #3
0
def test_genelatex_no_wrap():
    """
    Test genelatex with wrap=False.
    """
    def mock_kpsewhich(filename):
        assert False, ("kpsewhich should not be called "
                       "(called with {0})".format(filename))

    with patch_latextool(mock_kpsewhich):
        assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article}
Beispiel #4
0
def test_genelatex_wrap_without_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is not installed.
    """
    def mock_kpsewhich(filename):
        assert filename == "breqn.sty"
        return None

    with patch_latextool(mock_kpsewhich):
        assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
Beispiel #5
0
def test_genelatex_wrap_with_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is installed.
    """
    def mock_kpsewhich(filename):
        assert filename == "breqn.sty"
        return "path/to/breqn.sty"

    with patch.object(latextools, "kpsewhich", mock_kpsewhich):
        assert '\n'.join(latextools.genelatex(
            "x^2", True)) == r'''\documentclass{article}
Beispiel #6
0
def test_genelatex_wrap_without_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is not installed.
    """
    def mock_kpsewhich(filename):
        nt.assert_equals(filename, "breqn.sty")
        return None

    with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equals(
            '\n'.join(latextools.genelatex("x^2", True)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\pagestyle{empty}
\begin{document}
$$x^2$$
\end{document}''')
Beispiel #7
0
def test_genelatex_no_wrap():
    """
    Test genelatex with wrap=False.
    """
    def mock_kpsewhich(filename):
        assert False, ("kpsewhich should not be called "
                       "(called with {0})".format(filename))

    with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equals(
            '\n'.join(latextools.genelatex("body text", False)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\pagestyle{empty}
\begin{document}
body text
\end{document}''')
Beispiel #8
0
def test_genelatex_no_wrap():
    """
    Test genelatex with wrap=False.
    """
    def mock_kpsewhich(filename):
        assert False, ("kpsewhich should not be called "
                       "(called with {0})".format(filename))

    with patch.object(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equal(
            '\n'.join(latextools.genelatex("body text", False)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\pagestyle{empty}
\begin{document}
body text
\end{document}''')
Beispiel #9
0
def test_genelatex_wrap_without_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is not installed.
    """
    def mock_kpsewhich(filename):
        nt.assert_equal(filename, "breqn.sty")
        return None

    with patch.object(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equal(
            '\n'.join(latextools.genelatex("x^2", True)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\pagestyle{empty}
\begin{document}
$$x^2$$
\end{document}''')
Beispiel #10
0
def test_genelatex_wrap_with_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is installed.
    """
    def mock_kpsewhich(filename):
        nt.assert_equals(filename, "breqn.sty")
        return "path/to/breqn.sty"

    with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equals(
            '\n'.join(latextools.genelatex("x^2", True)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{breqn}
\pagestyle{empty}
\begin{document}
\begin{dmath*}
x^2
\end{dmath*}
\end{document}''')
Beispiel #11
0
def test_genelatex_wrap_with_breqn():
    """
    Test genelatex with wrap=True for the case breqn.sty is installed.
    """
    def mock_kpsewhich(filename):
        nt.assert_equal(filename, "breqn.sty")
        return "path/to/breqn.sty"

    with patch.object(latextools, "kpsewhich", mock_kpsewhich):
        nt.assert_equal(
            '\n'.join(latextools.genelatex("x^2", True)),
            r'''\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{breqn}
\pagestyle{empty}
\begin{document}
\begin{dmath*}
x^2
\end{dmath*}
\end{document}''')
Beispiel #12
0
def latex_to_image(latex, imgpath):
    """Turn latex source code into png image"""

    # tempfile stuff for tex-compiling
    with tempfile.TemporaryDirectory() as tmpdir:
        tmpfile = os.path.join(tmpdir, "tmp.tex")
        dvifile = os.path.join(tmpdir, "tmp.dvi")
        pngfile = os.path.join(tmpdir, "tmp.png")

        contents = list(genelatex(latex, False))
        with open(tmpfile, "w") as f:
            f.writelines(contents)

        with open(os.devnull, 'w') as devnull:
            try:
                subprocess.check_call(["latex", "-halt-on-error", tmpfile],
                                      cwd=tmpdir,
                                      stdout=devnull,
                                      stderr=devnull)
            except Exception:
                print("************")
                print(len(contents))
                print('\n'.join(contents))
                return False

            # create png
            subprocess.check_call([
                "dvipng", "-T", "tight", "-x", "6000", "-z", "9", "-bg",
                "transparent", "-o", pngfile, dvifile
            ],
                                  cwd=tmpdir,
                                  stdout=devnull,
                                  stderr=devnull)

        # copy png from temp folder to current folder
        shutil.copy(pngfile, Path(imgpath).with_suffix('.png'))
        return True