MCPcopy Index your code
hub / github.com/ipython/ipython / latex_to_png_dvipng

Function latex_to_png_dvipng

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

Source from the content-addressed store, hash-verified

139
140
141def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):
142 try:
143 find_cmd('latex')
144 find_cmd('dvipng')
145 except FindCmdError:
146 return None
147
148 startupinfo = None
149 if os.name == "nt":
150 # prevent popup-windows
151 startupinfo = subprocess.STARTUPINFO()
152 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
153
154 try:
155 workdir = Path(tempfile.mkdtemp())
156 tmpfile = "tmp.tex"
157 dvifile = "tmp.dvi"
158 outfile = "tmp.png"
159
160 with workdir.joinpath(tmpfile).open("w", encoding="utf8") as f:
161 f.writelines(genelatex(s, wrap))
162
163 subprocess.check_call(
164 ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile],
165 cwd=workdir,
166 stdout=subprocess.DEVNULL,
167 stderr=subprocess.DEVNULL,
168 startupinfo=startupinfo,
169 )
170
171 resolution = round(150 * scale)
172 subprocess.check_call(
173 [
174 "dvipng",
175 "-T",
176 "tight",
177 "-D",
178 str(resolution),
179 "-z",
180 "9",
181 "-bg",
182 "Transparent",
183 "-o",
184 outfile,
185 dvifile,
186 "-fg",
187 color,
188 ],
189 cwd=workdir,
190 stdout=subprocess.DEVNULL,
191 stderr=subprocess.DEVNULL,
192 startupinfo=startupinfo,
193 )
194
195 with workdir.joinpath(outfile).open("rb") as f:
196 return f.read()
197 except subprocess.CalledProcessError:
198 return None

Callers

nothing calls this directly

Calls 3

find_cmdFunction · 0.90
genelatexFunction · 0.85
readMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…