From 68db760be9dea902246e19206391576d1c9f77ac Mon Sep 17 00:00:00 2001 From: James Foster <38274066+jd-foster@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:13:40 -0600 Subject: [PATCH] Enable MathJax functionality (#11) * Enable MathJax functionality Allow LaTeX to appear in images produced using savefig(). * Make MathJax configurable --- src/PlotlyKaleido.jl | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/PlotlyKaleido.jl b/src/PlotlyKaleido.jl index c26fd58..6ac65f7 100644 --- a/src/PlotlyKaleido.jl +++ b/src/PlotlyKaleido.jl @@ -17,13 +17,18 @@ end const P = Pipes() +const _mathjax_url_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax" +const _mathjax_last_version = v"2.7.9" + kill_kaleido() = is_running() && kill(P.proc) is_running() = isdefined(P, :proc) && isopen(P.stdin) && process_running(P.proc) restart(;kwargs...) = (kill_kaleido(); sleep(0.1); start(;kwargs...)) -function start(;plotly_version = missing, kwargs...) +function start(;plotly_version = missing, + mathjax = missing, mathjax_version::VersionNumber = _mathjax_last_version, + kwargs...) is_running() && return cmd = joinpath(Kaleido_jll.artifact_dir, "kaleido" * (Sys.iswindows() ? ".cmd" : "")) basic_cmds = [cmd, "plotly"] @@ -39,6 +44,21 @@ function start(;plotly_version = missing, kwargs...) kwargs... ) end + if !(mathjax === missing) + if mathjax_version > v"2.7.9" + error("The given mathjax version $(mathjax_version) is greater than the last supported version of $(_mathjax_last_version).") + end + if mathjax isa Bool && mathjax + push!(chromium_flags, "--mathjax=$(_mathjax_url_path)/$(mathjax_version)/MathJax.js") + elseif mathjax isa String + # We expect the keyword argument to be a valid URL or similar, else error "Kaleido startup failed with code 1". + push!(chromium_flags, "--mathjax=$(mathjax)") + else + @warn """The value of the provided argument + mathjax=$(mathjax) + is neither a Bool nor a String and has been ignored.""" + end + end # Taken inspiration from https://github.com/plotly/Kaleido/blob/3b590b563385567f257db8ff27adae1adf77821f/repos/kaleido/py/kaleido/scopes/base.py#L116-L141 user_flags = String[] for (k,v) in pairs(extra_flags)