How to get the total number of pages?

I can use (nr-pages) in a scheme session to get the total number of pages. How do I use it to insert it in the document or in the footer? nr-pages is not a macro.

Through the extern macro (see the manual for how to use it).

1 Like

Mimicking the manual, I define this in the preamble.

(assign "total-number-pages" (macro (extern "(lambda () (nr-pages))")))

Then I got the error

If I change it to

(assign "total-number-pages" (macro (extern "(lambda () (number->string (nr-pages)))")))

Then TeXmacs freezes.

This grammar looks correct to me, maybe nr-pages has some bugs?

IIRC, ‘extern’ commands get called without the proper context of the document tree where they were called from (which would be a reasonable expectation, in my opinion). It may be possible to create a ‘locus’ in the document, pass its ID to the extern and use it in Scheme to manipulate the locus content.

Somehow this macro does not freeze the editor but always gives “1” in generic. On the other hand, it gives the correct number of slides in beamer style! This is strange, because I recall slide in beamer is different from page in usual documents.

1 Like

That macro doesn’t work for me in the generic style.

It does work in beamer, as you say. This is because nr-pages is redefined in Scheme in this context:
https://codeberg.org/XmacsLabs/mogan/src/commit/1df66fa6a02159cc93c2fd1cf9ff2be963a5b3b4/TeXmacs/progs/dynamic/fold-edit.scm#L1038

In the beamer style, seperate slides are elements of the document tree, so it is easy to count them. In the generic style this is not the case, so to count pages the document needs to be broken up into pages by the typesetter first.

I see. But it does work in a Scheme session with generic style.

It seems to me that somehow access to the box eb (defined in edit_main.hpp) causes problems. This box does not seem to be properly set up when calling from an extern.

Note, for example, that (get-page-width #t) works in extern, as well as a Scheme session, but the similar (get-total-width #t) only works in a Scheme session. get-page-width relies on the environment, while get-total-width uses the eb box width.

(nr-pages) also uses the eb box via the C++ nr_pages (see edit_main.cpp)

2 Likes