How can I create and use context-specific formatting rules?

I was wondering if Typst had support for context when processing specific rules. For the sake of the argument, to make things clear, let’s say I define the following show rule:

#show "FOO": #smallcaps("foo")

Would there be a way to alter that show rule depending on immediate context, like whether or not it’s preceded by a punctuation mark?

Yes, I could create 2 versions manually in this instance and while being able to write an auto-capitalising #Smallcaps() variant would be neat, I’m more interested in the kind of mechanisms Typst may or may not provide to solve that family of problems instead of this particular one.

I do know Typst has support for Context. But as far as I know it doesn’t provide something magic like #content.cur_word.split_sentence that would return all the words on the left (and, separately, on the right) of the current word, in the current sentence. So my question could become: how would you go around to create such context? Does it have to be done in Rust land?

You could use a regex show rule to extract individual sentences. You can then modify the sentences manually or with nested show rules. Note that the matching does not work correctly if there is an equation inside the sentence, and there will probably be other things that interfere with the regex matching.

#show regex(".*?\."): it => {
  it
}
1 Like

Hi @Gael, could you maybe try to revise your post’s title to be a complete question as per the question guidelines:

Good titles are questions you would ask your friend about Typst.

We hope by adhering to this, we make the information in this forum easy to find in the future.

I wasn’t sure what a proper title would be – is your question more about accessing content around a given content in context, or about a different kind of value that Typst could support (this is what I assumed from the current title)?

Thanks!

2 Likes