typst

Access math.equation numbering and labeling in template


I am writing a template for my reports in Typst and I want to move the following part of the code (it only enumerates labeled equations) from my main script, where it works fine, to my template script.

#set math.equation(numbering: "(1)")
#show math.equation: set block(breakable: true)

#show ref: it => {
  [equation #numbering(
      it.element.numbering,
      ..counter(math.equation).at(it.element.location())
    )]
}

#show math.equation: it => {
  if it.block and not it.has("label") [
    #counter(math.equation).update(v => v - 1)
    #math.equation(it.body, block: true, numbering: none)#label("")
  ] else {
    it
  }  
}

When I moved this part of code to a function conf() in template that wraps the document, there were no compiling errors in the template, however when I tried to reference to an equation, I got an error "cannot reference equation without numbering".

I tried to make another function, that I pass equations into, since I assume the error arises from the show rules, as they apply only to the script they are written in. Here is the function I've written in my template script

#let customEq(content) = {
  set math.equation(numbering: "(1)")
  show math.equation: set block(breakable: true)

  show ref: it => {
    [equation #numbering(
        it.element.numbering,
        ..counter(math.equation).at(it.element.location())
      )]
  }

  show math.equation: it => {
    if it.block and not it.has("label") [
      #counter(math.equation).update(v => v - 1)
      #math.equation(it.body, block: true, numbering: none)#label("")
    ] else {
      it
    }  
  }

  content
}

and here is the code I wrote in my main script to filter out equations that are written in text

#show math.equation: it => {
  if it.block {
  customEq(it.body)
  } else {
    it
  }
}

but the compiling error remains the same. I hope the question is well structured, as it is my first time writing on this platform. Thank you for the answers in advance!


Solution

  • The missing part seems to be the show: conf or show: customEq in the main document to "activate" the show and set rules.

    I've tried your code and could make it work, so to me, it is only a question of how you import and declare your code in the two typ files.

    //template.typ
    #let conf(body) = {
      // set and show rules
      body
    }
    
    //main.typ
    #import "template.typ":*
    
    #show: conf
    
    $ x + y $ <test>
    

    If you want some ideas on how to reduce your code to something simpler, depending on what you need, you could use: (credit to Typst forum post)

    //template.typ
    #let customEq(body) = [
      #show math.equation: it => {
        if it.numbering == none and it.has("label") {
          math.equation(it.body, numbering: "(1)", block: true)
        } else { it }
      }
      #body
    ]
    
    
    //main.typ
    #import "template.typ": *
    
    #show: customEq
    
    $ x + y $
    $ a + b $ <xyz>
    $ c + d $ <abc>
    
    

    Also, have you had a look at the Typst package equate?