Why is the template function I wrote not working properly?

Basically, show will take everything below it and process it using the function you specify. For example:

#show: style
content

is equivalent to:

#style[content]

Based on this rule, let’s take a look at your code.

Firstly,

#let theorems(doc) = [
  #show: thmrules
  #doc
]

is equivalent to:

#let theorems(doc) = [
  #thmrules(doc)
]

So, in reality, thmrules and theorems are exactly the same.

Secondly, when you call show: all, your doc (which, in this case, refers to your entire document) is replaced by:

#theorems(doc)   // the document with theorem styles
#styles(doc)     // the document with other styles
#doc             // the original document

This means your document will be repeated three times, each with different styles.

If you want to apply both styles to your document simultaneously (i.e., you want the result to be):

#theorems(styles(doc))

you can simply define all as:

#let all(doc) = thmrules(styles(doc))

Or, if you prefer a more readable and neat approach:

#let all(doc) = [
  #show: thmrules
  #show: styles
  #doc
]
4 Likes