# Nice fonts with Plots, GR and LaTeXStrings

**URL:** https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037
**Category:** Visualization
**Tags:** latex, plots, gr
**Created:** [April 26, 2021, 2:41pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037 "2021-04-26T14:41:09Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 26, 2021, 2:41pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/1 "2021-04-26T14:41:09Z")

</div>

This not a question, just something that might be helpful to others. With Plots 1.12 and GR0.57.4 (at least), and using these options below, one can get a plot with homogeneous fonts (“Computer Modern”) in every element.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/f/8fbd629af1a11bfcf7b524705ec1bf2cba5a1f36.png)

```julia
using Plots
using LaTeXStrings

plot_font = "Computer Modern"
default(fontfamily=plot_font,
        linewidth=2, framestyle=:box, label=nothing, grid=false)
scalefontsizes(1.3)

plot(sort(rand(10)),sort(rand(10)),label="Legend")
plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
plot!(ylabel="Same font as everything")
annotate!(0.2,0.8,text("My note",plot_font,12))

savefig("./plot.pdf")

```

> **Previously**
>
> ```julia
> const plot_font = "Computer Modern"
> const PLOTS_DEFAULTS = Dict(:theme=>:default,
> :fontfamily => plot_font,
> :linewidth => 2,
> :framestyle => :box,
> :label => "",
> :grid => false)
> using Plots
> using LaTeXStrings
> 
> plot(sort(rand(10)),sort(rand(10)),label="Legend")
> plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
> plot!(ylabel="Same font as everything")
> annotate!(0.2,0.8,text("My note",plot_font,12))
> 
> savefig("./plot.pdf")
> 
> ```

Additionally, some basic latex is good to know:

- `\textm` removes the math mode from a latex string.
- `\mathrm` removes the italics (not math mode) from the latex string, such that, for example, the exponent looks nicely placed in a non-italic string.

What I would like sometimes is to change everything to `sffamily` fonts. But that I was unable to do consistently yet.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 26, 2021, 3:24pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/2 "2021-04-26T15:24:29Z")

</div>

```julia
using Unitful, Plots, Latexify, UnitfulRecipes, UnitfulLatexify
default(; # Plots defaults
    fontfamily="Computer modern",
    label="" # only explicit legend entries
    )
set_default(; # Latexify defaults
    #unitformat=:slash # in case you want `m/s`
    )
x = rand(10)u"m"
v = x./(4u"s") .+ 0.3*randn(10)u"m/s"

plot(x, v; xguide="x", yguide="v", unitformat=latexify)
```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 26, 2021, 3:30pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/3 "2021-04-26T15:30:27Z")

</div>

![random](https://global.discourse-cdn.com/julialang/original/3X/9/f/9f059a9ad3f54ddb224b2cf09e09536fb4125729.png)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 26, 2021, 3:35pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/4 "2021-04-26T15:35:44Z")

</div>

Note that it does not produce the correct placement for the exponent of the unit (the difference between using `mathrm` and `textrm`):

that example:

![image](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d58dcc169261d7ca1cee376edf6fe2a3307a32d.png)

using `\mathrm{ms^{-1}}`:

![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/00e36fad155957b385b4ffe6295f56bbb5c46b77.png)

(not that everyone needs to have obsessive compulsive disorder, though, but if you are a developer of that, maybe that is something to improve in the packages).

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 26, 2021, 3:39pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/5 "2021-04-26T15:39:17Z")

</div>

It’s actually `$v\;/\;\mathrm{m}\,\mathrm{s}^{-1}$` which I would consider the more correct behaviour.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 26, 2021, 3:50pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/6 "2021-04-26T15:50:15Z")

</div>

Well, I don’t agree that produced the correct output there. Actually here all look the same, and correspond to what I think is the correct placement:

`ms^{-1}`: ms^{-1}

`\mathrm{ms^{-1}}`: \mathrm{ms^{-1}}

`\mathrm{ms}^{-1}`: \mathrm{ms}^{-1}

`\textrm{ms}^{-1}`: \textrm{ms}^{-1}

(in the plot above the `-1` is clearly moved upwards).

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 26, 2021, 4:06pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/7 "2021-04-26T16:06:00Z")

</div>

Staring at them like this, I’m becoming more and more convinced they are both the wrong height.

Yes, that seems to be a bug in GR’s LaTeX rendering. Syntactically I still think your version is wrong, but I guess I could implement it in UnitfulLatexify as a workaround.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 26, 2021, 4:20pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/8 "2021-04-26T16:20:26Z")

</div>

In latex they all look the same to me:

![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bfc59f0b0a52bb2f66eb84988470f369eaa375b2.png)

thus, it is a rendering issue indeed in GR.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 26, 2021, 4:35pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/9 "2021-04-26T16:35:22Z")

</div>

Anyway, you made me notice that the `default` keyword is properly setting the fonts to all elements (that was not working in some previous version). Thus, the initial example can be made simpler:

