#Only number an equation if it has a label

16 messages ยท Page 1 of 1 (latest)

gilded lark
#

Is it possible to automatically give an equation a numbering if and only if it has a label?

To be completely clear, I want the following

$ a $
$ b $ <eqa>
$ c $
$ d $ <eqb>

to compile automatically to

a
b (1)
c
d (2)

rich walrus
#

if you want just one label you can make counter(<some-label>)

gilded lark
#

@rich walrus what do you mean?

grand swift
#

?render

#show math.equation:it => {
    if it.fields().keys().contains("label"){
        math.equation(block:true, numbering: "(1)", it)
        // change your numbering style in `numbering`
    } else {
        it        
    }
}

$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-2>
$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-3>
gilded lark
#

@grand swift thank you so much!

#

Oh no! This seems to break references to equations ๐Ÿ˜ฆ

#

?render

#show math.equation:it => {
    if it.fields().keys().contains("label"){
        math.equation(block:true, numbering: "(1)", it)
        // change your numbering style in `numbering`
    } else {
        it        
    }
}

$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-2>
$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-3>
In @ep-2 and @ep-3 we see equations
small kilnBOT
# gilded lark ?render ``` #show math.equation:it => { if it.fields().keys().contains("labe...

An error occurred:

Error: cannot reference equation without numbering
    โ•ญโ”€[/main.typ:21:4]
    โ”‚
 21 โ”‚ In @ep-2 and @ep-3 we see equations
    โ”‚    โ”€โ”€โ”€โ”€โ”€  
    โ”‚            
    โ”‚ 
    โ”‚ Help: you can enable equation numbering with `#set math.equation(numbering: "1.")`
โ”€โ”€โ”€โ”€โ•ฏ

Error: cannot reference equation without numbering
    โ•ญโ”€[/main.typ:21:14]
    โ”‚
 21 โ”‚ In @ep-2 and @ep-3 we see equations
    โ”‚              โ”€โ”€โ”€โ”€โ”€  
    โ”‚                      
    โ”‚ 
    โ”‚ Help: you can enable equation numbering with `#set math.equation(numbering: "1.")`
โ”€โ”€โ”€โ”€โ•ฏ

small kilnBOT
gilded lark
#

Okay so this works and keeps the clickable links, but the counter has a off-by-one error in the snippet render....

pale cosmos
#

I think there is a PR on this topic by @final pasture

small kilnBOT
short shale
#

That first if is equivalent to it.has("label") btw

gilded lark
#

?render

#let eqnumbering = "(1)"

#show math.equation:it => {
    if it.has("label"){
        math.equation(block:true, numbering: eqnumbering , it)
    } else {
        it
    }
}

#show ref: it => {
    let el = it.element
    if el != none and el.func() == math.equation {
      link(el.location(),numbering(
        eqnumbering,
        counter(math.equation).at(el.location()).at(0)+1
      ))
    } else {
      it
    }
}

$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-2>
$ sum_x^2 $ 
$ dif/(dif x)(A(t)+B(x))=dif/(dif x)A(t)+dif/(dif x)B(t) $ <ep-3>
In @ep-2 and @ep-3 we see equations