# Plots + PlotlyJS + LaTeX

**URL:** <https://discourse.julialang.org/t/plots-plotlyjs-latex/38250>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [April 26, 2020, 5:45pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250 "2020-04-26T17:45:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 26, 2020, 5:45pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/1 "2020-04-26T17:45:10Z")

</div>

I love PlotlyJS interactivity mainly in Jupyter notebook or VS Code Plot pane, on the other hand, I had always problems with latex in labels.

Recently, I noticed that in one of my julia environment (Julia 1.3.1, jupyter 2.0.0, [plotly extension](https://github.com/plotly/plotly.py)) the LaTeX rendering in Jupyter notebook just works

```julia
using Plots, LaTeXStrings

font = Plots.font("arial", 12)

plotlyjs(guidefont=font, xtickfont=font, 
   ytickfont=font, legendfont=font, lw = 2, size=(600,400));

p = plot(rand(10))
p = xlabel!("\$\\alpha \\, F_\\mathrm{test}\$")
p = ylabel!(L"\Gamma^2")

plot(p)

```

![Unknown](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4e9524dd8316d6c51ff44a58667a42c901ab4fa.png)

**but**

```julia
savefig("test.pdf")

```

gave errors `IOError(Base.IOError("connect: connection refused (ECONNREFUSED)", -111) during request(http://localhost:7982))` and empty file.

I can save it only as an HTML, but in the web browser, the LaTeX formulas were not rendered. The same ouput I got in VS Code plot pane.

I followed the [plotly.js readme](https://github.com/plotly/plotly.js/blob/master/dist/README.md#to-support-mathjax) and now I have plotly figure with correct LaTeX rendering.

Original HTML HEAD was

```julia
<head>
        <title>Plots.jl</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

```

![Unknown-2](https://global.discourse-cdn.com/julialang/original/3X/3/e/3eb5d3b8e61d23eabcc2be023753f5bff9be9b0c.png)

The new HTML HEAD is

```julia
 <head>
         <title>Plots.jl</title>
         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
         <script src="mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>
         <script>
            window.PlotlyConfig = {MathJaxConfig: 'local'}
         </script>
         <script src="plotly.js"></script>
</head>

```

Here, I downloaded `plotly.js` [GitHub - plotly/plotly.js: Open-source JavaScript charting library behind Plotly and Dash](https://github.com/plotly/plotly.js)

_ **Any chance to get it to work out of the box?** _

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [April 27, 2020, 11:05pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/2 "2020-04-27T23:05:35Z")

</div>

Best to open an issue at [https://github.com/JuliaPlots/Plots.jl](https://github.com/JuliaPlots/Plots.jl)

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [April 27, 2020, 11:57pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/3 "2020-04-27T23:57:24Z")

</div>

[https://wiki.archlinux.org/index.php/MathJax](https://wiki.archlinux.org/index.php/MathJax)

> If you are using `plotly.js` as well, loading MathJax before Plotly might fail to render TeX code. Loading Plotly _before_ MathJax should work.

Auto generated HTML doc by Weave and Jupyter puts mathjax before plotly in the header. So you may work around by editing the doc manually but It would be great if users can control the ordering programatically.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 28, 2020, 8:00am UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/4 "2020-04-28T08:00:06Z")

</div>

According to [plotly.js/README.md at master · plotly/plotly.js · GitHub](https://github.com/plotly/plotly.js/blob/master/dist/README.md#to-support-mathjax) it should be as it in my example above. The html with edited head section works fine.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [July 6, 2020, 5:53pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/5 "2020-07-06T17:53:01Z")

</div>

Today, I found [here in Plots docs](https://docs.juliaplots.org/latest/backends/) that

> Plotly needs to load mathjax to render LaTeX strings, therefore passing extra keywords with `extra_kwargs = :plot` is implemented. With that it is possible to pass a header to the extra `include_mathjax` keyword. It has the following options:

However, the example they provided does not work for me.

```julia
using Plots, LaTeXStrings
plotly()
plot(1:4, [[1,4,9,16]*10000, [0.5, 2, 4.5, 8]],
           labels = [L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}";
                     L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}"] |> permutedims,
           xlabel = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}",
           ylabel = L"d, r \text{ (solar radius)}",
           yformatter = :plain,
           extra_plot_kwargs = KW(
               :include_mathjax => "cdn",
               :yaxis => KW(:automargin => true),
               :xaxis => KW(:domain => "auto")
               ),
       )

```

I have this error:

> ERROR: Unknown key: extra\_plot\_kwargs  
> Stacktrace:  
> [1] default(::Symbol) at /Users/oto/.julia/packages/Plots/WwFyB/src/args.jl:632  
> [2] warnOnUnsupported\_args(::Plots.PlotlyBackend, ::Dict{Symbol,Any}) at /Users/oto/.julia/packages/Plots/WwFyB/src/args.jl:1160  
> [3] \_add\_the\_series(::Plots.Plot{Plots.PlotlyBackend}, ::Plots.Subplot{Plots.PlotlyBackend}, ::Dict{Symbol,Any}) at /Users/oto/.julia/packages/Plots/WwFyB/src/pipeline.jl:376  
> [4] \_process\_seriesrecipe(::Plots.Plot{Plots.PlotlyBackend}, ::Dict{Symbol,Any}) at /Users/oto/.julia/packages/Plots/WwFyB/src/pipeline.jl:404  
> [5] \_plot!(::Plots.Plot{Plots.PlotlyBackend}, ::Dict{Symbol,Any}, ::Tuple{UnitRange{Int64},Array{Array{Float64,1},1}}) at /Users/oto/.julia/packages/Plots/WwFyB/src/plot.jl:234  
> [6] #plot#137(::Base.Iterators.Pairs{Symbol,Any,NTuple{5,Symbol},NamedTuple{(:labels, :xlabel, :ylabel, :yformatter, :extra\_plot\_kwargs),Tuple{Array{LaTeXString,2},LaTeXString,LaTeXString,Symbol,Dict{Symbol,Any}}}}, ::typeof(plot), ::UnitRange{Int64}, ::Vararg{Any,N} where N) at /Users/oto/.julia/packages/Plots/WwFyB/src/plot.jl:57  
> [7] (::RecipesBase.var"#kw##plot")(::NamedTuple{(:labels, :xlabel, :ylabel, :yformatter, :extra\_plot\_kwargs),Tuple{Array{LaTeXString,2},LaTeXString,LaTeXString,Symbol,Dict{Symbol,Any}}}, ::typeof(plot), ::UnitRange{Int64}, ::Array{Array{Float64,1},1}) at ./none:0  
> [8] top-level scope at REPL[3]:1

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 7, 2020, 9:42am UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/6 "2020-07-07T09:42:18Z")

</div>

You are probably on an older version of Plots

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [July 7, 2020, 1:41pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/7 "2020-07-07T13:41:15Z")

</div>

I see, you are right one package downgraded my Plots package.  
Unfortunately, I have latex rendered only when saved as html.  
In vs code plot pane and in PDF the latex is not correct.

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [September 23, 2020, 7:05am UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/8 "2020-09-23T07:05:27Z")

</div>

For me it is similiar. In the html everything works, but if I save `savefig(p1, <file_path>)` i.e. as a pdf it isn´t working

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [November 29, 2021, 8:41am UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/9 "2021-11-29T08:41:02Z")

</div>

PlotlyJS supports latex string via Mathjax, but only in html format. ([Latex in Julia](https://plotly.com/julia/LaTeX/))  
Is there any way how to convert html figure to pdf/eps?

---

<div class="post-metadata">

**Author:** ![JanKap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jankap/32/22763_2.png) [@JanKap](https://discourse.julialang.org/u/JanKap)\
**Post date:** [December 10, 2021, 12:23pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/10 "2021-12-10T12:23:24Z")

</div>

For me, even the original code does not work (disabled plot pane):  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/b/8b03a6c96b00b1ea5c31d769a2cc89cbaec73639.png)

Guess, MathJax must be enabled for the standalone windows somehow.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [December 10, 2021, 7:40pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/11 "2021-12-10T19:40:29Z")

</div>

You need to save it as HTML…

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 24, 2022, 1:00pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/12 "2022-07-24T13:00:02Z")

</div>

@JanKap@Oto_Brzobohaty  
Well, I try to, but it does not works, here my codes:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, MathJaxRenderer 
plotlyjs()

x = collect(-5:0.5:5)
y = collect(-5:0.5:5)

z(x, y) = x^2 - 3*y + 2

l = L"$x^2 - 3y + 2$"

plot(x, y, z, st = :surface, camera = (10, 10))
title!(l)

ylabel!(L"\Gamma^2")

savefig("test.html")

```

 ![Capture d’écran_2022-07-24_19-58-53](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c77500c57c5d4bacb89f3e8ca88942fb57f9e9f.png)

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [July 24, 2022, 6:16pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/13 "2022-07-24T18:16:07Z")

</div>

> **[LaTeX workflow - Backends · Plots](https://docs.juliaplots.org/stable/backends/#LaTeX-workflow)**
>
> Documentation for Plots.

### [MathJax](https://docs.juliaplots.org/stable/backends/#MathJax)

Plotly needs to load MathJax to render LaTeX strings, therefore passing extra keywords with `extra_kwargs = :plot` is implemented. With that it is possible to pass a header to the extra `include_mathjax` keyword. It has the following options:

- `include_mathjax = ""` (default): no mathjax header
- `include_mathjax = "cdn"` include the standard online version of the header
- `include_mathjax = "<filename?config=xyz>"` include a user-defined file

These can also be passed using the `extra_plot_kwargs` keyword.

```julia
using LaTeXStrings
plotlyjs()
plot(1:4, [[1,4,9,16]*10000, [0.5, 2, 4.5, 8]],
           labels = [L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}";
                     L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}"] |> permutedims,
           xlabel = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}",
           ylabel = L"d, r \text{ (solar radius)}",
           yformatter = :plain,
           extra_plot_kwargs = KW(
               :include_mathjax => "cdn",
               :yaxis => KW(:automargin => true),
               :xaxis => KW(:domain => "auto")
               ),
       )

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 25, 2022, 5:05am UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/14 "2022-07-25T05:05:08Z")

</div>

I try your code, it works if I use `"cdn"`, but not if only using `""`

```julia
using LaTeXStrings, Plots
plotlyjs()

plot(1:4, [[1,4,9,16]*10000, [0.5, 2, 4.5, 8]],
           labels = [L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}";
                     L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}"] |> permutedims,
           xlabel = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}",
           ylabel = L"d, r \text{ (solar radius)}",
           yformatter = :plain,
           extra_plot_kwargs = KW(
               :include_mathjax => "cdn",
               :yaxis => KW(:automargin => true),
               :xaxis => KW(:domain => "auto")
               ),
       )
savefig("test.html")

```

 ![Capture d’écran_2022-07-25_12-06-49](https://global.discourse-cdn.com/julialang/original/3X/6/4/642a701a3d6227aff408a55d77eecef20c37b3f2.png)

Thanks a Lot!

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [October 5, 2022, 3:46pm UTC](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/15 "2022-10-05T15:46:28Z")

</div>

In the current version of PlotlyJS, which is v0.18.8, it is under OS WIN10 not necessary to specify the value of `include_mathjaxt`, it works fine out of the box for html-output.  
What currently not yet work is, the propper output in another vector format.
