diff --git a/src/MakieTeX.jl b/src/MakieTeX.jl index a738443..9d05881 100644 --- a/src/MakieTeX.jl +++ b/src/MakieTeX.jl @@ -53,6 +53,8 @@ export LTeX export LaTeXStrings, LaTeXString, latexstring, @L_str export Typstry, TypstString, @typst_str +export tex_annotation! + "Try to write to `engine` and see what happens" function try_tex_engine(engine::Cmd) try diff --git a/src/recipe.jl b/src/recipe.jl index 7752a1d..181128b 100644 --- a/src/recipe.jl +++ b/src/recipe.jl @@ -143,3 +143,37 @@ function Makie.plot!(plot::TeXImg) markerspace = plot.markerspace, ) end + +""" + tex_annotation!(axis::Axis, lstring, x, y; mainfont = nothing, mathfont = nothing, scale_factor=1) + +Add TeX annotation to an existing Makie Axis. Under the hood, it does a few things: + +1. via `mathspec` LaTeX package, we set the `mainfont` and `mathfont` +2. Render it using `tectonic_jll` and convert to an image matrix. +3. call `Makie.scatter!` and using the image as `marker`, scale the image by `scale_factor` while preserving aspec ratio. + + !!! note +You can use `\textcolor` from `xcolor` inside the latex string. +""" +function tex_annotation!(axis::Axis, lstring, x, y; mainfont = nothing, mathfont = nothing, scale_factor=1) + texdoc = TeXDocument( + String(lstring), true; + requires = "\\RequirePackage{luatex85}", + preamble = """ + \\usepackage{amsmath, xcolor} + \\usepackage{mathspec} + \\pagestyle{empty} + $(isnothing(mainfont) ? "" : + "\\setmainfont{$mainfont}[Scale=MatchLowercase, Ligatures=TeX]" + ) + $(isnothing(mathfont) ? "" : + "\\setmathfont(Digits,Latin)[Scale=MatchLowercase]{$mathfont}" + ) + """, + class = "standalone", + classoptions = "preview, tightpage, 12pt" + ) + tex = CachedTeX(texdoc) + scatter!(axis, x, y; tex, markersize=tex.dims .* scale_factor) +end