How to draw a textbox around a text in Makie.jl?

In Makie.jl, how can I draw a box around a text? More specifically, I’d like to draw the bounding box of a given text that I’ve plotted using the text! function.

In fact, it is even better if I could use the existing Textbox, which looks gorgeous, and already has all the customization I need regarding line and box colors. But so far Textbox is a layoutable, while I want it to be plottable at arbitrary locations on an axis.

https://makie.juliaplots.org/stable/examples/layoutables/textbox/index.html#textbox

1 Like

The two step projection makes this a bit more complicated. With the text position in data space and the textsize in pixel units you’d need

fig, ax, p = text("Test", position = Point2f(2))
scene = campixel(ax.scene);
bb = Rect2f(boundingbox(
    p.plots[1][1][], 
    Makie.to_ndim(Point3f, Makie.project(ax.scene, p.plots[1].position[]), 0), 
    Makie.to_rotation(p.plots[1].rotation[])
))
mesh!(scene, bb, color = :yellow, shading = false)
lines!(scene, bb)
translate!(scene, 0, 0, -1)
fig

boundingbox(p) seems to fall back on some default method that isn’t useful. boundingbox(p.plots[1]) does use the right method but assumes the text position and textsize to be in the same space. So you need to project position into pixel space first and use the boundingbox above. The result is in pixelspace so you need to plot in on a scene with a pixel camera.