Browse Source

support tectonic as fallback compiler

compathelper/new_version/2021-10-12-01-00-34-844-3839746377
Mykel Kochenderfer 2 months ago
parent
commit
5e334504de
  1. 8
      Project.toml
  2. 2
      README.md
  3. 67
      src/TikzPictures.jl
  4. 42
      src/svg.jl
  5. 11
      test/runtests.jl

8
Project.toml

@ -1,18 +1,20 @@
name = "TikzPictures"
uuid = "37f6aa50-8035-52d0-81c2-5a1d08754b2d"
license = "MIT"
version = "3.3.3"
version = "3.4.0"
[deps]
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Poppler_jll = "9c32591e-4766-534b-9725-b71a8799265b"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Tectonic = "9ac5f52a-99c6-489f-af81-462ef484790f"
[compat]
julia = "1"
LaTeXStrings = "1.1"
Poppler_jll = "0.87"
Requires = "1.0"
Requires = "1.1"
Tectonic = "0.6"
julia = "1"
[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

2
README.md

@ -5,7 +5,7 @@
This library allows one to create Tikz pictures and save in various formats. It integrates with IJulia, outputting SVG images to the notebook.
In order to use this library, lualatex must be installed. The texlive and miktex distributions include lualatex. You must also have dvisvgm installed. On Ubuntu, you can get these, if not already present, by running `sudo apt-get install texlive-latex-base` and `sudo apt-get install texlive-binaries`.
This library will try to use the lualatex package already installed on the system. Lualatex may be installed through the texlive and miktex distributions. You should have dvisvgm installed. On Ubuntu, you can get these, if not already present, by running `sudo apt-get install texlive-latex-base` and `sudo apt-get install texlive-binaries`. If the library cannot run lualatex, it will fall back to trying to use tectonic for the compilation.
Note: this package will attempt to turn off interpolation in the generated SVG, but this currently only works in Chrome.

67
src/TikzPictures.jl

@ -1,12 +1,14 @@
module TikzPictures
export TikzPicture, PDF, TEX, TIKZ, SVG, save, tikzDeleteIntermediate, tikzCommand, TikzDocument, push!
export TikzPicture, PDF, TEX, TIKZ, SVG, save, tikzDeleteIntermediate, tikzCommand, tikzUseTectonic, TikzDocument, push!
import Base: push!
import LaTeXStrings: LaTeXString, @L_str
import Tectonic: tectonic
export LaTeXString, @L_str
_tikzDeleteIntermediate = true
_tikzCommand = "lualatex"
_tikzUseTectonic = false
mutable struct TikzPicture
data::AbstractString
@ -71,6 +73,17 @@ function tikzCommand()
_tikzCommand
end
function tikzUseTectonic(value::Bool)
global _tikzUseTectonic
_tikzUseTectonic = value
nothing
end
function tikzUseTectonic()
global _tikzUseTectonic
_tikzUseTectonic
end
function push!(td::TikzDocument, tp::TikzPicture; caption="")
push!(td.pictures, tp)
push!(td.captions, caption)
@ -242,8 +255,39 @@ end
_joinpath(a, b) = "$a/$b"
function save(f::PDF, tp::TikzPicture)
function _run(tp::TikzPicture, temp_dir::AbstractString, temp_filename::AbstractString; dvi::Bool=false)
arg = String[tikzCommand()]
latexSuccess = false
if tikzUseTectonic() || !success(`$(tikzCommand()) -v`)
tectonic() do tectonic_bin
if dvi
error("Tectonic does not currently support dvi backend")
end
arg[1] = tectonic_bin
if tp.enableWrite18
push!(arg, "-Zshell-escape")
end
push!(arg, "-o$(temp_dir)")
latexSuccess = success(`$(arg) $(temp_filename*".tex")`)
end
else
if tp.enableWrite18
push!(arg, "--enable-write18")
end
if dvi
push!(arg, "--output-format=dvi")
end
push!(arg, "--output-directory=$(temp_dir)")
latexSuccess = success(`$(arg) $(temp_filename*".tex")`)
end
if !latexSuccess
latexerrormsg(tex_log)
error("LaTeX error")
end
return latexSuccess
end
function save(f::PDF, tp::TikzPicture)
# Isolate basename and foldername of file
basefilename = basename(f.filename)
working_dir = dirname(abspath(f.filename))
@ -255,16 +299,7 @@ function save(f::PDF, tp::TikzPicture)
# Save the TEX file in tmp dir
save(TEX(temp_filename * ".tex"), tp)
# From the .tex file, generate a pdf within the tmp folder
latexCommand = ``
if tp.enableWrite18
latexCommand = `$(tikzCommand()) --enable-write18 --output-directory=$(temp_dir) $(temp_filename*".tex")`
else
latexCommand = `$(tikzCommand()) --output-directory=$(temp_dir) $(temp_filename*".tex")`
end
latexSuccess = success(latexCommand)
latexSuccess = _run(tp, temp_dir, temp_filename)
tex_log = ""
try
@ -274,7 +309,7 @@ function save(f::PDF, tp::TikzPicture)
end
if occursin("LaTeX Warning: Label(s)", tex_log)
latexSuccess = success(latexCommand)
latexSuccess = _run(tp, temp_dir, temp_filename)
end
# Move PDF out of tmpdir regardless
@ -324,11 +359,7 @@ function save(f::PDF, td::TikzDocument)
try
save(TEX(temp_filename * ".tex"), td)
if td.pictures[1].enableWrite18
success(`$(tikzCommand()) --enable-write18 --output-directory=$(temp_dir) $(temp_filename)`)
else
success(`$(tikzCommand()) --output-directory=$(temp_dir) $(temp_filename)`)
end
latexSuccess = _run(td.pictures[1], temp_dir, temp_filename)
# Move PDF out of tmpdir regardless
if isfile("$(basefilename).pdf")

42
src/svg.jl

@ -20,7 +20,7 @@ end
function __init__svg()
# determine the backend to use
if Sys.which("pdf2svg") != nothing
if Sys.which("pdf2svg") !== nothing
svgBackend(PdfToSvgBackend())
else
try
@ -71,25 +71,31 @@ _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)
latexArgs = String[tikzCommand()]
if tp.enableWrite18
push!(latexArgs, "--enable-write18")
end
if dvi
push!(latexArgs, "--output-format=dvi")
end
latexCommand = `$latexArgs --output-directory=$(temp_dir) $(temp_filename*".tex")`
latexSuccess = success(latexCommand)
_run(tp, temp_dir, temp_filename; dvi=dvi)
# latexArgs = String[tikzCommand()]
# if tp.enableWrite18
# push!(latexArgs, "--enable-write18")
# end
# if dvi
# push!(latexArgs, "--output-format=dvi")
# end
# latexCommand = `$latexArgs --output-directory=$(temp_dir) $(temp_filename*".tex")`
# # latexSuccess = success(latexCommand)
# println("HI2342")
# latexSuccess = _run(tp, temp_filename, temp_dir, dvi=dvi)
# println("HI2342234234")
tex_log = read(temp_filename * ".log", String)
if occursin("LaTeX Warning: Label(s)", tex_log)
latexSuccess = success(latexCommand)
end # second run
# tex_log = read(temp_filename * ".log", String)
# if occursin("LaTeX Warning: Label(s)", tex_log)
# latexSuccess = _run(tp, temp_filename, temp_dir, dvi=dvi)
# end # second run
if !latexSuccess
latexerrormsg(tex_log)
error("LaTeX error")
end
# @show latexSuccess
# if !latexSuccess
# latexerrormsg(tex_log)
# error("LaTeX error")
# end
end
# compile temporary SVGs with different backends

11
test/runtests.jl

@ -69,8 +69,15 @@ if success(`lualatex -v`)
@testset for (k, v) in svgBackends
svgBackend(v)
save(SVG(k), tp)
@test isfile(k)
for usetectonic in [true, false]
if usetectonic && v == DVIBackend()
continue
end
tikzUseTectonic(usetectonic)
save(SVG(k), tp)
@test isfile(k)
rm(k)
end
end
save(PDF("testDoc"), td)

Loading…
Cancel
Save