#boolean operation not casting datatype to bool
48 messages · Page 1 of 1 (latest)
could you paste your typst code?
dont do that
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
[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]
}
in other words you'd write
context {
let isTitlePage = counter(page)...etc == counter(page)...etc
// ...
if isTitlePage [yes] else [no]
}
okay, I don't really understand what it means to decalare depending on context, or independent of context for that matter. I'll read up on it a bit more
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
more info here: https://typst.app/docs/reference/context/
but here
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
for sure. I really need to learn contexts :p
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
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?
yeah!
great! I've been wondering all afternoon how to do that 😅 thanks a bunch
no problem
Kind of a tangent to the main point of this thread but related to this ^
Can I update old code that used locate to just use a context block instead?
yes
you should actually
Ok awesome
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()