#boolean operation not casting datatype to bool

48 messages · Page 1 of 1 (latest)

half leaf
#

The two inspected elements yield different page numbers. The isTitlePage variable is a bool (or so it seems) and yet I cannot use it for an if statement; see error message.

raw reef
#

could you paste your typst code?

dusk salmon
#

that will never return true

#

cuz when u write context code(), code() isnt evaluated immediately, it's delayed until the document is laid out

#

otherwise it doesnt have access to the context

#

instead you have to declare all of your code depending on context stuff as dependent on context

half leaf
# raw reef could you paste your typst code?
[title page: ]; context counter(page).at(<Abstract>).first(); linebreak()
  [this page: ]; context counter(page).at(here()).first(); linebreak()
  
  let isTitlePage = context counter(page).at(<Abstract>).first() == context counter(page).at(here()).first()
  
  isTitlePage; linebreak()
  
  if isTitlePage {
    [yes]
  } else {
    [no]
  }
dusk salmon
#

in other words you'd write

context {
  let isTitlePage = counter(page)...etc == counter(page)...etc
  // ...
  if isTitlePage [yes] else [no]
}
half leaf
dusk salmon
#

generally code is run before your document is laid out

#

so your code isnt aware of the amount of pages in the document, amount of headings, its own location in the document, stuff like that

#

thats what context provides

#

it does that by creating a document element

#

similar to a heading, figure, and others you know

#

but what that element does is: run your code and display what it returns

#

but at layout time, after code outside it

half leaf
dusk salmon
#

just not where it was before

half leaf
#

ahh, I misread that

#

it works now. Thanks! I'm currently rewriting my department's latex template to typst, it's a great exercise, but the learning curve is kinda steep

dusk salmon
#

np

#

yea typst is pretty powerful but you gotta get used to its idioms

half leaf
#

for sure. I really need to learn contexts :p

dusk salmon
#

dont worry, it's simpler than it sounds :p

#

it was much more confusing in the past

#

there were like 2 different functions (style(styles => ...) and locate(loc => ...)) which used callbacks

#

meaning you had to manually pass that particular bit of the context around as a parameter

#

context { } does both under the hood for you

#

so anything that depends on the context to work (e.g. here() to get the current location) will just receive the context as a parameter "implicitly"

#

but only when inside a context block

#

otherwise it will complain

half leaf
#

so is the point of the context block make the piece of code reactive to where it is called rather than where it is declared?

dusk salmon
#

yeah!

half leaf
#

great! I've been wondering all afternoon how to do that 😅 thanks a bunch

dusk salmon
#

no problem

storm jasper
storm jasper
#

Ok awesome

dusk salmon
#

there are some things that still need a location though

#

but for those it makes sense

#

and then you can use let loc = here()

#

e.g. if you want to access the value of a counter at a specific location you use .at(loc)

#

but to get at the current location you dont have to counter("...").at(here()), you can just counter("...").get()