Legend items with italic parts in AlgebraOfGraphics

Is there a way to display legend items in AoG that are made of rich text objects? For example, in biology it is usual for species names to be written in italics. It seems the best way to do this with Makie is to use RichText. However, this causes problems when you try to use these elements in legends using AoG, see below for a minimal working example:

using CairoMakie, Makie, AlgebraOfGraphics, DataFrames

df = DataFrame(x = [1,2,3,4], y = [1,2,3,4], r = ["Ec", "Vn", "Ec", "Vn"])

rf = [
    "Ec" => rich(rich("E. coli", font = :italic), " MG1655"), 
    "Vn" => rich("V. natriegens", font = :italic),
]

data(df) * mapping(:x, :y, color = :r => renamer(rf) => "Species") * visual(Scatter) |> draw

which gives the error:

ERROR: MethodError: no method matching isless(::Makie.RichText, ::Makie.RichText)
The function `isless` exists, but no method is defined for this combination of argument types.

A potential fix for this is to extend some base functions allowing sorting on RichText objects:

import Base: isless, isequal
Base.isequal(a::Makie.RichText, b::Makie.RichText) = a == b
Base.isless(a::Makie.RichText, b::Makie.RichText) = Base.isless(String(a), String(b))

This is also reported in an issue on Github: renamer returning RichText causes issues · Issue #695 · MakieOrg/AlgebraOfGraphics.jl · GitHub. Note, this problem is not just restricted to the renamer function, but rather any time a RichText object is used when a sort is called internally somewhere.

I tried making a small fix for this, but these extensions break other plots as reported in the issue.

Does anyone have a way to address this issue? Is there a “good” workaround?

LaTeX to the rescue.

using CairoMakie, AlgebraOfGraphics, DataFrames, LaTeXStrings

df = DataFrame(
    x = [1, 2, 3, 4],
    y = [1, 2, 3, 4],
    r = ["Ec", "Vn", "Ec", "Vn"],
)

rf = [
    "Ec" => L"\mathit{E.\ coli}\ \mathrm{MG1655}",
    "Vn" => L"\mathit{V.\ natriegens}",
]

plt =
    data(df) *
    mapping(:x, :y, color = :r => renamer(rf) => "Species") *
    visual(Scatter)

draw(plt)