```julia
using Plots
using LaTeXStrings

plot_font = "Computer Modern"
default(fontfamily=plot_font,
        linewidth=2, framestyle=:box, label=nothing, grid=false)

plot(sort(rand(10)),sort(rand(10)),label="Legend")
plot!(xlabel=L"\textrm{Standard text}(r) / \mathrm{cm^3}")
plot!(ylabel="Same font as everything")
annotate!(0.2,0.8,text("My note",plot_font,12))

savefig("./plot.pdf")

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 26, 2021, 4:51pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/10 "2021-04-26T16:51:54Z")

</div>

I opened an issue here. It seems like the gull wings are to blame:

[https://github.com/jheinen/GR.jl/issues/402](https://github.com/jheinen/GR.jl/issues/402)

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [May 1, 2021, 3:53pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/11 "2021-05-01T15:53:05Z")

</div>

It would be nice if `text` would inherit the default font from `default` so that the last line could be simply  
`annotate!(0.2,0.8,text("My note",12))`

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 3, 2021, 10:22am UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/12 "2021-05-03T10:22:19Z")

</div>

@lmiq, thanks for this very useful post.  
In case it might be of interest, the following example provides some additional formatting options:

```julia
using LaTeXStrings, Printf, Plots; gr()

plot_font = "Computer Modern";
default(fontfamily=plot_font,framestyle=:box, label=nothing, grid=false, tickfontsize=7)

u = 15 # persistent wind speed in m/s (~30 knot)
st0 = "Significant wave height vs persistent wind speed ("
st1 = @sprintf(": u = %i m/s → ", u)
st2 = @sprintf(" = %.1f m", 0.24*u^2/9.8)
plot(title=L"\textbf{%$st0} \mathbf{H_s \approx 0.24 u^2/g} ) \textit{%$st1} {H_s} \textit{%$st2}", titlefont=font(8,plot_font))
plot!(ylims=(0,30))

```

To produce:

 ![Plots_LaTeXStrings_title_bold_and_italic](https://global.discourse-cdn.com/julialang/original/3X/e/8/e82b773a6e1ffd4166632c826ae521781254a0dc.png)

---

<div class="post-metadata">

### Author: ![jo-fleck](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jo-fleck](https://discourse.julialang.org/u/jo-fleck)
#### Post date: [May 23, 2021, 4:21pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/13 "2021-05-23T16:21:03Z")

</div>

Thanks @rafael.guerra

This is very useful!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 16, 2021, 12:39pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/14 "2021-10-16T12:39:29Z")

</div>

This example ` using LaTeXStrings, Printf, Plots; gr()` worked fine about 6 months ago but it doesn’t seem to work anymore.  
No problem with the `pgfplotsx()` backend though.

Any clues?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 16, 2021, 6:36pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/15 "2021-10-16T18:36:09Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> using LaTeXStrings, Printf, Plots; gr()
> 
> plot_font = "Computer Modern";
> default(fontfamily=plot_font,framestyle=:box, label=nothing, grid=false, tickfontsize=7)
> 
> u = 15 # persistent wind speed in m/s (~30 knot)
> st0 = "Significant wave height vs persistent wind speed ("
> st1 = @sprintf(": u = %i m/s → ", u)
> st2 = @sprintf(" = %.1f m", 0.24*u^2/9.8)
> plot(title=L"\textbf{%$st0} \mathbf{H_s \approx 0.24 u^2/g} ) \textit{%$st1} {H_s} \textit{%$st2}", titlefont=font(8,plot_font))
> plot!(ylims=(0,30))
> 
> ```

The problem is with the unicode arrow in

```julia
julia> st1
": u = 15 m/s → "

```

You can replace that with the actual latex symbol, like:

```julia
julia> v = 15
15

julia> plot(rand(10),title=L"\textit{v = %$v m/s \rightarrow}")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/4/749bf37d9916c14bd8476284c9cf9c98ee4b0b82.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 16, 2021, 6:46pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/16 "2021-10-16T18:46:36Z")

</div>

Well done @lmiq for the detective work.

_ **NB:** _ _still a bit annoying and confusing that_ `gr()` _doesn’t consume the unicode symbol of the right arrow anymore (also in text strings spaces need now to be replaced by ~)_

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 16, 2021, 6:50pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/17 "2021-10-16T18:50:00Z")

</div>

They have been working on the LaTeX parser recently, leading to some regressions. You may want to open an issue there on GR.jl.

---

<div class="post-metadata">

### Author: ![chrisoffner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisoffner/32/31162_2.png) [@chrisoffner](https://discourse.julialang.org/u/chrisoffner)
#### Post date: [February 7, 2022, 10:43am UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/18 "2022-02-07T10:43:37Z")

</div>

Thank you for this. 🙂 Any idea how I can change the font used by `LatexStrings` to _Palladio_? It’s what I use in my thesis, and I’d like to have the plots match that. Setting the font family in `default()` seems to affect `Plots` only, but not the `LatexStrings` labels.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 7, 2022, 11:34am UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/19 "2022-02-07T11:34:23Z")

</div>

This will be difficult to do. Depending on your backend, `Plots` uses `Mathjax` or a homebrew version thereof to render `TeX` strings (it doesn’t actually use the fact that your string is a `LaTeXString`, but rather the fact that `LaTeXString`s include `$$` automatically, so this is a question for `Plots` rather than `LaTeXStrings`).

For the Plotly and PlotlyJS backends, you can use a user-defined config file:  
[https://docs.juliaplots.org/latest/backends/#MathJax](https://docs.juliaplots.org/latest/backends/#MathJax)

which I guess could be used for this. but I don’t know MathJax, I saw someone say that the available fonts are limited anyway.

If this was an issue for me I would probably use the `PGFPlots` backend, so you’re actually leaving the rendering to `LaTeX` itself. If the compilation times get too large, you can add a recipe to your makefile to compile those image files to pdf before including them in the document.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [July 22, 2024, 1:43pm UTC](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037/20 "2024-07-22T13:43:49Z")

</div>

As of today’s GR release, the default height is more reasonable. Only took me three years to get around to it.
