Here it is in context, though the returned content no longer behaves like a heading.
#show heading.where(level: 1): it => {
if not it.body.has("children") {return it}
let kinder = it.body.children
let has-equation = kinder.any(child => child.func() == [$x$].func())
if has-equation {
[#kinder.at(0) ]
} else {
[#kinder.at(0) #kinder.at(2)]
}
}
I’ll try to explain it but the best understanding will come from playing around with it and finding things out for yourself.
At this line kinder is a variable of the type array, so it contains multiple items (though arrays can also be empty). Every variable of type array has the any() function available for use (and many others too!). any works by taking a function as an argument, then returning a single true/false value. It asks the question “do any values contained within this array satisfy the given requirement?”. The requirement is defined by the function you give it.
For instance:
#let numbers = range(5)
All numbers: #numbers
Is number 1 in the array? #numbers.any(number => number == 1)
Does the array contain any even numbers? #numbers.any(calc.even)
Does the array contain any numbers of type `float`? #numbers.any(n => type(n) == float)
An array is created using range and is then printed.
Then there are a few questions asked. First in English (which the compiler does not understand) and with a function (which the compiler does understand). The function that any expects must take one value and return true/false. This function is then applied to each value in the array. The format for defining a function has a few forms which are demonstrated in the code (note that there are more ways to do it than demonstrated here).
Side note:
The map() function works in a really similar way except it returns another array. This new array is the result of applying the given function to each value in the “old” array.
#range(5).map(n => n * 2) //(0, 2, 4, 6, 8)
Edit: Oops, I only focused on one aspect of that line.
The function given is child => child.func() == [$x$].func() which in natural language would ask the question:
Is the func() of this child equal to the func() of something that I know is an equation?
And func() is something that all content has which can be used to check its type.
I think using the other function ,type(x) == type($x$), is more clear in what it is trying to achieve so that’s probably the better option between the two I provided. Maybe someone knows an even more clear option.
If you want to hide a specific child of the heading body, could you do that with a show rule instead? show X: none will hide the element X for example.
There is the slightly more performance intensive (I’m guessing) query based way to find the equation, which will also always find an equation without having to rift through content directly. If the first solution works for you though, no reason to abandon it.
This takes advantage of equations now being locatable in typst 0.14.
#show heading: it => {
context {
let has-equation = query(
selector(math.equation)
.after(here())
.before(selector(<_heading_end>).after(here()))
).len() > 0
set text(red) if has-equation
it
}
[#metadata(none)<_heading_end>]
}
= A
= _B $x^y$_
= C