How can I remove spaces between bold and non-bold content with show rule?

With the function ns defined below, I can replace spaces with the text SPACE

#let ns(txt) = {
  show regex("\ "): it => [SPACE]
  txt
}

#ns[one two three four] // shows oneSPACEtwoSPACEthreeSPACEfour

However, if some of that content is bolded, the show rule is ineffective for adjacent spaces:

#ns[one *two* three four] 

This shows “one two threeSPACEfour”. Using repr, I see the two inputs are structured differently:

#repr[one two three four] 
// gives [one two three four]
#repr[one *two* three four] 
// gives sequence([one], [ ], strong(body:[two]), [ ], [three four]) 

… but I’m still not sure why the show rule isn’t catching the space between bolded and unbolded words. How can I use show rules that apply to those spaces as well?

Solution:

#let func-seq = [].func()

#show func-seq: it => {
  let blank = [ ]
  let c = it.children
  if (blank in c) {
    func-seq(c.filter(x => x != blank))
  } else {
    it
  }
}

one *two* three

Thanks. I applied your solution like this:

#let func-seq = [].func()

#let ns(txt) = {
  show regex("\ "): it => []
  show func-seq: it => {
    let blank = [ ]
    let c = it.children
    if (blank in c) {func-seq(c.filter(x => x != blank))} else {it}
  }
  txt
}

/aʊ/ as in #ns[*ou* t]
1 Like

You can simplify this in two ways:

  • There is no need for a regex, as you can just match a string containing a space.
  • You don’t need to edit sequences by hand, as you can create a show rule for space elements.
#let ns(txt) = {
  show " ": none
  show [ ].func(): none
  txt
}
2 Likes