# Makie text with variable styling

**URL:** <https://discourse.julialang.org/t/makie-text-with-variable-styling/81666>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [May 25, 2022, 5:21pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666 "2022-05-25T17:21:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 25, 2022, 5:21pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/1 "2022-05-25T17:21:38Z")

</div>

I’m often plotting things where I need the labels to have some styling. Particularly species names that need to be in italics, eg a y-label that’s something like

“Relative abundance of _E. coli_”

There are easy ways to set the overall font of a label or other text element, but as far as I can tell, no way to have different fonts withing the same text element.

If this feature doesn’t currently exist, I’d be interested in taking a stab at implementing it, though I imagine that the way that stuff interacts with layouts could get pretty complicated, and I haven’t spent much time in the Makie internals. In an ideal world, this would work with `@md_str` or the stuff from `CommonMark.jl`, in the same way that `@L_str` currently works for LaTeX, but even if it requires something a bit more hacky, that would be really useful.

---

<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 25, 2022, 8:25pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/2 "2022-05-25T20:25:57Z")

</div>

It does exist but this part of the API is pretty rough/hacky. Currently you can pass a vector of font names and each glyph takes its own font. Text shaping / styling / font substitution, etc, is a verrry deep rabbit hole and so far we’ve gotten pretty far without relying on complex tools like Pango / Harfbuzz. However that also means that we don’t have any of their more advanced features like style markup, bidirectional text, etc.

```julia
text("Relative abundance of E. coli",
    font = [fill("TeX Gyre Heros Makie", 22); fill("TeX Gyre Heros Makie Italic", 7)],
    textsize = 30,
    align = (:center, :center))

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/7/9/79c7b9b0f6f180564ed5d301e73ad31a0df6406c.png)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 26, 2022, 1:14am UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/3 "2022-05-26T01:14:51Z")

</div>

> [@jules](#):
>
> so far we’ve gotten pretty far without relying on complex tools like Pango / Harfbuzz

I had no idea such tools existed 🤯

I’m happy to use the hacky solution for now in my own stuff - would there be interest in a PR that attempts to support a small number of markdown styles (I’m thinking just bold and italic really) using that hack, or does it make more sense to wait for a more complete solution (which I do not feel capable of tackling)?

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [May 26, 2022, 11:29am UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/4 "2022-05-26T11:29:47Z")

</div>

what about a different colour for E. coli?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 12:32pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/5 "2022-05-26T12:32:59Z")

</div>

Sorry gentlemen to chime in, as I am not yet a regular Makie user but would like to become one.

As I understand that Makie supports LaTeXStrings, is it not possible to write the equivalent of Plots.jl annotate!() using the pgfplotsx() backend:

```julia
using LaTeXStrings, Plots; pgfplotsx()
st0 = "Relative abundance of: "
st1 = "E. coli"
plot();
annotate!(0.5,0.5, (L"\textcolor{blue}{\textrm{%$st0}} \textcolor{red}{\textbf{\textit{%$st1}}}", 12))

```

Thank you.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 26, 2022, 4:15pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/6 "2022-05-26T16:15:00Z")

</div>

> [@rafael.guerra](#):
>
> As I understand that Makie supports LaTeXStrings,

I got really excited for a moment, but I tried:

```julia
ax = Axis(fig[1, 1]; xlabel=L"Relative abundance of \textit{E. coli}")

```

and got

```julia
caused by: TeXParseError: unsupported command \textit
at position 30 (string index 30)
Relative abundance of \textit{E. coli}
                             ^

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [May 26, 2022, 4:22pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/7 "2022-05-26T16:22:41Z")

</div>

LaTeXStrings.jl does not do any rendering. It is only for convenience to avoid mess with all the escaping when interpolating strings that contain LaTeX commands, as far as I understand. The rendering in Makie.jl is done via [MathTexEngine.jl](https://github.com/Kolaru/MathTeXEngine.jl/) but it looks like it has not yet implemented different fonts. It could be a good pathway to contribute to MathTexEngine, no?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 26, 2022, 4:43pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/8 "2022-05-26T16:43:22Z")

</div>

> [@kevbonham](#):
>
> Particularly species names that need to be in italics

You can just use [Unicode italics](https://yaytext.com/bold-italic/) (assuming you have a font that covers these characters):

```julia
text("Relative abundance of 𝘌. 𝘤𝘰𝘭𝘪")

```

(In Julia, type by tab-completing `\itE`, `\itc`, etcetera. You could easily write a function that converts ASCII letters in a string to their Unicode italic, bold, etcetera equivalents, but the web site linked above is also pretty convenient.)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 18, 2022, 5:56pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/9 "2022-07-18T17:56:27Z")

</div>

This no longer seems to work:

```julia
text("Relative abundance of E. coli",
           font = [fill("TeX Gyre Heros Makie", 22); fill("TeX Gyre Heros Makie Italic", 7)])
ERROR: BoundsError: attempt to access 1-element UnitRange{Int64} at index [2]
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 15, 2023, 11:03am UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/10 "2023-02-15T11:03:41Z")

</div>

> [@kevbonham](#):
>
> I’m often plotting things where I need the labels to have some styling.

what about [`rich()`](https://docs.makie.org/stable/examples/plotting_functions/text/index.html#rich_text)?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 15, 2023, 7:31pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/11 "2023-02-15T19:31:54Z")

</div>

Ah, lovely. TIL

```julia
fig=Figure()
ax = Axis(fig[1,1], 
          xlabel=rich("Relative abundance of ", rich("E. coli"; font=:italic))
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b00c81bed083781aefb2807119eea25d98487ce.png)

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 15, 2023, 9:08pm UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/12 "2023-02-15T21:08:25Z")

</div>

> [@stevengj](#):
>
> In Julia, type by tab-completing `\itE`, `\itc`, etcetera.

Is there a chance to get sans-serif font?  
`\textsf{}` does not work for me under GLMakie

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2023, 12:50am UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/13 "2023-02-16T00:50:39Z")

</div>

> [@ellocco](#):
>
> Is there a chance to get sans-serif font?

Yes, Unicode has [codepoints for sans-serif characters](https://www.fileformat.info/info/unicode/char/1d5a0/index.htm), which you can type in Julia by tab-completing `\sansE`, `\sanst` etcetera to get text like “𝖤𝗍𝖼”.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 16, 2023, 6:54am UTC](https://discourse.julialang.org/t/makie-text-with-variable-styling/81666/14 "2023-02-16T06:54:59Z")

</div>

> [@stevengj](#):
>
> tab-completing `\sansE`, `\sanst` etcetera to get text like

Cool!!! - Thanks!!! ❤
