Access variables between Pages

I have a shortcode which increases a counter each time I call it and associate the current value to a key:

  {{ .Page.Scratch.Add "nfig" 1}}
  {{ .Page.Scratch.Set (.Get "key") (.Page.Scratch.Get "nfig") }}

I use this code to count the number of figures in my site and associate to each of them a number.
(I try to reproduce the same figure numbering than in LaTeX for example).
Currently, I have another shortcode used to make reference to this counter from other pages:

{{ with .Site.GetPage "/blog/" }}
  Figure {{ .Page.Scratch.Get (.Get "key") }} <br>
  Counter {{ .Page.Scratch.Get "nfig" }} <br>
{{ end }}

However, this code does not display the same thing depending on the pages:
Sometimes, I have:
Figure X (X depends on the “key”, 1 to 3)
Counter 3

And in another page, I have:

In the last case, the different variables seem to not exist depending on the caller page.
I supposed it comes from the order used to build the pages, but I am not sure of this hypothesis.
I think a solution can be to force Hugo to build two times the website to create the different variables before using them, but I am not sure if it is possible …
Any idea on this issue ?

Do you want unique figure numbers per page, or should every figure on the site have a unique figure number?

Ideally I want a unique figure number per page.
However, if you have a working solution with a unique figure number for every figure on the site, it also interests me !

Follow up question…

If you have 3 figures on Page A, what do you want to know about those figures when you are rendering Page B?

If in Page A I have (the parenthesis are not displayed):
Figure 1 (associated to key=“a”)
Figure 2 (associated to key=“b”)
Figure 3 (associated to key=“c”)

I want on Page B (and for any possible Page B) to be able to recover the value associated to each Figure using the corresponding key, to be able to do things like:

“In Figure (key=“a” ->) 1 of Page A, …”
“In Figure (key=“b” ->) 2 of Page A, …”
“In Figure (key=“c” ->) 3 of Page A, …”

Instead of my actual result which displays in some Page B:

“In Figure of Page A, …”
“In Figure of Page A, …”
“In Figure of Page A, …”

I hope to be clear enough …

Let’s start with an example that auto-numbers the figures on each page using the shortcode .Ordinal property.

git clone --single-branch -b hugo-forum-topic-37903 hugo-forum-topic-37903
cd hugo-forum-topic-37903
hugo server

Post 1 contains the figures.
Post 2 references the figures on Post 1.

I am still not clear why you need a .Scratch.

It’s impossible to pass variables between Pages with .Scratch due to the parallel nature of Hugo.

In the Hugo documentation about “Page Variables”, it is written:

See .Scratch for page-scoped, writable variables.

Because I need at least variables for each page, I thought it was the good solution …

How can I do that without using .Scratch ?

Thanks for this suggestion: I didn’t know .Ordinal. I will read the documentation about it.
However, in your example, you are refering to the Figure directly by using their number:

[this figure on Post 1]({{< relref "posts/post-1#figure-1" >}})

In my case, I want to be able to dynamically recover this number only using a key…