How to define plot labels in math mode?

Hello everyone,

With Lilaq, I’m having trouble in defining plot labels featuring math mode for display in the legend. Below is a code example of what I’m attempting:

#import "@preview/lilaq:0.6.0" as lq
#import "@preview/zero:0.6.1": set-num

#set page(width: auto, height: auto, margin: 0.5cm, fill: none)
#show lq.selector(lq.diagram): set text(font: "Montserrat", weight: "regular")
#set-num(math: false)

#let x = lq.linspace(0, 10)
#let y = x.map(x => calc.sin(0.1 * x * x))

#show: lq.theme.skyline
#lq.diagram(
  xlabel: "x",
  ylabel: "y",

  lq.plot(x, x => calc.sin(x + 0.541), label: "$f_{one}$"),
  lq.plot(x, x => calc.sin(2*x), label: math.attach("f", b: "two")),
  lq.plot(x, y, label: "function three"),
)

which produces:

I would expect the first plot to produce a label similar to that of the second one, which I obtained with a workaround by invoking math.attach(...). While the solution works in this case, I wouldn’t know how to proceed if I had to define a more complicated plot label, and I feel like the first syntax is more intuitive. Is the (non-)behavior intentional? If so, what would be the correct way to define math-enabled labels?

Additionally, I noticed that mixing math-mode labels with standard textual ones yields unmatching proportions between the font sizes (e.g. comparing the second and third labels). This remains true even if I use the standard font. I’ve tried playing around with show rules, but wasn’t able to obtain a good-looking result. The only partially satisfying option I found was to manually reduce the font size of text to an arbitrary value, but I’m sure there must be a better way.

I’m very new to Typst in general, so I’m sorry if I’m asking something very obvious, but I really couldn’t figure it out from the docs.

Thanks in advance for any kind of help,
Walter

By writing label: "$f_{one}$" inside quotes you’re passing a raw string, which doesn’t interpret Typst markup. You can use math mode directly:

lq.plot(x, x => calc.sin(x + 0.541), label: $f_"one"$),

There are special fonts for math content, by default Typst uses “New Computer Modern Math”. You can use a text font that has a matching math font, but even then the size can vary and you have to manually set the size.
You can change the math font with:

#show math.equation: set text(font: "New Computer Modern Math")
3 Likes

Hi Walter, welcome to the forum!

From the Lilaq label documentation, we see that it accepts any as its body, meaning you can pass in arbitrary content. In typst content is a very broad category which encompasses basically anything you’ll see in the output, so things like text, equations, images, etc.

From the documentation for syntax, which explains the differences between the three “modes” typst has (math, code, and markup), we see that when we are inserting parameters inside a function call we are in code mode, where anything encompassed in quotes is interpreted as a string and therefore displayed on the screen “as is”, with no markup. To display an equation it suffices to remove the quotes, as this now creates a math block:

lq.plot(x, x => calc.sin(x + 0.541), label: $f_"one"$)

As the label can be anything, you can therefore include more complicated equations in there, like $Gamma(z) = integral_0^oo t^(z-1)e^(-t) d t$

Notice also that the subscript is “one” in quotes as this creates a string. Unlike LaTeX, Typst doesn’t use curly braces for grouping (they are only really used for scoping something inside of code mode). There’s a good tutorial for getting to learn the basic differences between LaTeX and Typst: Guide for LaTeX Users - Typst Documentation

I’m not quite sure which font you mean by the standard font (the standarm math font or the standard text font?), but calls inside the math module (so including math.attach) will use the math font “new computer modern math” which is different (by default) to the text font “libertinus serif”. Additionally, since you are not inside an equation, typst defaults to the “upright” style, which can look a bit off