Skip to content

Commit

Permalink
Merge pull request #59 from mehalter/master
Browse files Browse the repository at this point in the history
Added custom environments to support tikz-cd
  • Loading branch information
mykelk authored Aug 27, 2020
2 parents f586d07 + 77b5dfe commit f75c93b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/TikzPictures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ mutable struct TikzPicture
data::AbstractString
options::AbstractString
preamble::AbstractString
environment::AbstractString
enableWrite18::Bool
TikzPicture(data::AbstractString; options="", preamble="", enableWrite18=true) = new(data, options, preamble, enableWrite18)
TikzPicture(data::AbstractString; options="", preamble="", environment="tikzpicture", enableWrite18=true) = new(data, options, preamble, environment, enableWrite18)
end

mutable struct TikzDocument
Expand Down Expand Up @@ -125,13 +126,13 @@ function save(f::Union{TEX,TIKZ}, tp::TikzPicture)
println(tex, tp.preamble)
println(tex, "\\begin{document}")
end
print(tex, "\\begin{tikzpicture}[")
print(tex, "\\begin{$(tp.environment)}[")
print(tex, tp.options)
println(tex, "]")
end
println(tex, tp.data)
if f.limit_to in [:all, :picture]
println(tex, "\\end{tikzpicture}")
println(tex, "\\end{$(tp.environment)}")
if f.limit_to == :all
println(tex, "\\end{document}")
end
Expand Down Expand Up @@ -159,11 +160,11 @@ function save(f::TEX, td::TikzDocument)
i = 1
for tp in td.pictures
println(tex, "\\centering")
print(tex, "\\begin{tikzpicture}[")
print(tex, "\\begin{$(tp.environment)}[")
print(tex, tp.options)
println(tex, "]")
println(tex, tp.data)
println(tex, "\\end{tikzpicture}")
println(tex, "\\end{$(tp.environment)}")
print(tex, "\\captionof{figure}{")
print(tex, td.captions[i])
println(tex, "}")
Expand Down
36 changes: 31 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ end

# Pre-test cleanup (for repeated tests)
for file in ["testPic.pdf", "testPic.svg", "testDoc.pdf", "testDoc.tex", first.(svgBackends)...]
if isfile(file)
rm(file)
end
if isfile(file)
rm(file)
end
end

# Run tests
Expand All @@ -39,6 +39,7 @@ function has_environment(content::String, environment::String)
error("\\begin{$environment} and \\end{$environment} do not match")
end
end

filecontent = join(readlines("testPic.tex", keep=true)) # read with line breaks
@test occursin(data, filecontent) # also check that the data is contained
@test has_environment(filecontent, "tikzpicture")
Expand All @@ -60,8 +61,8 @@ filecontent = join(readlines("testPic.tex", keep=true))

save(TEX("testPic"), tp) # save again with limit_to=:all
if success(`lualatex -v`)
save(PDF("testPic"), tp)
@test isfile("testPic.pdf")
save(PDF("testPic"), tp)
@test isfile("testPic.pdf")

save(SVG("testPic"), tp)
@test isfile("testPic.svg") # default SVG backend
Expand All @@ -77,3 +78,28 @@ if success(`lualatex -v`)
else
@warn "lualatex is missing; can not test compilation"
end

# Test tikz-cd

data = "A\\arrow{rd}\\arrow{r} & B \\\\& C"
tp = TikzPicture(data, options="scale=0.25", environment="tikzcd", preamble="\\usepackage{tikz-cd}")
td = TikzDocument()
push!(td, tp, caption="hello")

save(TEX("testCD"), tp)
@test isfile("testCD.tex")

filecontent = join(readlines("testCD.tex", keep=true)) # read with line breaks
@test occursin(data, filecontent) # also check that the data is contained
@test has_environment(filecontent, "tikzcd")
@test has_environment(filecontent, "document")

if success(`lualatex -v`)
save(PDF("testCD"), tp)
@test isfile("testCD.pdf")

save(PDF("testCDDoc"), td)
@test isfile("testCDDoc.pdf")
else
@warn "lualatex is missing; can not test compilation"
end

0 comments on commit f75c93b

Please sign in to comment.