Browse Source

Optionally omit the tikzpicture and document environments.

When writing TEX files, this feature supersedes the include_preamble option, which is maintained for compatibility.
pull/56/head
Mirko Bunse 9 months ago
parent
commit
b3d07a66ba
  1. 10
      README.md
  2. 48
      src/TikzPictures.jl

10
README.md

@ -31,3 +31,13 @@ tp = TikzPicture(L"""
\node at (5,5) {tikz $\sqrt{\pi}$};"""
, options="scale=0.25", preamble="")
```
## Embedding TEX files in external documents
Compiling a standalone LaTeX file requires the Tikz code to be wrapped in a `tikzpicture` environment, which again is wrapped in a `document` environment. You can omit these wrappers if you intend to embed the output in a larger document, instead of compiling it as a standalone file.
```julia
save(TEX("test"; limit_to=:all), tp) # the default, save a complete file
save(TEX("test"; limit_to=:picture), tp) # only wrap in a tikzpicture environment
save(TEX("test"; limit_to=:data), tp) # do not wrap the Tikz code, at all
```

48
src/TikzPictures.jl

@ -95,14 +95,15 @@ end
mutable struct TEX <: SaveType
filename::AbstractString
include_preamble::Bool
TEX(filename::AbstractString; include_preamble::Bool=true) = new(removeExtension(filename, ".tex"), include_preamble)
limit_to::Symbol
TEX(filename::AbstractString; include_preamble::Bool=true, limit_to::Symbol=(include_preamble ? :all : :picture)) =
new(removeExtension(filename, ".tex"), limit_to)
end
mutable struct TIKZ <: SaveType
filename::AbstractString
include_preamble::Bool
TIKZ(filename::AbstractString) = new(removeExtension(filename, ".tikz"), false)
limit_to::Symbol
TIKZ(filename::AbstractString) = new(removeExtension(filename, ".tikz"), :all)
end
mutable struct SVG <: SaveType
@ -115,35 +116,44 @@ extension(f::SaveType) = lowercase(split("$(typeof(f))",".")[end])
showable(::MIME"image/svg+xml", tp::TikzPicture) = true
function save(f::Union{TEX,TIKZ}, tp::TikzPicture)
if !in(f.limit_to, [:all, :picture, :data])
throw(ArgumentError("limit_to must be one of :all, :picture, and :data"))
end
filename = f.filename
ext = extension(f)
tex = open("$(filename).$(ext)", "w")
if f.include_preamble
if standaloneWorkaround()
println(tex, "\\RequirePackage{luatex85}")
if f.limit_to in [:all, :picture]
if f.limit_to == :all
if standaloneWorkaround()
println(tex, "\\RequirePackage{luatex85}")
end
println(tex, "\\documentclass[tikz]{standalone}")
println(tex, tp.preamble)
println(tex, "\\begin{document}")
end
println(tex, "\\documentclass[tikz]{standalone}")
println(tex, tp.preamble)
println(tex, "\\begin{document}")
print(tex, "\\begin{tikzpicture}[")
print(tex, tp.options)
println(tex, "]")
end
print(tex, "\\begin{tikzpicture}[")
print(tex, tp.options)
println(tex, "]")
println(tex, tp.data)
println(tex, "\\end{tikzpicture}")
if f.include_preamble
println(tex, "\\end{document}")
if f.limit_to in [:all, :picture]
println(tex, "\\end{tikzpicture}")
if f.limit_to == :all
println(tex, "\\end{document}")
end
end
close(tex)
end
function save(f::TEX, td::TikzDocument)
if isempty(td.pictures)
error("TikzDocument does not contain pictures")
throw(ArgumentError("TikzDocument does not contain pictures"))
elseif !in(f.limit_to, [:all, :picture])
throw(ArgumentError("limit_to must be either :all or :picture"))
end
filename = f.filename
tex = open("$(filename).tex", "w")
if f.include_preamble
if f.limit_to == :all
println(tex, "\\documentclass{article}")
println(tex, "\\usepackage{caption}")
println(tex, "\\usepackage{tikz}")
@ -167,7 +177,7 @@ function save(f::TEX, td::TikzDocument)
println(tex)
i += 1
end
if f.include_preamble
if f.limit_to == :all
println(tex, "\\end{document}")
end
close(tex)

Loading…
Cancel
Save