PlutoPlotly: save a plot with LaTeX strings

Hi,

I can produce a plot in Pluto with some LaTeX ingredients using PlutoPlotly and LaTexStrings (which is not possible to achieve to the best of my knowledge using PlotlyJS). The plot is perfectly rendered in the notebook’s active and static versions, and the LaTeX strings are correctly displayed.

However, when I try to save it as a PDF, SVG, or PNG file, the LaTeX output is gone (please see the images below). Is there any way we can overcome this problem by using Pluto? @jd-foster seems to propose a way out of this problem here. I tried it, but I was not successful.

Thanks.

MWE: Julia 1.10.0, PlutoPlotly 0.4.4, Pluto 0.19.38, PlotlyJS 0.18.12.

begin
	using PlutoUI ,  PlutoPlotly , LaTeXStrings
	import PlotlyJS: savefig

	fig2 = plot(1:300, [randn(300) randn(300)])
		
	relayout!(fig2, 
		height = 450,
		title_text = L"\text{A title with some LaTeX:} \sum \beta^i x_{t-i}", title_x = 0.5, 
		hovermode = "x", 
		yaxis_title = L"y(t), x(t)", 
		xaxis_title = L"t",
		xaxis_range=[-10 , 310]) 
	
	restyle!(fig2, 
		name = [L"\sigma^2_x = 1" , L"\sigma^2_y = 1"], 
		mode="markers+lines", line_width = 0.3, line_color = ["RoyalBlue", "Maroon"])
	
	fig2
	
	savefig(fig2.Plot, "fig2.pdf", width = 1000)
end

Output in the notebook:

Saved pdf figure:

Hi @VivMendes, you have to tell Kaleido (which is used internally to save plots) that it has to load mathjax or the plot will not render latex as you experienced there.

I do not think that the savefig from PlotlyJS has that functionality, but luckily you don’t need to use the outdated savefig in PlotlyJS with PlutoPlotly and you can directly use the smaller and more featureful PlotlyKaleido.jl package, which implemented mathjax in kaleido with this PR.

With PlotlyKaleido (if you are on windows you may catch the problem of Kaleido always hanging that is mentioned here, here and in other places, but that is a problem of underlying library and is also present in PlotlyJS), you can simply use this code

# ╔═╡ 42cd34bf-7b19-4d01-87c2-b76d6ef01912
import Pkg

# ╔═╡ a18c9202-8390-4861-a33b-732b940dde7f
using PlutoUI ,  PlutoPlotly , LaTeXStrings

# ╔═╡ 90f140e1-33fa-4240-bb9e-112e3cd7c34a
begin
	Pkg.pkg"add Kaleido_jll@v0.1.0"
	using PlotlyKaleido
end

# ╔═╡ 9d18f8e9-8c4b-473e-a775-a29d01b4aae0
PlotlyKaleido.start(;mathjax = true)

# ╔═╡ 6eabfdc0-ca94-11ee-0ba2-7df4b3955314
begin
	

	fig2 = plot(1:300, [randn(300) randn(300)])
		
	relayout!(fig2, 
		height = 450,
		title_text = L"\text{A title with some LaTeX:} \sum \beta^i x_{t-i}", title_x = 0.5, 
		hovermode = "x", 
		yaxis_title = L"y(t), x(t)", 
		xaxis_title = L"t",
		xaxis_range=[-10 , 310]) 
	
	restyle!(fig2, 
		name = [L"\sigma^2_x = 1" , L"\sigma^2_y = 1"], 
		mode="markers+lines", line_width = 0.3, line_color = ["RoyalBlue", "Maroon"])
	
	fig2
	
	savefig(fig2, "fig2.pdf", width = 1000)
end

(I am importing Pkg and calling Pkg.pkg"" to install a custom version of Kaleido_jll without upsetting Pluto Pkg Manager to fix the issue mentioned above. If you don’t have problems on your machine with Kaleido you can skip that instruction).

This will generate the pdf with latex correctly:

As you can see, the only issue is that latex text is bold, but again that is an issue with the underlying Kaleido library which has currently no fix.

1 Like

@disberd, thank you very much. I do not bother with the bold thing. The output looks immaculate in any format.

As usual, you make solutions so easy; that is amazing.

1 Like

Hi @disberd,

In case someone else has the same problem as myself, I tested your solution on various computers (Windows and Linux), and I can summarize my situation as follows:

  1. I did not need the downgrade of Kaleido_jll. In my case, even if I did so, it did not solve the problem. So, Kaleido_jll@v0.1.0 vs. Kaleido_jll@v0.2.1 was not the main issue in my case.

  2. I need to use both packages: PlutoPlotly and PlotlyKaleido

  3. I need to apply both commands:

    PlotlyKaleido.start(;mathjax = true)
    force_pluto_mathjax_local(true)

    in order to (i) save plots that include LaTeX strings (PlotlyKaleido.start(...)) and (ii) to obtain a visible display of those strings in both the active and static versions of the notebooks (force_pluto_mathjax_local(...)). If I include only the former, saving plots with LaTeX works very well, but no LaTeX strings will appear in the two notebook versions. If I include only the latter, the visible display of those strings works OK in the browser, but no savefig function will be available to save the plots.

  4. My initial problem was that before yesterday, when I started to test your solution, I had never used simultaneously PlotlyKaleido.start(...) and force_pluto_mathjax_local(...), which led to the two polar cases: I could save plots with LaTeX strings in them (and those strings were perfectly rendered), but there would be no visible strings on both notebooks’ versions; and vice versa.

Thanks a lot for your help.

1 Like