Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom environments to support tikz-cd #59

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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