diff --git a/src/Latexify.jl b/src/Latexify.jl index 9723d557..943332cc 100644 --- a/src/Latexify.jl +++ b/src/Latexify.jl @@ -13,6 +13,7 @@ using Markdown using MacroTools: postwalk import MacroTools using Format +import Base.showerror export latexify, md, copy_to_clipboard, auto_display, set_default, get_default, reset_default, @latexrecipe, render, @latexify, @latexrun, @latexdefine diff --git a/src/utils.jl b/src/utils.jl index 24b7b8d8..a5d558fd 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -36,11 +36,18 @@ function _compile(s::LaTeXString, cmd::Cmd, ext::String; open=true, kw... ) + name = abspath(name) mktempdir() do source_dir cd(source_dir) do _writetex(s; name="main", kw...) debug || (cmd = pipeline(cmd, devnull)) - run(cmd) + try + run(cmd) + catch err + isa(err, ProcessFailedException) || rethrow(err) + mv("$source_dir/main.log", "$name.log"; force=true) + rethrow(LatexifyRenderError("$name.log")) + end end mv("$source_dir/main.$ext", "$name.$ext"; force=true) end @@ -226,3 +233,23 @@ end _packagename(x::AbstractString) = "{$x}" _packagename(x::Tuple) = "[$(join(x[2:end], ", "))]{$(first(x))}" + +struct LatexifyRenderError <: Exception + logfilename::String +end +function Base.showerror(io::IO, e::LatexifyRenderError) + isfile(e.logfilename) || return println(io, "an error occured while rendering LaTeX, no log file available.") + println(io, "an error occured while rendering LaTeX: ") + secondline = false + for l = eachline(e.logfilename) + if secondline + println(io, "\t", l) + break; + end + m = match(r"^! (.*)$", l) + isnothing(m) && continue + println(io, "\t", m[1]) + secondline = true + end + println(io, "Check the log file at ", e.logfilename, " for more information") +end diff --git a/test/utils_test.jl b/test/utils_test.jl index fcd99974..b1605917 100644 --- a/test/utils_test.jl +++ b/test/utils_test.jl @@ -74,3 +74,29 @@ tex = read(filename, String) @test occursin("MathJax", Latexify.html_wrap(latexify(:(sin(α))))) @test Latexify.best_displayable() isa MIME + +#@test_throws Latexify.LatexifyRenderError render(L"x^2^3") # Does not run on Windows and Mac CI +logfile = tempname() +open(logfile, "w") do io + println(io, raw""" + This is LuaHBTeX, Version 1.18.0 (TeX Live 2024/Arch Linux) (format=lualatex 2024.4.3) 7 AUG 2024 14:19 + restricted system commands enabled. + ** Skipping many files ** + LaTeX Font Info: Trying to load font information for U+msb on input line 5. + (/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd + File: umsb.fd 2013/01/14 v3.01 AMS symbols B + ) + ! Double superscript. + l.8 $x^2^ + 3$ + I treat `x^1^2' essentially like `x^1{}^2'. + ** More lines skipped ** + """) +end +e = Latexify.LatexifyRenderError(logfile) +@test sprint(showerror, e) == """ +an error occured while rendering LaTeX: \n\tDouble superscript. +\tl.8 \$x^2^ +Check the log file at $logfile for more information +""" +