import getopt import os import subprocess import sys import tempfile def __build_preamble(): preamble = '\documentclass[a4paper, 12pt]{article}\n' preamble += \ """ \usepackage[unicode]{hyperref} \usepackage{mathtext} \usepackage[T1, T2A]{fontenc} \usepackage[utf8]{inputenc} \usepackage[english, russian]{babel} \\vskip 0.5 cm \pagestyle{empty} """ preamble += "\\begin{document}\n" return preamble def math2png(eqs, outdir, prefix = '', size = 1): # Set the working directory workdir = tempfile.gettempdir() # Get a temporary file fd, texfile = tempfile.mkstemp('.tex', 'eq', workdir, True) # Create the TeX document with os.fdopen(fd, 'w+') as f: f.write(__build_preamble()) for eq in eqs: f.write("$%s$\n" % eq.strip()) f.write('\end{document}') f.close() # Generate the DVI file latexcmd = 'latex -halt-on-error -output-directory %s %s' % (workdir, texfile) _run(latexcmd.split()) # Convert the DVI file to PNG's dvifile = texfile.replace('.tex', '.dvi') outprefix = os.path.join(outdir, prefix) dvicmd = "dvipng -T tight -x %i -z 9 -bg Transparent "\ "-o %s%%d.png %s" % (size * 1000, outprefix, dvifile) _run(dvicmd.split()) def _run(cmd): env = os.environ.copy() env.update(dict( LANG='ru_RU.UTF-8', LC_CTYPE='ru_RU.UTF-8', HOME='/root', )) subprocess.Popen( cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ).communicate()
понедельник, 21 ноября 2016 г.
latex math to png
Подписаться на:
Комментарии к сообщению (Atom)
Комментариев нет:
Отправить комментарий