PlutoPlotly: save a plot with LaTeX strings

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