Browse Source

fixes

pull/30/head
sidthekid402 2 years ago
parent
commit
624e0fa9f9
4 changed files with 84 additions and 32 deletions
  1. +1
    -0
      .#.gitignore
  2. +3
    -0
      .gitignore
  3. +78
    -31
      src/TikzPictures.jl
  4. +2
    -1
      test/runtests.jl

+ 1
- 0
.#.gitignore View File

@ -0,0 +1 @@
user@users-mbp-2.attlocal.net.45748

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
.DS_Store
*.svg
*.pdf

+ 78
- 31
src/TikzPictures.jl View File

@ -182,23 +182,45 @@ function latexerrormsg(s)
end
function save(f::PDF, tp::TikzPicture)
# Generate the .tex file and make pass along any possible errors
foldername = dirname(f.filename)
if isempty(foldername)
foldername = "."
# PDF's are intermediate files for SVG's, so if this function
# is being called inside the save(f::SVG, tp::TikzPicture) function,
# the f.filename string would have a temp directory path attached to it
# ex - tmp3208yfe/test
ind = findfirst(isequal('/'), f.filename)
if typeof(ind) == Int64
foldername = f.filename[1:ind-1]
f.filename = f.filename[ind + 1 : end]
else
foldername = dirname(f.filename)
if isempty(foldername)
foldername = "."
end
end
# latex command will only work if in the same directory as the .tex files
# switching directories (temporarily) since the .tex files are in the tmp dir
tempdir = mktempdir(foldername)
pwd = abspath(".")
cd(abspath(tempdir))
# Generate the .tex file and make pass along any possible errors
save(TEX(f.filename * ".tex"), tp) # Save the tex file in the directory that was given
# This will throw an error if the directory doesn't exist
# From the .tex file, generate a pdf within the specified folder
latexCommand = ``
if tp.enableWrite18
latexCommand = `$(tikzCommand()) --enable-write18 --output-directory=$(foldername) $(f.filename)`
latexCommand = `$(tikzCommand()) --enable-write18 --output-directory=$(".") $(f.filename)`
else
latexCommand = `$(tikzCommand()) --output-directory=$(foldername) $(f.filename)`
latexCommand = `$(tikzCommand()) --output-directory=$(".") $(f.filename)`
end
latexSuccess = success(latexCommand)
log = read(string(f.filename, ".log"), String)
# switch back to original directory
cd(pwd)
log = read(tempdir * "/" * f.filename * ".log", String)
if !latexSuccess
if !standaloneWorkaround() && contains(log, "\\sa@placebox ->\\newpage \\global \\pdfpagewidth")
@ -218,9 +240,9 @@ function save(f::PDF, tp::TikzPicture)
# Shouldn't need to be try-catched anymore, but best to be safe
# This failing is NOT critical either, so just make it a warning
if tikzDeleteIntermediate()
rm("$(f.filename).tex")
rm("$(f.filename).aux")
rm("$(f.filename).log")
# Moves pdf out of temp directory and removes temp directory
mv(tempdir * "/" * "$(f.filename).pdf", foldername * "/" * "$(f.filename).pdf")
rm(tempdir, recursive=true)
end
catch
warn("TikzPictures: Your intermediate files are not being deleted.")
@ -228,25 +250,41 @@ function save(f::PDF, tp::TikzPicture)
end
function save(f::PDF, td::TikzDocument)
foldername = dirname(f.filename)
if isempty(foldername)
foldername = "."
end
if isempty(td.pictures)
error("TikzDocument does not contain pictures")
# PDF's are intermediate files for SVG's, so if this function
# is being called inside the save(f::SVG, tp::TikzPicture) function,
# the f.filename string would have a temp directory path attached to it
# ex - tmp3208yfe/test
ind = findfirst(isequal('/'), f.filename)
if typeof(ind) == Int64
foldername = f.filename[1:ind-1]
f.filename = f.filename[ind + 1 : end]
else
foldername = dirname(f.filename)
if isempty(foldername)
foldername = "."
end
end
# lualatex command (tikzCommand()) will only work if in the same directory as the .tex files
# switching directories (temporarily) since the .tex files are in the tmp dir
tempdir = mktempdir(foldername)
pwd = abspath(".")
cd(abspath(tempdir))
try
save(TEX(f.filename * ".tex"), td)
if td.pictures[1].enableWrite18
success(`$(tikzCommand()) --enable-write18 --output-directory=$(foldername) $(f.filename)`)
success(`$(tikzCommand()) --enable-write18 --output-directory=$(".") $(f.filename)`)
else
success(`$(tikzCommand()) --output-directory=$(foldername) $(f.filename)`)
success(`$(tikzCommand()) --output-directory=$(".") $(f.filename)`)
end
# switch back to original directory
cd(pwd)
if tikzDeleteIntermediate()
rm("$(f.filename).tex")
rm("$(f.filename).aux")
rm("$(f.filename).log")
# Moves pdf out of temp directory and removes temp directory
mv(tempdir * "/" * "$(f.filename).pdf", foldername * "/" * "$(f.filename).pdf")
rm(tempdir, recursive=true)
end
catch
println("Error saving as PDF.")
@ -258,25 +296,34 @@ end
function save(f::SVG, tp::TikzPicture)
try
filename = f.filename
folder = abspath(".")
tempdir = mktempdir(folder)
pwd = abspath(".")
cd(abspath(tempdir))
if tp.usePDF2SVG
save(PDF(filename), tp)
success(`pdf2svg $filename.pdf $filename.svg`) || error("pdf2svg failure")
cd(pwd)
success(`pdf2svg $(tempdir * "/" * filename).pdf $filename.svg`) || error("pdf2svg failure")
if tikzDeleteIntermediate()
rm("$filename.pdf")
# delete tmp dir
rm(tempdir, recursive=true)
end
else
save(TEX("$filename.tex"), tp)
save(TEX("$(filename).tex"), tp)
if tp.enableWrite18
success(`$(tikzCommand()) --enable-write18 --output-format=dvi $filename`)
success(`$(tikzCommand()) --enable-write18 --output-format=dvi $(".")`)
else
success(`$(tikzCommand()) --output-format=dvi $filename`)
success(`$(tikzCommand()) --output-format=dvi $(".")`)
end
success(`dvisvgm --no-fonts $filename`)
success(`dvisvgm --no-fonts $(filename)`)
# switch back to current dir
cd(pwd)
if tikzDeleteIntermediate()
rm("$filename.tex")
rm("$filename.aux")
rm("$filename.dvi")
rm("$filename.log")
# move svg from tmp to current dir and delete tmp dir
mv(tempdir * "/" * "$(filename).svg", foldername * "/" * "$(filename).svg")
rm(tempdir, recursive=true)
end
end
catch


+ 2
- 1
test/runtests.jl View File

@ -3,7 +3,8 @@ using Test
using TikzPictures
tp = TikzPicture("\\draw (0,0) -- (10,10);\n\\draw (10,0) -- (0,10);\n\\node at (5,5) {tikz \$\\sqrt{\\pi}\$};", options="scale=0.25", preamble="")
td = TikzDocument([tp],["hello"])
td = TikzDocument()
push!(td, tp, caption="hello")
save(TEX("testPic"), tp)
if success(`lualatex -v`)
save(PDF("testPic"), tp)


Loading…
Cancel
Save