Search code examples
julialatexplots.jl

Change which font package Plots.jl is using for latex strings?


I am trying to make a plot with the math symbol:

\m

in the ylabel, which appears in latex as "\mathcal{I}", but this specific symbol cannot be made in Plots.jl. When entering "\mathcal{I}", you instead get:

\m

Here is a MWE:

using Plots
using LaTeXStrings

plot([1],[1],ylabel=L"\mathcal{I}")

Which produces:

enter image description here

The wrong symbol on the y-axis. I have gathered that this is because Plots.jl is using a different font package. Is there any way to change which font package it uses?

I have many many plots, entire libraries really, written using Julia Plots. The convention in my field is to use "\mathcal{I}" and so it would be a great pain for me to change everything in my paper to use "\mathcal{J}" just because this one library cannot print the right symbol.

Any help would be appreciated.

Edit: Thanks to advice from @BallpointBen, I have tried the following code:

default(;fontfamily="Bookman"); plot([1],[1],ylabel=L"\mathcal{I}")

Unfortunately, for me this produces the error:

enter image description here

I should point out that it works fine if one renders empty latex or not latex at all. For example, the following code works:

default(;fontfamily="Bookman"); plot([1],[1],xlabel="",ylabel=L"")

It is only when at least one character in the latex string is asked for (e.g. L"1") that there is a problem. I further realised that the following actually works to get the right symbol:

default(;fontfamily="Times Roman");plot([1],[1],ylabel="𝓘")

Which produces:

enter image description here

Quite a few font families work by just inserting this literal character, but alas, if one tries to include latex as well, one gets the same error. That is, I try

default(;fontfamily="Times Roman"); plot([1],[1],ylabel=string("𝓘",L"1"))

And obtain:

enter image description here

One final idea that does not work is trying to make the 𝓘 character normal text within the latex string as follows:

default(;fontfamily="Times Roman"); plot([1],[1],ylabel=L"\textrm{𝓘}")

Which produces the same output as the first picture that I sent showing the error.

I am looking into solving this error.


Solution

  • When using the GR backend, you can find the list of available fonts here. You can set the default font of Plots.jl using default. You have a lot of fonts to choose from, but one that (sort of) works is Bookman.

    julia> default(; fontfamily="Bookman"); plot([1],[1],ylabel=L"\mathcal{I}")
    

    Or you can set the font directly:

    julia> plot([1],[1], ylabel=L"\mathcal{I}", yguidefont=font("Bookman"))
    

    Result:

    enter image description here

    Unfortunately this results the LateX being pixelated — not sure why.

    Also unfortunate is that the Unicode character U+2110 Script Capital I, which might've been a suitable substitute, is rendered as something like ℐ in basically every font.


    Of course, you do not need to use the GR backend. The PGFPlotsX backend uses LaTeX natively (how? not sure, I think it writes tikx and renders it). You will need to ]add PGFPlotsX.

    julia> pgfplotsx()
    Plots.PGFPlotsXBackend()
    
    julia> plot([1],[1],ylabel=L"\mathcal{I}")
    
    julia> savefig("fig.pdf")
    

    This produces a (high-res) PDF, which I've converted to an image below:

    enter image description here