Skip to content

Commit

Permalink
rework latexify (issue 3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Jul 11, 2022
1 parent f61f467 commit c113bdf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 59 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
InspectDR = "d0351b0e-4b05-5898-87b3-e2a8edfddd1d"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925"
Expand All @@ -90,4 +91,4 @@ UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

[targets]
test = ["Colors", "Distributions", "FileIO", "Gaston", "Gtk", "ImageMagick", "Images", "InspectDR", "LibGit2", "OffsetArrays", "PGFPlotsX", "PlotlyJS", "PlotlyBase", "PyPlot", "HDF5", "RDatasets", "StableRNGs", "StaticArrays", "StatsPlots", "Test", "TestImages", "UnicodePlots", "VisualRegressionTests"]
test = ["Colors", "Distributions", "FileIO", "Gaston", "Gtk", "ImageMagick", "Images", "InspectDR", "LaTeXStrings", "LibGit2", "OffsetArrays", "PGFPlotsX", "PlotlyJS", "PlotlyBase", "PyPlot", "HDF5", "RDatasets", "StableRNGs", "StaticArrays", "StatsPlots", "Test", "TestImages", "UnicodePlots", "VisualRegressionTests"]
23 changes: 11 additions & 12 deletions src/backends/pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1060,23 +1060,22 @@ function pgfx_fillrange_args(fillrange, x, y, z)
return PGFPlotsX.Coordinates(x_fill, y_fill, z_fill)
end

sanitize_raw_latex_string(s::AbstractString) =
replace(s, r"\\?\#" => raw"\#") |> s -> replace(s, r"\\?\%" => raw"\%")

pgfx_sanitize_string(p::PlotText) = PlotText(pgfx_sanitize_string(p.str), p.font)
function pgfx_sanitize_string(s::AbstractString)
s = replace(s, r"\\?\#" => "\\#")
s = replace(s, r"\\?\%" => "\\%")
s = replace(s, r"\\?\_" => "\\_")
s = replace(s, r"\\?\&" => "\\&")
s = replace(s, r"\\?\{" => "\\{")
s = replace(s, r"\\?\}" => "\\}")
join(map(c -> isascii(c) ? c : latexify(c), split(s, "")))
map(collect(sanitize_raw_latex_string(s))) do c
if isascii(c) && (isletter(c) || isdigit(c) || isspace(c) || ispunct(c))
c
else
Latexify.latexify(c; parse = false)
end
end |> join
end
@require LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" begin
using .LaTeXStrings
function pgfx_sanitize_string(s::LaTeXString)
s = replace(s, r"\\?\#" => "\\#")
s = replace(s, r"\\?\%" => "\\%")
return LaTeXString(s)
end
pgfx_sanitize_string(s::LaTeXString) = LaTeXString(sanitize_raw_latex_string(s))
end
@require UnitfulRecipes = "42071c24-d89e-48dd-8a24-8a12d9b8861f" begin
using .UnitfulRecipes
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import JSON

using VisualRegressionTests
using RecipesPipeline
using LaTeXStrings
using RecipesBase
using TestImages
using FileIO
Expand Down
105 changes: 59 additions & 46 deletions test/test_pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ with(:pgfplotsx) do
x = ts .* map(cos, ts)
y = (0.1ts) .* map(sin, ts)
z = 1:n
pl = plot(
p = plot(
x,
y,
z,
Expand All @@ -49,7 +49,7 @@ with(:pgfplotsx) do
cbar = true,
w = 5,
)
pgfx_plot = plot!(pl, zeros(n), zeros(n), 1:n, w = 10)
pgfx_plot = plot!(p, zeros(n), zeros(n), 1:n, w = 10)
Plots._update_plot_object(pgfx_plot)
if @test_nowarn(haskey(Plots.pgfx_axes(pgfx_plot.o)[1].options.dict, "colorbar"))
@test Plots.pgfx_axes(pgfx_plot.o)[1]["colorbar"] === nothing
Expand All @@ -67,15 +67,15 @@ with(:pgfplotsx) do
fill = 0,
α = 0.6,
)
pl = scatter!(
p = scatter!(
y,
zcolor = abs.(y .- 0.5),
m = (:hot, 0.8, Plots.stroke(1, :green)),
ms = 10 * abs.(y .- 0.5) .+ 4,
lab = ["grad", "", "ient"],
)
Plots._update_plot_object(pl)
axis = Plots.pgfx_axes(pl.o)[1]
Plots._update_plot_object(p)
axis = Plots.pgfx_axes(p.o)[1]
@test count(x -> x isa PGFPlotsX.LegendEntry, axis.contents) == 6
@test count(x -> x isa PGFPlotsX.Plot, axis.contents) == 108 # each marker is its own plot, fillranges create 2 plot-objects
marker = axis.contents[15]
Expand Down Expand Up @@ -238,7 +238,7 @@ with(:pgfplotsx) do
u = ones(length(x))
v = cos.(x)
arrow_plot = plot(x, y, quiver = (u, v), arrow = true)
# TODO: could adjust limits to fit arrows if too long, but how?
# TODO: could adjust limits to fit arrows if too long, but how ?
# TODO: get latex available on CI
# mktempdir() do path
# @test_nowarn savefig(arrow_plot, path*"arrow.pdf")
Expand Down Expand Up @@ -327,14 +327,14 @@ with(:pgfplotsx) do
end

@testset "Markers and Paths" begin
pl = plot(
p = plot(
5 .- ones(9),
markershape = [:utriangle, :rect],
markersize = 8,
color = [:red, :black],
)
Plots._update_plot_object(pl)
axis = Plots.pgfx_axes(pl.o)[1]
Plots._update_plot_object(p)
axis = Plots.pgfx_axes(p.o)[1]
plots = filter(x -> x isa PGFPlotsX.Plot, axis.contents)
@test length(plots) == 9
end
Expand All @@ -343,86 +343,99 @@ with(:pgfplotsx) do
group = rand(map((i -> begin
"group $(i)"
end), 1:4), 100)
pl = plot(
p = plot(
rand(100),
layout = @layout([a b; c]),
group = group,
linetype = [:bar :scatter :steppre],
linecolor = :match,
)
Plots._update_plot_object(pl)
axis = Plots.pgfx_axes(pl.o)[1]
Plots._update_plot_object(p)
axis = Plots.pgfx_axes(p.o)[1]
legend_entries = filter(x -> x isa PGFPlotsX.LegendEntry, axis.contents)
@test length(legend_entries) == 2
end

@testset "Extra kwargs" begin
pl = plot(1:5, test = "me")
@test pl[1][1].plotattributes[:extra_kwargs][:test] == "me"
pl = plot(1:5, test = "me", extra_kwargs = :subplot)
@test pl[1].attr[:extra_kwargs][:test] == "me"
pl = plot(1:5, test = "me", extra_kwargs = :plot)
@test pl.attr[:extra_plot_kwargs][:test] == "me"
pl = plot(
p = plot(1:5, test = "me")
@test p[1][1].plotattributes[:extra_kwargs][:test] == "me"
p = plot(1:5, test = "me", extra_kwargs = :subplot)
@test p[1].attr[:extra_kwargs][:test] == "me"
p = plot(1:5, test = "me", extra_kwargs = :plot)
@test p.attr[:extra_plot_kwargs][:test] == "me"
p = plot(
1:5,
extra_kwargs = Dict(
:plot => Dict(:test => "me"),
:series => Dict(:and => "me too"),
),
)
@test pl.attr[:extra_plot_kwargs][:test] == "me"
@test pl[1][1].plotattributes[:extra_kwargs][:and] == "me too"
pl = plot(
@test p.attr[:extra_plot_kwargs][:test] == "me"
@test p[1][1].plotattributes[:extra_kwargs][:and] == "me too"
p = plot(
plot(1:5, title = "Line"),
scatter(
1:5,
title = "Scatter",
extra_kwargs = Dict(:subplot => Dict("axis line shift" => "10pt")),
),
)
Plots._update_plot_object(pl)
axes = Plots.pgfx_axes(pl.o)
Plots._update_plot_object(p)
axes = Plots.pgfx_axes(p.o)
@test !haskey(axes[1].options.dict, "axis line shift")
@test haskey(axes[2].options.dict, "axis line shift")
pl = plot(
p = plot(
x -> x,
-1:1;
add = raw"\node at (0,0.5) {\huge hi};",
extra_kwargs = :subplot,
)
@test pl[1][:extra_kwargs] == Dict(:add => raw"\node at (0,0.5) {\huge hi};")
Plots._update_plot_object(pl)
axes = Plots.pgfx_axes(pl.o)
@test p[1][:extra_kwargs] == Dict(:add => raw"\node at (0,0.5) {\huge hi};")
Plots._update_plot_object(p)
axes = Plots.pgfx_axes(p.o)
@test filter(x -> x isa String, axes[1].contents)[1] ==
raw"\node at (0,0.5) {\huge hi};"
plot!(pl)
@test pl[1][:extra_kwargs] == Dict(:add => raw"\node at (0,0.5) {\huge hi};")
Plots._update_plot_object(pl)
axes = Plots.pgfx_axes(pl.o)
plot!(p)
@test p[1][:extra_kwargs] == Dict(:add => raw"\node at (0,0.5) {\huge hi};")
Plots._update_plot_object(p)
axes = Plots.pgfx_axes(p.o)
@test filter(x -> x isa String, axes[1].contents)[1] ==
raw"\node at (0,0.5) {\huge hi};"
end

@testset "Titlefonts" begin
pl = plot(1:5, title = "Test me", titlefont = (2, :left))
@test pl[1][:title] == "Test me"
@test pl[1][:titlefontsize] == 2
@test pl[1][:titlefonthalign] === :left
Plots._update_plot_object(pl)
ax_opt = Plots.pgfx_axes(pl.o)[1].options
p = plot(1:5, title = "Test me", titlefont = (2, :left))
@test p[1][:title] == "Test me"
@test p[1][:titlefontsize] == 2
@test p[1][:titlefonthalign] === :left
Plots._update_plot_object(p)
ax_opt = Plots.pgfx_axes(p.o)[1].options
@test ax_opt["title"] == "Test me"
@test(haskey(ax_opt.dict, "title style")) isa Test.Pass
pl = plot(1:5, plot_title = "Test me", plot_titlefont = (2, :left))
@test pl[:plot_title] == "Test me"
@test pl[:plot_titlefontsize] == 2
@test pl[:plot_titlefonthalign] === :left
pl = heatmap(
p = plot(1:5, plot_title = "Test me", plot_titlefont = (2, :left))
@test p[:plot_title] == "Test me"
@test p[:plot_titlefontsize] == 2
@test p[:plot_titlefonthalign] === :left
p = heatmap(
rand(3, 3),
colorbar_title = "Test me",
colorbar_titlefont = (12, :right),
)
@test pl[1][:colorbar_title] == "Test me"
@test pl[1][:colorbar_titlefontsize] == 12
@test pl[1][:colorbar_titlefonthalign] === :right
@test p[1][:colorbar_title] == "Test me"
@test p[1][:colorbar_titlefontsize] == 12
@test p[1][:colorbar_titlefonthalign] === :right
end

@testset "Latexify - LaTeXStrings" begin
@test Plots.pgfx_sanitize_string("A string, with 2 punctuation chars.") ==
"A string, with 2 punctuation chars."
@test Plots.pgfx_sanitize_string("this is #3") == raw"this is \#3"
@test Plots.pgfx_sanitize_string("10% increase") == raw"10\% increase"
@test Plots.pgfx_sanitize_string("Interpolação polinomial") ==
raw"Interpola$\textrm{\c{c}}$$\textrm{\~{a}}$o polinomial"
@test Plots.pgfx_sanitize_string("∫∞ ∂x") == raw"$\int$$\infty$ $\partial$x"

@test Plots.pgfx_sanitize_string(L"this is #5") == L"$this is \#5$"
@test Plots.pgfx_sanitize_string(L"10% increase") == L"$10\% increase$"
end
end

0 comments on commit c113bdf

Please sign in to comment.