Skip to content

Commit 4a3e41b

Browse files
authored
Merge pull request #299 from gustaphe/renderlog
Informative error messages for failed renders
2 parents c93bc3d + b327d73 commit 4a3e41b

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/Latexify.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using Markdown
1313
using MacroTools: postwalk
1414
import MacroTools
1515
using Format
16+
import Base.showerror
1617

1718
export latexify, md, copy_to_clipboard, auto_display, set_default, get_default,
1819
reset_default, @latexrecipe, render, @latexify, @latexrun, @latexdefine

src/utils.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ function _compile(s::LaTeXString, cmd::Cmd, ext::String;
3636
open=true,
3737
kw...
3838
)
39+
name = abspath(name)
3940
mktempdir() do source_dir
4041
cd(source_dir) do
4142
_writetex(s; name="main", kw...)
4243
debug || (cmd = pipeline(cmd, devnull))
43-
run(cmd)
44+
try
45+
run(cmd)
46+
catch err
47+
isa(err, ProcessFailedException) || rethrow(err)
48+
mv("$source_dir/main.log", "$name.log"; force=true)
49+
rethrow(LatexifyRenderError("$name.log"))
50+
end
4451
end
4552
mv("$source_dir/main.$ext", "$name.$ext"; force=true)
4653
end
@@ -226,3 +233,23 @@ end
226233

227234
_packagename(x::AbstractString) = "{$x}"
228235
_packagename(x::Tuple) = "[$(join(x[2:end], ", "))]{$(first(x))}"
236+
237+
struct LatexifyRenderError <: Exception
238+
logfilename::String
239+
end
240+
function Base.showerror(io::IO, e::LatexifyRenderError)
241+
isfile(e.logfilename) || return println(io, "an error occured while rendering LaTeX, no log file available.")
242+
println(io, "an error occured while rendering LaTeX: ")
243+
secondline = false
244+
for l = eachline(e.logfilename)
245+
if secondline
246+
println(io, "\t", l)
247+
break;
248+
end
249+
m = match(r"^! (.*)$", l)
250+
isnothing(m) && continue
251+
println(io, "\t", m[1])
252+
secondline = true
253+
end
254+
println(io, "Check the log file at ", e.logfilename, " for more information")
255+
end

test/utils_test.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,29 @@ tex = read(filename, String)
7474

7575
@test occursin("MathJax", Latexify.html_wrap(latexify(:(sin(α)))))
7676
@test Latexify.best_displayable() isa MIME
77+
78+
#@test_throws Latexify.LatexifyRenderError render(L"x^2^3") # Does not run on Windows and Mac CI
79+
logfile = tempname()
80+
open(logfile, "w") do io
81+
println(io, raw"""
82+
This is LuaHBTeX, Version 1.18.0 (TeX Live 2024/Arch Linux) (format=lualatex 2024.4.3) 7 AUG 2024 14:19
83+
restricted system commands enabled.
84+
** Skipping many files **
85+
LaTeX Font Info: Trying to load font information for U+msb on input line 5.
86+
(/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd
87+
File: umsb.fd 2013/01/14 v3.01 AMS symbols B
88+
)
89+
! Double superscript.
90+
l.8 $x^2^
91+
3$
92+
I treat `x^1^2' essentially like `x^1{}^2'.
93+
** More lines skipped **
94+
""")
95+
end
96+
e = Latexify.LatexifyRenderError(logfile)
97+
@test sprint(showerror, e) == """
98+
an error occured while rendering LaTeX: \n\tDouble superscript.
99+
\tl.8 \$x^2^
100+
Check the log file at $logfile for more information
101+
"""
102+

0 commit comments

Comments
 (0)