# How to get a per page total count?

How could I in the following document replace the X by the correct per-page-total? (29, 31, 20 in my compilation).

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{zref-perpage,expl3}
\newcounter{mycounter}
\zmakeperpage{mycounter}

\begin{document}\pdfsetrandomseed 4
\ExplSyntaxOn
\int_step_inline:nnnn {1 }{1}{80}
{\refstepcounter{mycounter}\themycounter.~an~object \\
\int_if_odd:nT {\fp_eval:n { randint( 9 ) }}{some~randomness\\}
}
\ExplSyntaxOff
\end{document}


I suggest to store the expanded number of objects per page in a \prop list and extract the value of that list in the header, where the property name is just the page number (hopefully not reset in between); even if the number of objects has to be evaluated more than once (in the loop), this is will update the key - value only (since property - lists have a unique key - value relationship, the potential older value is overwritten)

I tested this with a loop of 5000 iterations (resulting in 169 pages) and the results are correct. (Takes a few seconds, however)

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{zref-perpage,expl3}
\newcounter{mycounter}
\zmakeperpage{mycounter}

\begin{document}\pdfsetrandomseed 4

\ExplSyntaxOn
\cs_generate_variant:Nn \prop_item:Nn {No,Nx}
\cs_generate_variant:Nn \prop_gput:Nnn {Nxx}
\cs_new:Npn \propextract{%
\prop_item:Nx \g_ufischer_object_counter {\thepage}
}
\prop_new:N \g_ufischer_object_counter
\int_step_inline:nnnn {1 }{1}{80}
{\refstepcounter{mycounter}\themycounter.~an~object \\
\int_if_odd:nT {\fp_eval:n { randint( 9 ) }}{some~randomness\\}
\prop_gput:Nxx \g_ufischer_object_counter {\thezpage} {\themycounter}

}
\ExplSyntaxOff
\end{document}

• It fails when different page numberings systems are used, you need to use \thepage instead of \number\value{page}. – Ulrike Fischer Mar 30 '18 at 12:53
• @UlrikeFischer: That's a design decision then -- your original setup did not have other page numberings – user31729 Mar 30 '18 at 13:45
• But your code is not consistent. You are using \thezpage which is a formatted counter to store the values but the unformatted \number\value{page} to retrieve them. But beside this your answer is fine and helped me to solve my actual problem! – Ulrike Fischer Mar 30 '18 at 13:54
• @UlrikeFischer: I was in a hurry. I replaced \number\value{page} with \thepage – user31729 Mar 30 '18 at 18:26