How can I make uppercase letters upright by default in math mode, but selectively italic?

Hello everyone,

I’m trying to implement a specific formatting rule in my Typst document where uppercase letters in math mode should be upright by default (instead of the default italic style), but I still want to be able to selectively make some of them italic when needed.

Here’s what I’ve tried:

#show math.equation: eq => {
  show regex("[A-Z]"): it => math.upright(it.text)
  eq
}

$A = B + C + italic(A)$

The problem is that while this successfully makes all uppercase letters upright by default (A, B, C), the italic(A) still appears upright, ignoring the italic() function.

I understand this happens because the regex rule is applied after other styling, effectively “overriding” any italic() commands.

Is there a way to achieve what I’m trying to do? Ideally, I’d like:

  1. All uppercase letters to be upright by default in math mode
  2. The ability to use something like italic(A) or a custom function to selectively make specific uppercase letters italic when needed

I don’t want to use “A” (quoted text) in math mode to get upright letters, as this would require changing too much of my existing document. I’m looking for a solution that works with regular uppercase letters and math syntax.

Any suggestions would be greatly appreciated!

Thank you!

2 Likes

The regex show rule only matches the “raw” content without the math.italic() style. The style math.upright() is therefore added first and the “custom” style math.italic() is ignored. You could work around this (or even use this behavior) by applying a general show rule that applies math.upright() and a regex show rule matching everything but uppercase characters that applies math.italic().

I don’t know if there are any special cases where this approach fails. The few examples below work exactly according to your specifications.

#show math.equation: eq => {
  show: math.upright
  show regex("[^A-Z]"): math.italic
  eq
}

$A = B + C + italic(A)$ \
$a = b + c + d$ \
$1 + 2$
2 Likes

Thank you for your solution! This works perfectly and helped me understand how Typst handles style application. I appreciate you taking the time to explain the underlying mechanism with the regex matching only raw content. Your approach is elegant and exactly what I needed.

1 Like