Skip to content

Commit

Permalink
fix error messages with tectonic
Browse files Browse the repository at this point in the history
  • Loading branch information
mykelk committed Sep 9, 2021
1 parent 645c2e1 commit f37165a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
71 changes: 45 additions & 26 deletions src/TikzPictures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ extension(f::SaveType) = lowercase(split("$(typeof(f))",".")[end])

resize(tp::TikzPicture) = !isempty(tp.width) || !isempty(tp.height)

# from: https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592
function execute(cmd::Cmd)
out = Pipe()
err = Pipe()
process = run(pipeline(ignorestatus(cmd), stdout=out, stderr=err))
close(out.in)
close(err.in)
(
stdout = String(read(out)),
stderr = String(read(err)),
code = process.exitcode
)
end

function write_adjustbox_options(tex::IO, tp::TikzPicture)
adjustbox_options = []
if !isempty(tp.width)
Expand Down Expand Up @@ -236,20 +250,24 @@ function save(f::TEX, td::TikzDocument)
end

function latexerrormsg(s)
beginError = false
for l in split(s, '\n')
if beginError
if !isempty(l) && l[1] == '?'
return
if '?' in s || '!' in s
beginError = false
for l in split(s, '\n')
if beginError
if !isempty(l) && l[1] == '?'
return
else
println(l)
end
else
println(l)
end
else
if !isempty(l) && l[1] == '!'
println(l)
beginError = true
if !isempty(l) && l[1] == '!'
println(l)
beginError = true
end
end
end
else
println(s)
end
end

Expand All @@ -258,6 +276,7 @@ _joinpath(a, b) = "$a/$b"
function _run(tp::TikzPicture, temp_dir::AbstractString, temp_filename::AbstractString; dvi::Bool=false)
arg = String[tikzCommand()]
latexSuccess = false
texlog = ""
if tikzUseTectonic() || !success(`$(tikzCommand()) -v`)
tectonic() do tectonic_bin
if dvi
Expand All @@ -268,7 +287,9 @@ function _run(tp::TikzPicture, temp_dir::AbstractString, temp_filename::Abstract
push!(arg, "-Zshell-escape")
end
push!(arg, "-o$(temp_dir)")
latexSuccess = success(`$(arg) $(temp_filename*".tex")`)
result = execute(`$(arg) $(temp_filename*".tex")`)
latexSuccess = (result.code == 0)
texlog = result.stderr
end
else
if tp.enableWrite18
Expand All @@ -279,8 +300,13 @@ function _run(tp::TikzPicture, temp_dir::AbstractString, temp_filename::Abstract
end
push!(arg, "--output-directory=$(temp_dir)")
latexSuccess = success(`$(arg) $(temp_filename*".tex")`)
try
texlog = read(temp_filename * ".log", String)
catch
texlog = read(_joinpath(temp_dir,"texput.log"), String)
end
end
return latexSuccess
return latexSuccess, texlog
end

function save(f::PDF, tp::TikzPicture)
Expand All @@ -295,17 +321,10 @@ function save(f::PDF, tp::TikzPicture)

# Save the TEX file in tmp dir
save(TEX(temp_filename * ".tex"), tp)
latexSuccess = _run(tp, temp_dir, temp_filename)

tex_log = ""
try
tex_log = read(temp_filename * ".log", String)
catch
tex_log = read(_joinpath(temp_dir,"texput.log"), String)
end
latexSuccess, texlog = _run(tp, temp_dir, temp_filename)

if occursin("LaTeX Warning: Label(s)", tex_log)
latexSuccess = _run(tp, temp_dir, temp_filename)
if occursin("LaTeX Warning: Label(s)", texlog)
latexSuccess, texlog = _run(tp, temp_dir, temp_filename)
end

# Move PDF out of tmpdir regardless
Expand All @@ -330,12 +349,12 @@ function save(f::PDF, tp::TikzPicture)

if !latexSuccess
# Remove failed attempt.
if !standaloneWorkaround() && occursin("\\sa@placebox ->\\newpage \\global \\pdfpagewidth", tex_log)
if !standaloneWorkaround() && occursin("\\sa@placebox ->\\newpage \\global \\pdfpagewidth", texlog)
standaloneWorkaround(true)
save(f, tp)
return
end
latexerrormsg(tex_log)
latexerrormsg(texlog)
error("LaTeX error")
end
end
Expand All @@ -355,7 +374,7 @@ function save(f::PDF, td::TikzDocument)

try
save(TEX(temp_filename * ".tex"), td)
latexSuccess = _run(td.pictures[1], temp_dir, temp_filename)
latexSuccess, texlog = _run(td.pictures[1], temp_dir, temp_filename)

# Move PDF out of tmpdir regardless
if isfile("$(basefilename).pdf")
Expand Down
9 changes: 4 additions & 5 deletions src/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ _initialize(backend::PopplerBackend) =

# compile a temporary PDF file that can be converted to SVG
function _mkTempPdf(tp::TikzPicture, temp_dir::AbstractString, temp_filename::AbstractString; dvi::Bool=false)
latexSuccess = _run(tp, temp_dir, temp_filename; dvi=dvi)
tex_log = read(temp_filename * ".log", String)
if occursin("LaTeX Warning: Label(s)", tex_log)
latexSuccess = _run(tp, temp_filename, temp_dir, dvi=dvi)
latexSuccess, texlog = _run(tp, temp_dir, temp_filename; dvi=dvi)
if occursin("LaTeX Warning: Label(s)", texlog)
latexSuccess, texlog = _run(tp, temp_filename, temp_dir, dvi=dvi)
end # second run
if !latexSuccess
latexerrormsg(tex_log)
latexerrormsg(texlog)
error("LaTeX error")
end
end
Expand Down

2 comments on commit f37165a

@mykelk
Copy link
Member Author

@mykelk mykelk commented on f37165a Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/44604

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.4.1 -m "<description of version>" f37165a0ada7f96ae2e93567647b3aefb8b3debd
git push origin v3.4.1

Please sign in to comment.