How to get the position of an item placed by a show rule in Typst 0.12?

Let’s say I place a title using a show rule. In Typst 0.11 I could get its position with a query, but in 0.12 I get the position of the place call:

#set page(margin: 20pt, paper: "a6", flipped: true)
#set block(spacing: 0pt)

#show heading: it => place(
  top,
  float: true,
  clearance: 0pt,
  block(width: 100%, height: 40pt, fill: luma(90%), it),
)
  
#block(width: 100%, height: 100pt, fill: eastern)

= Title

#context locate(heading).position()

image

In Typst 0.11 this would show y: 20pt which is indeed the vertical position of the title. How can I get it in Typst 0.12?

First, note that locate deprecated. Instead use:

#context query(heading).first().location().position()

Now as you noticed, it shows you the position where the heading is placed before applying the show rule, and not the position of the place.

I don’t know if that’s a bug or intended, maybe @PgBiel can tell.

But also, what are you trying to achieve? I’m pretty sure show heading: it => place(...) is not a good pattern. If you tell us what you want to do (title page?) maybe we can give you a better way to implement it.

Actually, locate(loc => ...) (using locate as a means of obtaining the current location) is deprecated. Using locate(selector) to obtain the location of something else was added in Typst 0.11 together with context and is not deprecated, being encouraged if that is the purpose of the usage. :slight_smile:

I’m not sure, but either way it seems ambiguous. I’d recommend not using a show rule for this and using a query which unambiguously matches something inside place, e.g. by adding a label.

1 Like

This was the result of a few internal changes. It solves some problems and creates some new ones. :slight_smile: For figures specifically, it ensures that the figure numbering stays correct even within floats, but at the same time it makes links to figures not jump to their floating contents. It’s a very tricky situation.

I see, thanks for the links. That looks tricky indeed.

@PgBiel I had tried with a label without success:

#show heading: it => place(top, float: true, [#it <a>])
= Title
#context query(<a>)

Here the query returns nothing for some reason (and I guess it might still give the wrong location if it did work). But it works when I replace it with it.body. I think that’ll do for my use case, I’ll just have to recreate the numbering manually.

The it in this case was already visited by Typst (since you are in its show rule) and will not be considered further. That’s kind of a subtle edge case.

1 Like