How to use show set to select non text characters in math equation?

image

#show math.equation: it => {
    show text: set text(green)
    show ??: set text(red)
    it
}
$"ABCDabcd" a b c d$

I want only select italic letter in math equation.

Hi @HuangMI, thanks for your question. It is not clear what you’re asking, please add some description so that we know what your problem is.

What is a “show code”?

I don’t understand why regex eats the space. Might be a bug.

To get the result above, just use a better regex.

#show regex("[a-d]{2,}"): set text(red)
$"ABCDabcd" a b c d$

image

Sorry,I modified this question,thanks your answer. @SillyFreak @Andrew

There seems to be a similarity with How to override hard-coded equation colors?

Something like this?


#show math.equation: set text(red)
#show math.text: set text(green)

$"ABCDabcd" a b c d$

I want only select italic letter in math equation.

Sorry you’ve been changing the initial post a few times now.

I’m comparing your intended result with mine and can’t figure out what is missing.

Perhaps you could describe what you are trying to achieve exactly?

#show math.equation: it => {
  show regex(".+"): it => {
    if it.text in ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"){
      return text(it, red)
    }
    it
  }
  it
}
$"ABCDabcd" a b c d$

By using repr(), you can learn more about what kinds of character you need to replace:

(note that I added "a" and > to the equation as well to learn more about the kinds of contents)

#repr($"ABCDabcd" "a" a > b c d$)
// equation(
//   block: false,
//   body: sequence(...),
// )",

#$"ABCDabcd" "a" a > b c d$.body.children.map(x => {
  (x, x.func())
})
// (
//   ([ABCDabcd], text),
//   ([ ], space),
//   ([a], text),
//   ([ ], space),
//   ([a], symbol),
//   ([ ], space),
//   ([>], symbol),
//   ([ ], space),
//   ([b], symbol),
//   ([ ], space),
//   ([c], symbol),
//   ([ ], space),
//   ([d], symbol),
// )

You can see that there are three kinds of content in there: text (that you don’t want to format), space, and symbol (the content of interest). That symbol is a math element function, and not the same as the symbol type! A bit confusing, but we can work with that.

Your previous approach would style the "a": it is a single character, but not italic as it is not a math symbol. Here’s a show rule that, as far as I understand your requirements, works:

#show math.equation: it => {
  // we need to refer to the symbol element function somehow
  let math-symbol = $a$.body.func()

  // match all symbols
  show math-symbol: it => {
    // style if it's a lowercase letter
    set text(red) if it.text.match(regex("^[a-z]$")) != none
    it
  }

  it
}

$"ABCDabcd" "a" a > b c d$

image

1 Like

Thank you so much. Typst writing code is not easy and not flexible enough.