# PlotlyJS + LaTeXStrings error

**URL:** <https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054>\
**Category:** General Usage\
**Tags:** plotting, plotlyjs, latexstrings\
**Created:** [April 4, 2023, 9:36am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054 "2023-04-04T09:36:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [April 4, 2023, 9:36am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/1 "2023-04-04T09:36:38Z")

</div>

Hi everyone,

I am having trouble producing a plot using PlotlyJS and LaTeXStrings.  
Here it is a simplified version of the code:

```julia
using DataFrames, PlotlyJS, LaTeXStrings

df = DataFrame(
    country = [repeat(["Portugal"], 15); repeat(["USA"], 15); repeat(["Sweden"], 15)],
    year = repeat(1:15, 3),
    r = randn(15*3)
)
sort!(df,:year)

plot(df,
	x = :year,
	y = :r,
	color = :country,
	labels = Dict(:country => "Countries"),
	Layout(
		font_size=14,
		margin=attr(l=80, r=60, b=60, t=80),
		title = "My Plot",
		xaxis_title = "",
		yaxis_title = L"$r$"
	)
)

```

The error I am getting is:  
`ERROR: MethodError: no method matching get(::LaTeXString, ::Symbol, ::Dict{Any, Any})`

Does anyone have a clue on how to solve it?  
I saw other discussions where people have trouble getting the math shown up properly. But no one with the same error as me.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 4, 2023, 11:15am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/2 "2023-04-04T11:15:29Z")

</div>

The error comes from here:

> [@rgouveiamendes](#):
>
> `yaxis_title = L"$r$"`

since `PlotlyBase` doesn’t understand how to get the text from a `LaTeXString`. You can do this “manually” with a `LaTeXString` by appending `.s` e.g. `L"r".s`  
Also, you don’t need to wrap the expression in `$` signs, this is done by the `LaTeXStrings ` package for you.

The only time I’ve been able to see LaTeX from `PlotlyJS.jl` is by rendering the plot in a browser (that is, via html): so I would run code like this from a Julia REPL session in a terminal:

```julia-auto
Plot(df, # Note: using `Plot`, not `plot`, from the terminal
	x = :year,
	y = :r,
	color = :country,
	labels = Dict(:country => "Countries"),
	Layout(
		font_size=14,
		margin=attr(l=80, r=60, b=60, t=80),
		title = "My Plot",
		xaxis_title = L"\sqrt{N_c}".s,
		yaxis_title = L"\int r".s # Note the `.s` suffix
	)
)

```

Changes are noted.

---

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [April 4, 2023, 1:44pm UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/3 "2023-04-04T13:44:20Z")

</div>

@jd-foster thank you very much! It worked perfectly.

---

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [April 5, 2023, 8:21am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/4 "2023-04-05T08:21:09Z")

</div>

@jd-foster Actually, I have another question that maybe you can help me with.  
As you said, we cannot render the plot with LaTeX unless we do it through a browser.  
That is not a big issue for me if at least I am able to **save it** correctly, namely in PDF or SVG formats.  
However, when using `savefig()` from `PlotlyJS`, in whatever format I choose, I am getting the same problem: math does not show up correctly. Do you know any way to overcome the problem?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 5, 2023, 1:36pm UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/5 "2023-04-05T13:36:47Z")

</div>

This is a good question. I do not think it is possible with the current code in `PlotlyJS.jl`, but would be happy to be corrected.  
The best work-around I can think of is to save it as in `.html` format, e.g.

```julia
savefig(p, "countries.html")

```

then convert the html somehow to the desired format.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [April 5, 2023, 5:12pm UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/6 "2023-04-05T17:12:35Z")

</div>

The math (LaTeX) symbols are saved as expected, only if you provide the unicode for each symbol, either like this:

```julia
fig = Plot(scatter(x=1:5, y=rand(5), mode="lines"), Layout(width=390, height=300,
        xaxis_title="μ²/3", yaxis_title="ξ"))

```

or:

```julia
fig = Plot(scatter(x=1:5, y=rand(5), mode="lines"), Layout(width=390, height=300,
        xaxis_title="\u03bc²/3", yaxis_title="\u03be"))

```

Then the corresponding plot is saved OK in pdf, svg, png format.  
LE: For a more LaTeX looking text, you may set , `font_family="Open Sherif"` or  
`font_family="Balto"`.

---

<div class="post-metadata">

**Author:** ![rgouveiamendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgouveiamendes/32/218636_2.png) [@rgouveiamendes](https://discourse.julialang.org/u/rgouveiamendes)\
**Post date:** [April 6, 2023, 10:04am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/7 "2023-04-06T10:04:36Z")

</div>

Thank you @empet. Your solution actually solves most of my needs, but I think it is just a nice workaround and not a fully satisfying solution.  
That way we cannot use more complicated expressions like `\sqrt{2 + x}`.  
Checking [LaTeXStrings.jl](https://github.com/stevengj/LaTeXStrings.jl) GitHub I found a FAQ regarding this issue: the package is not responsible for rendering the math.  
Using `Pluto.jl` Notebooks, the math is rendered perfectly once you append `.s` to the LaTeXString. But using the function `savefig()` from `PlotlyJS.jl` still does not save the figure with math (I tried to save it as PDF, SVG and PNG and none of the formats worked…).

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [April 6, 2023, 10:57am UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/8 "2023-04-06T10:57:41Z")

</div>

I don’t know why your plots are not saved with unicode. Here are my saved files:  
pdf: [https://github.com/empet/Datasets/blob/master/Images/save-unicode1.pdf](https://github.com/empet/Datasets/blob/master/Images/save-unicode1.pdf)  
and png:  
 ![save-unicode1](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d54cfe26911e5ec6bf086c7ccd4fc476c1ec1c5.png)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 6, 2023, 1:19pm UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/9 "2023-04-06T13:19:34Z")

</div>

I believe the issue is about rendering the LaTeX commands, and having them saved in the static image format. Having Unicode characters render in saved images should not be a problem.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 6, 2023, 2:01pm UTC](https://discourse.julialang.org/t/plotlyjs-latexstrings-error/97054/10 "2023-04-06T14:01:50Z")

</div>

I have figured out how to consistently have LaTeX appear in images produced using `savefig()`, but it requires modifying the source code of `PlotlyJS.jl` itself. If you would like to try this option, you can use the fork of the code at my GitHub [here](https://github.com/jd-foster/PlotlyJS.jl/tree/jdf/kaleido-mathjax-on) by doing

```julia
 pkg> add https://github.com/jd-foster/PlotlyJS.jl.git#jdf/kaleido-mathjax-on
```
