How to format Roman O in math mode?

I want to write a simple chemical formula of the type:

$“Cu”_2"O"_2$

Why does typst format the O as italic and not Roman as it should be when enclosed with “”?

Hey @Mark_Dean, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

This is a known bug and there have been several attempts to fix it, without success so far. It is an interaction with how typing a single letter (e.g. x) produces a variable.

You can work around this by typing upright("O"), or even (in a pinch) " O" or similar.

But given your intentions, as you appear to be typesetting chemical equations, I’ll first and foremost recommend the whalogen package, which seems to be made for this: whalogen – Typst Universe

If it doesn’t satisfy your needs, however, here are several alternatives. You can use variables to save some typing:

// Alternative 1 (requires '#' to disambiguate with variables, more annoying)
#let O = $upright("O")$

// Alternative 2 (does not require '#', better)
#let Ox = $upright("O")$

// Also, just for completeness:
#let Cu = $"Cu"$

// Usage of both:

// Requires semicolon as "#O_2" is a valid variable name...
$ Cu_2 #O;_2 $

// A bit saner in my opinion
$ Cu_2 Ox_2 $

An alternative is to use a show rule to ensure all isolated 'O’s in math mode are typeset as upright. I’d recommend isolating the show rule in a dedicated function in this case, to not affect mathematical equations:

#let ce(body) = {
  show math.equation: it => {
     show regex("\bO\b"): math.upright
     it
  }
  body
}


#ce($ "Cu"_2 O_2 $)

Cu2O2 (no italic O)

(By the way: You can also insert syntax-highlighted Typst code snippets with ```typ SOME CODE HERE``` , try it out!)

1 Like

FYI, there exists another package for that purpose by @James (I think), which seems to support more notations than whalogen (at least on the upstream version).

#import "@preview/chem-par:0.0.1": *
#show: chem-style
Cu2O2

image

1 Like

Thank you all.

I’m writing a CV without any math, and without much desire to include more dependencies. For me, it is sufficient to suppress all math mode italic O.

#show math.equation: it => {
     show regex("\bO\b"): math.upright
     it
  }

Sounds great @Mark_Dean. If a particular reply solved your problem, would you mind marking it as a solution with the :white_check_mark: (check mark) button? Thanks!

I just ticked it. Thanks for the help.

1 Like