Skip to content

Commit

Permalink
Merge b3d07a6 into d47c694
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobunse committed Aug 10, 2020
2 parents d47c694 + b3d07a6 commit d27cd58
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 29 additions & 19 deletions src/TikzPictures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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)
Expand Down

0 comments on commit d27cd58

Please sign in to comment.