Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tex_annotation!() #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/MakieTeX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Comment on lines +167 to +172
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa cool! I didn't know you could do that.

Do \bf, \it, \sc etc work correctly with this?

Copy link
Author

@Moelf Moelf Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably, we can probably have a comprehensive test of making 2x2x2 different math on the same canvase as a sanity check

two caveats:

  • I can't find the default font's name
  • I'm yet to fully understand how the exactly interaction between multiple set*font work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default font in Makie is "TeX Gyre Heros Makie", it's in the assets folder of Makie (I think).
I guess we should set the default math font to also be whichever math font relates to TeX Gyre Heros? This is what I was trying to do earlier, in building up a set of known font families for which we have good math fonts.

""",
class = "standalone",
classoptions = "preview, tightpage, 12pt"
)
tex = CachedTeX(texdoc)
scatter!(axis, x, y; tex, markersize=tex.dims .* scale_factor)
end
Loading