diff --git a/Project.toml b/Project.toml index d4d9dc06..d8134272 100644 --- a/Project.toml +++ b/Project.toml @@ -30,6 +30,7 @@ julia = "1.2" [extras] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004" +PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/Weave.jl b/src/Weave.jl index ce104b6d..24c94e97 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -23,8 +23,9 @@ end weave_info() = WEAVE_VERSION, string(Date(now())) function __init__() - @require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("plots.jl") - @require Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004" include("gadfly.jl") + @require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("integrations/Plots.jl") + @require Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004" include("integrations/Gadfly.jl") + @require PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" include("integrations/PGFPlotsX.jl") end # utilitity functions diff --git a/src/display_methods.jl b/src/display_methods.jl index 779cb247..245145f6 100644 --- a/src/display_methods.jl +++ b/src/display_methods.jl @@ -32,6 +32,7 @@ const mimetype_ext = Dict( ".pdf" => "application/pdf", ".ps" => "application/postscript", ".tex" => "text/latex", + ".tikz" => "text/tikz", ) function Base.display(report::Report, data) diff --git a/src/gadfly.jl b/src/integrations/Gadfly.jl similarity index 100% rename from src/gadfly.jl rename to src/integrations/Gadfly.jl diff --git a/src/integrations/PGFPlotsX.jl b/src/integrations/PGFPlotsX.jl new file mode 100644 index 00000000..19043f1c --- /dev/null +++ b/src/integrations/PGFPlotsX.jl @@ -0,0 +1,87 @@ +module PGFPlotsXPlots + +using ..Weave, ..PGFPlotsX + +Base.showable(m::MIME"text/latex", plot::PGFPlotsX.AxisLike) = true +Base.showable(m::MIME"text/tikz", plot::PGFPlotsX.AxisLike) = true + +const str_warning = "Could not add LaTeX preamble to document." + +function add_standalone(format::Weave.LaTeXFormat) + str = print_tex(String, "\\usepackage{standalone}") # Adds newline character. + if !contains(format.tex_deps, str) + format.tex_deps *= str + end + return nothing +end + +function add_standalone(format::Weave.LaTeX2PDF) + return add_standalone(format.primaryformat) +end + +function add_standalone(format) + @warn str_warning + return nothing +end + +function add_preamble(format::Weave.LaTeXFormat) + for item in unique([PGFPlotsX.DEFAULT_PREAMBLE; PGFPlotsX.CUSTOM_PREAMBLE]) + str = print_tex(String, item) # Adds newline character. + if !contains(format.tex_deps, str) + format.tex_deps *= str + end + end + return nothing +end + +function add_preamble(format::Weave.LaTeX2PDF) + return add_preamble(format.primaryformat) +end + +function add_preamble(format) + @warn str_warning + return nothing +end + + +function Base.display(report::Weave.Report, m::MIME"text/latex", figure::PGFPlotsX.AxisLike) + + add_standalone(report.format) + + add_preamble(report.format) + + chunk = report.cur_chunk + + ext = chunk.options[:fig_ext] + dpi = chunk.options[:dpi] + + full_name, rel_name = Weave.get_figname(report, chunk, ext = ext) + + pgfsave(full_name, figure; include_preamble = true, dpi = dpi) + + push!(report.figures, rel_name) + report.fignum += 1 + + return full_name +end + +function Base.display(report::Weave.Report, m::MIME"text/tikz", figure::PGFPlotsX.AxisLike) + + add_preamble(report.format) + + chunk = report.cur_chunk + + ext = chunk.options[:fig_ext] + dpi = chunk.options[:dpi] + + full_name, rel_name = Weave.get_figname(report, chunk, ext = ext) + + pgfsave(full_name, figure; include_preamble = false, dpi = dpi) + + push!(report.figures, rel_name) + report.fignum += 1 + + return full_name +end + +end # PGFPlotsXPlots diff --git a/src/plots.jl b/src/integrations/Plots.jl similarity index 100% rename from src/plots.jl rename to src/integrations/Plots.jl diff --git a/src/rendering/texformats.jl b/src/rendering/texformats.jl index 3f059057..7be6f4cd 100644 --- a/src/rendering/texformats.jl +++ b/src/rendering/texformats.jl @@ -96,7 +96,7 @@ function render_figures(docformat::LaTeXFormat, chunk) end for fig in fignames - if splitext(fig)[2] == ".tex" # Tikz figures + if splitext(fig)[2] in [".tex", ".tikz"] # Tikz figures figstring *= "\\resizebox{$width}{!}{\\input{$fig}}\n" else if isempty(attribs)