MCPcopy Create free account
hub / github.com/ipython/ipython / latex_to_png_dvipng

Function latex_to_png_dvipng

IPython/lib/latextools.py:132–163  ·  view source on GitHub ↗
(s, wrap, color='Black', scale=1.0)

Source from the content-addressed store, hash-verified

130
131
132def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):
133 try:
134 find_cmd('latex')
135 find_cmd('dvipng')
136 except FindCmdError:
137 return None
138 try:
139 workdir = tempfile.mkdtemp()
140 tmpfile = os.path.join(workdir, "tmp.tex")
141 dvifile = os.path.join(workdir, "tmp.dvi")
142 outfile = os.path.join(workdir, "tmp.png")
143
144 with open(tmpfile, "w", encoding='utf8') as f:
145 f.writelines(genelatex(s, wrap))
146
147 with open(os.devnull, 'wb') as devnull:
148 subprocess.check_call(
149 ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile],
150 cwd=workdir, stdout=devnull, stderr=devnull)
151
152 resolution = round(150*scale)
153 subprocess.check_call(
154 ["dvipng", "-T", "tight", "-D", str(resolution), "-z", "9",
155 "-bg", "transparent", "-o", outfile, dvifile, "-fg", color],
156 cwd=workdir, stdout=devnull, stderr=devnull)
157
158 with open(outfile, "rb") as f:
159 return f.read()
160 except subprocess.CalledProcessError:
161 return None
162 finally:
163 shutil.rmtree(workdir)
164
165
166def kpsewhich(filename):

Callers

nothing calls this directly

Calls 4

find_cmdFunction · 0.90
genelatexFunction · 0.85
writelinesMethod · 0.80
readMethod · 0.80

Tested by

no test coverage detected