# Legend items with italic parts in AlgebraOfGraphics

**URL:** https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341
**Category:** Visualization
**Tags:** makie, algebraofgraphics, aog
**Created:** [May 29, 2026, 9:44pm UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341 "2026-05-29T21:44:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stelmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stelmo/32/6242_2.png) [@stelmo](https://discourse.julialang.org/u/stelmo)
#### Post date: [May 29, 2026, 9:44pm UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341/1 "2026-05-29T21:44:12Z")

</div>

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:

```julia-auto
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:

```julia-auto
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:

```julia-auto
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](https://github.com/MakieOrg/AlgebraOfGraphics.jl/issues/695). 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?

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [May 29, 2026, 11:22pm UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341/2 "2026-05-29T23:22:24Z")

</div>

LaTeX to the rescue.

```julia
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)

```

 ![display](https://global.discourse-cdn.com/julialang/original/3X/2/b/2ba7221f92ae84258482dd2bade894941e987e0d.png)

---

<div class="post-metadata">

### Author: ![stelmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stelmo/32/6242_2.png) [@stelmo](https://discourse.julialang.org/u/stelmo)
#### Post date: [May 30, 2026, 7:19am UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341/3 "2026-05-30T07:19:00Z")

</div>

Thank you, this works!

For reference: I also set the font to Gyre to match the rest of Makie (I don’t like Computer Modern)

```julia-auto
using CairoMakie, Makie, AlgebraOfGraphics, DataFrames, LaTeXStrings, MathTeXEngine
set_texfont_family!(FontFamily("TeXGyreHeros"))

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}",
]

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

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 30, 2026, 8:14am UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341/4 "2026-05-30T08:14:25Z")

</div>

The problem here is just that I haven’t decided yet if rich text should be sortable or not, given that it has all this metadata. How should two rich text objects with the same text but different metadata be sorted? Maybe it doesn’t matter if metadata is ignored but I feared just being sloppy about it might have some unintended consequences in other places where isless is used.

You can always use the `categories` mechanism in the scale settings to relabel categories. The good thing there is that it’s after scale fitting, so the labels have already been sorted, so there’s no isless problem with rich text. You can still sort there if you want, though, it’s quite flexible. The renamer mechanism was just the only way to do it before scales were introduced.

---

<div class="post-metadata">

### Author: ![stelmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stelmo/32/6242_2.png) [@stelmo](https://discourse.julialang.org/u/stelmo)
#### Post date: [May 30, 2026, 8:35am UTC](https://discourse.julialang.org/t/legend-items-with-italic-parts-in-algebraofgraphics/137341/5 "2026-05-30T08:35:04Z")

</div>

Great, this works also! Thanks 🙂

Yep, after looking at `Makie/src/richtext.jl` I also realized why these definitions were missing, it is not clear at all what the ordering should be… However, the categories solution solves it, so probably not necessary to make any decision 🙂

Here is the code for those interested:

```julia-auto
using CairoMakie, Makie, AlgebraOfGraphics, DataFrames

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

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

data(df) * mapping(:x => "This is the x label", :y, color = :r => "Species") * visual(Scatter) |> draw(
    scales(
        Color = (; categories = rf2,)
    ), 
)

```
