From 070741052f2c1dd3962ad1cb8a278466784cb625 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Fri, 15 Oct 2021 00:31:10 +0200 Subject: [PATCH] fix LaTeXStrings support for UnicodePlots and InspectDR --- Project.toml | 2 ++ src/Plots.jl | 1 + src/backends/inspectdr.jl | 8 ++++---- src/backends/unicodeplots.jl | 8 ++++---- src/utils.jl | 3 +++ test/runtests.jl | 5 +++++ 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 4b31941a..88c12437 100644 --- a/Project.toml +++ b/Project.toml @@ -31,6 +31,7 @@ Showoff = "992d4aef-0814-514b-bc4d-f2e9a6c4116f" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" +UnicodeFun = "1cfade01-22cf-5700-b092-accc4b62d6e1" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] @@ -52,6 +53,7 @@ Requires = "1" Scratch = "1" Showoff = "0.3.1, 1.0" StatsBase = "0.32 - 0.33" +UnicodeFun = "0.4" UnicodePlots = "2.4" julia = "1.5" diff --git a/src/Plots.jl b/src/Plots.jl index cea12a03..69bed9f2 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -35,6 +35,7 @@ import Downloads import JSON using Requires +import UnicodeFun: to_latex #! format: off export diff --git a/src/backends/inspectdr.jl b/src/backends/inspectdr.jl index e5db6d2b..1fe7d7b1 100644 --- a/src/backends/inspectdr.jl +++ b/src/backends/inspectdr.jl @@ -70,7 +70,7 @@ function _inspectdr_add_annotations(plot, x, y, val::PlotText) color = _inspectdr_mapcolor(val.font.color), ) ann = InspectDR.atext( - val.str, + tex2unicode(val.str), x = x, y = y, font = fnt, @@ -384,9 +384,9 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend}) _inspectdr_setticks(sp, plot, strip, xaxis, yaxis) a = plot.annotation - a.title = sp[:title] - a.xlabel = xaxis[:guide] - a.ylabels = [yaxis[:guide]] + a.title = tex2unicode(sp[:title]) + a.xlabel = tex2unicode(xaxis[:guide]) + a.ylabels = [tex2unicode(yaxis[:guide])] #Modify base layout of new object: l = plot.layout.defaults = deepcopy(InspectDR.defaults.plotlayout) diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index a7697c10..0e97f8b2 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -42,9 +42,9 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend}) kw = ( compact = true, - title = sp[:title], - xlabel = xaxis[:guide], - ylabel = yaxis[:guide], + title = tex2unicode(sp[:title]), + xlabel = tex2unicode(xaxis[:guide]), + ylabel = tex2unicode(yaxis[:guide]), xscale = xaxis[:scale], yscale = yaxis[:scale], border = isijulia() ? :ascii : :solid, @@ -63,7 +63,7 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend}) o, x, y, - val.str; + tex2unicode(val.str); color = up_color(val.font.color), halign = val.font.halign, valign = val.font.valign, diff --git a/src/utils.jl b/src/utils.jl index 5f6d3c59..248cb2d2 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1209,6 +1209,7 @@ function mesh3d_triangles(x, y, z, cns::Tuple{Array,Array,Array}) end return X, Y, Z end + function mesh3d_triangles(x, y, z, cns::AbstractVector{NTuple{3,Int}}) X = zeros(eltype(x), 4length(cns)) Y = zeros(eltype(y), 4length(cns)) @@ -1239,3 +1240,5 @@ const _attrsymbolcache = Dict{Symbol,Dict{Symbol,Symbol}}() get_attr_symbol(letter::Symbol, keyword::String) = get_attr_symbol(letter, Symbol(keyword)) get_attr_symbol(letter::Symbol, keyword::Symbol) = _attrsymbolcache[letter][keyword] + +tex2unicode(s::AbstractString, pat=r"\$([^$]+)\$") = replace(s, pat => m->to_latex(m[2:(length(m)-1)])) diff --git a/test/runtests.jl b/test/runtests.jl index a69099bb..af71a2a7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -199,6 +199,11 @@ end @test Plots.process_clims(nothing) == Plots.process_clims(missing) == Plots.process_clims(:auto) + + @test (==)( + Plots.tex2unicode(raw"Equation $y = \\alpha \\cdot x + \β$ and eqn $y = \\sin(x)^2$"), + raw"Equation y = α ⋅ x + β and eqn y = sin(x)²" + ) end @testset "Backends" begin