Skip to content

Commit 2bfc661

Browse files
authored
Enhancement: parse .log file and try to print LaTeX errors if compilation fails (#1030)
* parse and print .log when latex error occurs * black
1 parent 564e89b commit 2bfc661

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

manim/utils/tex_file_writing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ def compile_tex(tex_file, tex_compiler, output_format):
164164
exit_code = os.system(command)
165165
if exit_code != 0:
166166
log_file = tex_file.replace(".tex", ".log")
167+
if not Path(log_file).exists():
168+
raise RuntimeError(
169+
f"{tex_compiler} failed but did not produce a log file. "
170+
"Check your LaTeX installation."
171+
)
172+
with open(log_file, "r") as f:
173+
log = f.readlines()
174+
log_error_pos = [
175+
ind for (ind, line) in enumerate(log) if line.startswith("!")
176+
]
177+
if log_error_pos:
178+
logger.error(f"LaTeX compilation error! {tex_compiler} reports:")
179+
for lineno in log_error_pos:
180+
# search for a line starting with "l." in the next
181+
# few lines past the error; otherwise just print some lines.
182+
printed_lines = 1
183+
for _ in range(10):
184+
if log[lineno + printed_lines].startswith("l."):
185+
break
186+
printed_lines += 1
187+
188+
for line in log[lineno : lineno + printed_lines + 1]:
189+
logger.error(line)
190+
167191
raise ValueError(
168192
f"{tex_compiler} error converting to"
169193
f" {output_format[1:]}. See log output above or"

0 commit comments

Comments
 (0)