Pgfplotsx with \mathbb

Hi,
I’m trying to put a latex label \mathbb{R} with pgfplotsx() backend.
But I get an error:

using Plots
using LaTeXStrings
pgfplotsx()
plot(rand(10), label=L"\mathcal{R}^{2N}") |> display # works!
plot(rand(10), label=L"\mathbb{R}^{2N}") |> display # not works :(

result:

! Undefined control sequence.
<recently read> \mathbb 
        
l.55 \end{axis}
             
ERROR: The latex command `lualatex jl_v6JARqh6hq.tex` failed
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] savepdf(filename::String, td::PGFPlotsX.TikzDocument; latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Vector{String}, run_count::Int64, tmp::String)
    @ PGFPlotsX ~/.julia/packages/PGFPlotsX/qpAgV/src/tikzdocument.jl:202
  [3] savesvg(filename::String, td::PGFPlotsX.TikzDocument; latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Vector{String}, keep_pdf::Bool)
    @ PGFPlotsX ~/.julia/packages/PGFPlotsX/qpAgV/src/tikzdocument.jl:272
  [4] save(filename::String, td::PGFPlotsX.TikzDocument; include_preamble::Bool, latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Vector{String}, dpi::Int64, showing_ide::Bool)
    @ PGFPlotsX ~/.julia/packages/PGFPlotsX/qpAgV/src/tikzdocument.jl:84
  [5] show(f::IOBuffer, #unused#::MIME{Symbol("image/svg+xml")}, td::PGFPlotsX.TikzDocument)
    @ PGFPlotsX ~/.julia/packages/PGFPlotsX/qpAgV/src/tikzdocument.jl:294
  [6] _show(io::IOBuffer, mime::MIME{Symbol("image/svg+xml")}, plt::Plots.Plot{Plots.PGFPlotsXBackend})
    @ Plots ~/.julia/packages/Plots/gzYVM/src/backends/pgfplotsx.jl:1315
  [7] #invokelatest#2
    @ ./essentials.jl:729 [inlined]
  [8] invokelatest
    @ ./essentials.jl:726 [inlined]
  [9] show
    @ ~/.julia/packages/Plots/gzYVM/src/output.jl:237 [inlined]
 [10] __binrepr(m::MIME{Symbol("image/svg+xml")}, x::Plots.Plot{Plots.PGFPlotsXBackend}, context::Nothing)
    @ Base.Multimedia ./multimedia.jl:159
 [11] display(d::VSCodeServer.InlineDisplay, m::MIME{Symbol("image/svg+xml")}, x::Plots.Plot{Plots.PGFPlotsXBackend})
    @ VSCodeServer ./strings/string.jl:0
 [12] display(d::VSCodeServer.InlineDisplay, mime::String, x::Any)
    @ Base.Multimedia ./multimedia.jl:216
 [13] display(d::VSCodeServer.InlineDisplay, x::Plots.Plot{Plots.PGFPlotsXBackend})
    @ VSCodeServer ~/.vscode-server/extensions/julialang.language-julia-1.38.2/scripts/packages/VSCodeServer/src/display.jl:165
 [14] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:328
 [15] |>(x::Plots.Plot{Plots.PGFPlotsXBackend}, f::typeof(display))
    @ Base ./operators.jl:911
 [16] top-level scope
    @ Untitled-1:5

Can pushPGFPlotsPreamble helps in some way?
Elyasaf

I don’t use Plots.jl but PGFPlotsX.jl directly but I think the preamble should be picked up anyways, try this above the call to plot():

import PGFPlotsX
push!(PGFPlotsX.CUSTOM_PREAMBLE, "\\usepackage{amsfonts}")
2 Likes

Thanks! Works like a magic!

1 Like