Aligning Multi-line Text (in Makie)

Hi,

Is it possible to do multiline text alignment in Makie plots LaTeX equation style? For example, describing some fluid conditions, I would like to write text something like:

text!(ax, location, text = "p=100kPa\nT=300K\nRe_inf=1e6")

but align the text at the “=” sign. In a LaTeX environment you can align using the & symbol, but I haven’t been able to work it out using regular text or LaTeXStrings.

This doesn’t exist yet and should probably be handled by MathTeXEngine.jl. I encourage you to open an issue in that repository if one doesn’t exist already.

Maybe I can help you with some hacky workaround code, though. You can offset each text in a vector of texts separately, and you can find out how much to offset by going into the GlyphCollections in the lower layer of the plot object and finding out where the first = is placed:

fig, ax, t = text([1, 1, 1], [1, 1, 1],
    text = [
        L"x = y",
        L"x/y = 1",
        L"\sum{x} = x \times y",
    ],
    offset = [
        Vec2f(0, 0),
        Vec2f(0, -50),
        Vec2f(0, -100),
    ],
    fontsize = 25
)
gc_obs = t.plots[1][1]
gcs = gc_obs[]
eq_offsets = map(gcs) do gc
    idx = findfirst(1:length(gc.glyphs)) do i
        Makie.FreeTypeAbstraction.glyph_index(gc.fonts[i], '=') === gc.glyphs[i]
    end
    idx === nothing && error("Didn't find a '=' in a glyphcollection.")
    return gc.origins[idx][1]
end
max_offset = maximum(eq_offsets)
new_offsets = max_offset .- eq_offsets
t.offset[] = [Vec2f(n, y) for (n, (x, y)) in zip(new_offsets, t.offset[])]
fig

This is just a proof of concept, the alignment would need to be adjusted as well and made automatic, but that’s how one could do it. The data structures used could also change at any time, they are not stable API.

Thanks for the response, that code snippet worked perfectly for aligning the text. Unfortunately it seems to break the positioning of the text, I’ll see if I can work out why.

The MathTexEngine Github specifies that line breaks/wraps are currently not supported in in-line mode, but it looks like there’s a currently open issue on the matter.