From ea686c331b73c31eb6c5b4a32ae0d0f2de8411e9 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Sat, 28 Aug 2021 21:08:03 +0200 Subject: [PATCH] improve axis decorations (#3756) * improve handling of LaTeXStrings in tick labels * respect guidefonthalign --- src/backends/pgfplotsx.jl | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index e63bd9ae..1612e0a8 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -1232,6 +1232,8 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter) opt, string(letter, "label style") => PGFPlotsX.Options( labelpos => nothing, + "at" => string("{(ticklabel cs:", get((left = 0, right = 1), axis[:guidefonthalign], 0.5),")}"), + "anchor" => "near ticklabel", "font" => pgfx_font(axis[:guidefontsize], pgfx_thickness_scaling(sp)), "color" => cstr, "draw opacity" => α, @@ -1279,21 +1281,28 @@ function pgfx_axis!(opt::PGFPlotsX.Options, sp::Subplot, letter) push!(opt, string(letter, "tick") => string("{", join(tick_values, ","), "}")) if axis[:showaxis] && axis[:scale] in (:ln, :log2, :log10) && axis[:ticks] == :auto # wrap the power part of label with } - tick_labels = Vector{String}(undef, length(ticks[2])) + tick_labels = similar(ticks[2]) for (i, label) in enumerate(ticks[2]) base, power = split(label, "^") power = string("{", power, "}") tick_labels[i] = string(base, "^", power) end - push!( - opt, - string(letter, "ticklabels") => - string("{\$", join(tick_labels, "\$,\$"), "\$}"), - ) + if tick_labels isa Vector{String} + push!( + opt, + string(letter, "ticklabels") => + string("{\$", join(tick_labels, "\$,\$"), "\$}"), + ) + elseif tick_labels isa Vector{LaTeXString} + push!( + opt, + string(letter, "ticklabels") => join(tick_labels) + ) + end elseif axis[:showaxis] tick_labels = ispolar(sp) && letter == :x ? [ticks[2][3:end]..., "0", "45"] : ticks[2] - if axis[:formatter] in (:scientific, :auto) + if axis[:formatter] in (:scientific, :auto) && tick_labels isa Vector{String} tick_labels = string.("\$", convert_sci_unicode.(tick_labels), "\$") tick_labels = replace.(tick_labels, Ref("×" => "\\times")) end